#! /bin/sh

# Build a Guile release.

# TODO:
# * Run Sanitize.
# * Various FIXME comments mark specific problems.
# * Consider mailing output to someone.
# * For now use --with-threads=no, because QT doesn't compile on Linux.

# Version of this file.
VERSION=1.2

# Hosts for a full build.
HOSTS='lisa axon snuffle'

# Modules to tag/check out.
MODULES='guile'

# Some variables.  The convention in this file is that boolean false
# is an empty value, and boolean true is the string "yes".
tag=''
local=''
release=''
prefix=''
work=yes
skip=''

while test "$#" -gt 0; do
   case "$1" in
    --tag=*)
       tag=`echo "$1" | sed 's/^--tag=//'`
       ;;

    --tag | -t)
       tag="$2"
       shift
       ;;

    --local | -l)
       local=yes
       ;;

    --prefix=*)
       prefix=`echo "$1" | sed 's/^--prefix=//'`
       ;;

    --prefix | -p)
       prefix="$2"
       shift
       ;;

    --release=*)
       release=`echo "$1" | sed 's/^--release=//'`
       ;;

    --release | -r)
       release="$2"
       shift
       ;;

    --skip-tag | -s)
       skip=yes
       ;;

    -n | --dry-run)
       work=''
       ;;

    --help | -h)
       echo "Usage: release-guile [OPTION]..."
       echo "  Note RELEASE is required"
       echo
       echo "Valid options are:"
       echo "  -l, --local           Build only on local host"
       echo "  -n, --dry-run         Show commands but don't run them"
       echo "  -p PREFIX, --prefix=PREFIX"
       echo "                        Set install prefix"
       echo "  -r REL, --release=REL Set release name"
       echo "  -s, --skip-tag        Don't tag or export, just build"
       echo "  -t TAG, --tag=TAG     Tag to give release"
       echo "  -h, --help            Print help and exit"
       echo "  -v, --version         Print version and exit"
       exit 0
       ;;

    --version | -v)
       echo "release-guile version $VERSION"
       exit 0
       ;;
   esac
   shift
done

if test -z "$release"; then
   echo "release-guile: release must be specified" 1>&2
   exit 1
fi

if test -z "$prefix"; then
   prefix=/usr/cygnus/guile-$release
   echo "release-guile: using prefix $prefix" 1>&2
fi

if test -z "$tag"; then
   tag=guile-$release
   echo "release-guile: using tag $tag" 1>&2
fi

# The -n option is handled by prefixing all commands with $echo.  This
# doesn't work too well in general; in particular you must be careful
# about redirections.
echo=
if test -z "$work"; then
   echo=echo
fi


if test -z "$skip"; then
   # We tag the release twice.  The first time we put a tag where the
   # branch occurs.  The second time we make an actual branch tag.
   $echo cvs -d cvs:/rel/cvsfiles -f -Q rtag -r HEAD bp-$tag $MODULES
   $echo cvs -d cvs:/rel/cvsfiles -f -Q rtag -b -r bp-$tag $tag $MODULES

   # Now export from the branch.  FIXME maybe we should consider doing a
   # checkout and not an export.  If we do this we will have to come up
   # with a way to strip the CVS directories from the install directory.
   # FIXME consider using -kv.
   $echo cvs -d cvs:/rel/cvsfiles -f -Q export -r $tag $MODULES
fi


# The source should now be in devo/guile.  Check that it is so.

test -d devo/guile || {
   echo "release-guile: directory devo/guile does not exist?!" 1>&2
   exit 1
}

test -f devo/config.guess || {
   echo "release-guile: file devo/config.guess does not exist" 1>&2
   exit 1
}


# Spawn remote builds if allowed.
if test -z "$local"; then
   script="$0"
   pwd=`pwd`
   if test -z "$work"; then
      wflag=-n
   else
      wflag=''
   fi
   for host in $HOSTS; do
      rsh -n $host cd $pwd \; $script -l -r $release $wflag -p $prefix -s &
   done
   echo "release-guile: remote builds started"

   # Install the source.  Do this here because it only needs to be
   # done once.
   $echo ./devo/guile/mkinstalldirs $prefix/src
   if test -z "$work"; then
      echo "release-guile: install source in $prefix/src here..."
   else
      (cd devo; tar cf - .) | (cd $prefix/src; tar xBpf -)
   fi
   exit 0
fi


# Doing a local build on some machine.  First determine our host
# triplet, and our actual prefix.
triplet=`sh devo/config.guess`
prefix=$prefix/$triplet

# FIXME blow away old build directory here?
$echo mkdir $triplet
$echo cd $triplet

# Configure
if test -n "$work"; then
   # Doing work, so redirect output.  Save a copy of stdout, so that
   # doing a local build and piping into mail causes a message to be
   # generated when build is done.
   exec 3>&1
   exec > CONFIG.LOG 2>&1
fi
$echo ../devo/configure --prefix $prefix --enable-i18n --with-threads=no

# Build
if test -n "$work"; then
   exec > BUILD.LOG 2>&1
fi
$echo make all

# Check.
if test -n "$work"; then
   exec > CHECK.LOG 2>&1
fi
$echo make check

# Install.  Note that while we do a full devo build, we only install
# in guile.  The reason is that otherwise we would have confusion
# between the Tcl used by expect and that used in Guile.
if test -n "$work"; then
   exec > INSTALL.LOG 2>&1
fi
$echo cd guile
$echo make install
