#!/bin/sh
#
# script to stage, (bootstrap) and create a tarball

progname="$0"

clean_up=yes
make_tar=yes
bootstrap=no
latest=no
pristine=no
revision_tag=yes

while [ $# -gt 0 ]; do
  case "$1" in
    ## bootstrap dist
    "--bootstrap" )
      bootstrap=yes
    ;;
    ## This option tells make-dist to update to latest svn.
    "--latest" )
      latest=yes
    ;;
    ## This option tells make-dist to delete the staging directory
    ## when done.  It is useless to use this unless you make a tar file.
    "--no-clean-up" )
      clean_up=no
    ;;
    ## make a tar file.
    "--no-tag" )
      revision_tag=no
    ;;
    ## make a tar file.
    "--no-tar" )
      make_tar=no
    ;;
    ## check tree has no local modifications.
    "--pristine" )
      pristine=yes
    ;;
    "--help")
      echo "Usage: ${progname} [options]"
      echo ""
      echo "  --bootstrap	: bootstrap the distribution (slow)"
      echo "  --latest	: svn update to latest head"
      echo "  --no-clean-up	: don't delete staging directory when done"
      echo "  --no-tag	: don't use svn revision tag in name"
      echo "  --no-tar	: don't create a tarball"
      echo "  --pristine	: check no local modifications in tree"
      echo ""
      exit 0
    ;;

    * )
      echo "${progname}: unrecognized argument: $1" >&2
      echo "Run \"${progname} --help\" for help" >&2
      exit 1
    ;;
  esac
  shift
done

# trace and exit on error
set -e

BRANCH=`basename ${PWD}`
NAME=iiimf

if [ "${latest}" = yes ]; then
  svn update || exit 1
fi

if [ "${pristine}" = yes ]; then
  echo "Checking tree is pristine..."
  pristine=`svn status | wc -l`
  [ $pristine -ne 0 ] && echo Tree is not pristine && exit 1
  unset pristine
fi

VERSION=`echo ${BRANCH} | sed -e 's/_/./g' -e 's/^r//'`

if [ "${revision_tag}" = "yes" ]; then
    echo "Getting svn revision number"
    REVISION=-svn`LANG=C svn info | grep "Revision: " | sed -e "s/Revision: //"`
fi

VERREL=${VERSION}${REVISION}
DIRNAME=${NAME}-${VERREL}
TARNAME=${NAME}-src-${VERREL}.tar.bz2

[ -r ${TARNAME} ] && echo "tarball already exists" && exit 0

### Make sure the temp subdirectory is available.
tempparent="make-dist.tmp.$$"
if [ -d ${tempparent} ]; then
  echo "${progname}: staging directory \`${tempparent}' already exists.
Perhaps a previous invocation of \`${progname}' failed to clean up after
itself.  Check that directories whose names are of the form
\`make-dist.tmp.NNNNN' don't contain any important information, remove
them, and try again." >&2
  exit 1
fi

mkdir ${tempparent}

STAGE_DIR=${tempparent}/${NAME}-${VERREL}

echo "Exporting tree to stage..."
svn export . ${STAGE_DIR} || exit 1

touch ${STAGE_DIR}

if [ "${bootstrap}" = yes ]; then
  echo "Bootstrapping..."
  make -C ${STAGE_DIR} bootstrap
fi

if [ "${make_tar}" = yes ]; then
  echo "Generating ${TARNAME}..."
  tar jcf ${TARNAME} -C ${tempparent} --exclude sources --exclude autom4te.cache ${DIRNAME}
fi

if [ "${clean_up}" = yes ]; then
  echo "Cleaning up the staging directory"
  rm -rf ${tempparent}
else
  [ -d ${DIRNAME} ] && echo "${DIRNAME}/ already exists" && exit 1
  cd ${tempparent} && mv ${DIRNAME} .. && rm -rf ${tempparent}
fi
