#!/bin/bash

# This script is really intended for "batch-mode" nightly builds of
# Squeak binary distributions.  It is therefore intended mainly for
# the VM maintainer (who runs it from cron) rather than VM hackers in
# general (who would probably prefer to run it from the command line).
# Nevertheless it does attempt to be interactive (and even makes some
# concessions towards user-friendliness ;) for those who cannot be
# bothered to type `mkdir bld; cd bld; ../.../configure; make') -- but
# your kilometrage may vary (enormously).
# 
# Run `build -h' for a list of options (without much explanation).

# Last edited: 2002-06-07 06:12:13 by piumarta on emilia.inria.fr

UTS_SYS=`(uname -s) 2>/dev/null`

if [ -x /usr/5bin/echo ]; then                          # SunOS SysV echo
  echo=/usr/5bin/echo
elif [ -z "`(echo -e a) 2>/dev/null | fgrep e`" ]; then # GNU bash, etc.
  echo="echo -e"
else                                                    # generic SysV
  echo=echo
fi

if [ "${UTS_SYS}" = QNX ]; then
  echo=echo
fi

ouch () {
  $echo
  $echo 'Ouch!  That hurts!!'
  exit 1
}

trap ouch 2

srcdir=""
blddir=""
batch=""
doConfig=yes
doMake=tes

getYN() {
  if [ "${batch}" ]; then
    YN="y"
  else
    YN=""
    while [ ! "${YN}" ]; do
      read YN
      case ${YN} in
	y|Y|n|N) ;;
	*)       $echo "Did you mean Y or N ? \c" >&2; YN="" ;;
      esac
    done
  fi
  $echo ${YN}
}

getN () {
  N=0
  while [ ${N} -eq 0 ]; do
    read n
    let n="${n}"
    if [ "${n}" -a \( ${n} -gt 0 \) -a \( ${n} -le ${1} \) ]; then
      N=${n}
    else
      $echo "Please answer a number in the range [1-${1}]: \c"
    fi
  done
  $echo ${N}
}

usage () {
  $echo "usage: ${0} [-c] [-h] [-m] [-y] [-s srcdir] [-b blddir]" >&2
}

help () {
  usage
  cat >&2 <<\EOF
  -b dir     build in `dir'
  -c         run `configure' only (don't make)
  -h         show this help message
  -m         run `make' only (don't configure)
  -s dir     VMMaker sources are in `dir'
  -y         assume `y' to all questions and work silently (`batch' mode)
EOF
  exit 0
}

notFound () {
  $echo "VMMaker src directory not found" >&2
  exit 1
}

while [ $# -gt 0 ]; do
  case $1 in
    -b) shift; blddir="${1}"; shift;;
    -c) shift; doMake="";;
    -h) help;;
    -m) shift; doConfig="";;
    -s) shift; srcdir="${1}"; shift;;
    -y) shift; batch=yes;;
    *)  usage; exit 1;;
  esac
done

[ $# -gt 0 ] && usage && exit 1

if [ "${srcdir}" ]; then
  srcdirs="${srcdir}"
else
  srcdirs=`$echo src*`
fi

[ ! "${srcdirs}" ] && notFound

count=0;

for d in ${srcdirs}; do
  [ -d ${d} ] && let count="${count}+1"
done

chooseDir () {
  $echo "We found the following source directories:"
  i=0
  for d in ${srcdirs}; do
    let i="${i}+1"
    list="${list}  [${i}] ${d}
"
  done
  $echo
  $echo "${list}"
  if [ "${batch}" ]; then
    $echo "batch mode failed!" >&2
    exit 1
  fi
  $echo "Which would you like to use [1-${i}]? \c"
  n=`getN ${count}`
  srcdir=`$echo "${list}" | fgrep "[$n]" | sed "s,^\[.*\] ,,"`
  [ "${srcdir}" ] || notFound
}

case ${count} in
  0) notFound ;;
  1) srcdir=${srcdirs} ;;
  *) chooseDir ;;
esac

$echo "Using source directory: ${srcdir}"

if [ ! "${blddir}" ]; then
  vers=`$echo ${srcdir} | sed 's,^src,,'`
  mach=`uname -m | sed 's,i.86,i386,;s,sun4.,sparc,'`
  blddir="bld${vers}-${mach}"
fi

$echo "Using build directory: ${blddir}"

if [ ! "${batch}" ]; then
  $echo "Is this okay [y/n]? \c"
  case `getYN` in
    y|Y) ;;
    n|N) $echo "Goodybye" >&2; exit 1 ;;
    *)   $echo "Huh?" >&2; exit 1 ;;
  esac
fi

[ -d ${blddir} ] || mkdir ${blddir}

cd ${blddir}

if [ "${doConfig}" ]; then
  $echo
  $echo '-------------------------------------------------------------------------------'
  $echo "Configuring in ${PWD}"
  $echo '-------------------------------------------------------------------------------'
  $echo
  ../platforms/unix/config/configure --with-src=${srcdir} || exit 1
fi

if [ "${doMake}" ]; then
  $echo 
  $echo '-------------------------------------------------------------------------------'
  $echo "Building in ${PWD}"
  $echo '-------------------------------------------------------------------------------'
  $echo
  make || exit 1
fi

$echo
$echo '-------------------------------------------------------------------------------'

if [ ! -x squeak ]; then
  $echo "The build appears to have failed.  We can't really continue." >&2
  $echo "Goodbye." >&2
  exit 1
fi

$echo "The build appears to have succeeded."

[ "${batch}" ] && exit 0

if [ "`whoami`" != "root" ]; then
  $echo "We cannot install Squeak because you are not root" >&2
  exit 0
fi

$echo "Would you like me to install Squeak in the default location [y/n]? \c"
case `getYN` in
  y|Y) ;;
  n|N) $echo "Less work for us that way."; exit 0 ;;
  *)   $echo "Huh?"; exit 1 ;;
esac

$echo
make install
$echo

$echo "We're done.  Happy Squeaking!"
