#!/bin/sh
# 
# inisqueak -- setup a directory for use with Squeak
# 
# @copyright@
# 
# Author: Ian.Piumarta@INRIA.Fr
# 
# Last edited: 2003-08-31 07:31:35 by piumarta on cartman.inria.fr

MAJOR=3
VERSION=3.6-5429

prefix=/usr/local
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
imgdir=${prefix}/lib/squeak
plgdir=/usr/local/lib/squeak/3.6-3

if test ! -w .; then
  echo "You don't have write permission in this directory." >&2
  exit 1
fi

# Sun's /bin/sh does not understand "test -e", but [/usr]/bin/test does
test="`which test`"

list=false
startup=true
batch=false
interactive=true

while true; do
  case "$1" in
    -l) list=true; shift;;
    -n) startup=false; shift;;
    -b) batch=true; interactive=false; shift;;
    -h|-help|--help)
        echo "`basename $0` [-option...] [rootdir]"
        echo '    -b      batch (avoid interaction, exit status reflects success)'
        echo '    -h      you already know about'
        echo '    -help   same as -h'
        echo '    -l      always present a list of alternative images (overrides -b)'
        echo '    -n      do not run Squeak after installing the files'
        echo '    --help  same as -help'
        exit 0;;
    *)  break;;
  esac
done

if test "$1" != ""; then
  bindir=${1}/${bindir}
  imgdir=${1}/${imgdir}
  plgdir=${1}/${plgdir}
fi

SQUEAK=${bindir}/squeak
SOURCES=SqueakV${MAJOR}.sources
IMAGE=squeak.image.gz
CHANGES=squeak.changes.gz

# local install function

missing()
{
  echo "The file ${1} is missing." >&2
  echo "Please check your Squeak installation." >&2
  exit 1
}

if ${test} \( -f squeak.image \) -a \( -f squeak.changes \) -a \( -e ${SOURCES} \)
then
  if ${startup}; then
    if test ! -x ${SQUEAK}; then
      missing "${SQUEAK}"
    fi
    if ${interactive}; then
      echo "Running ${SQUEAK}";
    fi
    exec ${SQUEAK}
  else
    if ${interactive}; then
      echo "Squeak is already installed in this directory" >&2
    fi
    exit 1
  fi
fi

install()
{
  cpy="${1}"
  src="${2}"
  red="${3}"
  dst="${4}"
  if ${test} ! -e ${dst}; then
    if ${test} -e ${src}; then
      if ${interactive}; then
	echo "+ ${cpy} ${src} ${red} ${dst}";
      fi
      eval      ${cpy} ${src} ${red} ${dst}
    else
      missing "${src}"
    fi
  else
    echo "${dst} is already present -- leaving it alone"
#   startup=false
  fi
}

# choose an image to install

if ${list}; then :; else
  if ${test} \( ! -e ${imgdir}/${IMAGE} \) \
          -o \( ! -e ${imgdir}/${CHANGES} \) ; then
    if ${batch}; then
      echo "Could not find default image to install -- giving up." >&2
      exit 1;
    fi
    list=true
    echo "No default image, looking for alternatives..."
    echo
  fi
fi

if ${list}; then
  images=`ls -1 ${imgdir}/*.image.gz 2>/dev/null | \
          sed "s.${imgdir}/..;s/.image.gz//"`
  if test "$images" = ""; then
    echo "I could not find an image to install." >&2
    echo "Please check your Squeak installation." >&2
    exit 1
  fi
  nimg=`echo ${images} | tr ' ' '\012' | wc -l`
  nimg=`echo ${nimg}`
  more=true
  while ${more}; do
    echo "I found the following images:"
    echo ${images} | tr ' ' '\012' | cat -n
    if echo ${images} | fgrep "Squeak${VERSION}" >/dev/null; then
      echo "(of which I might recommend Squeak${VERSION}, unless you know better)."
    fi
    echo -n "Which one should I install [1-${nimg}]? "
    read reply
    case ${reply} in
      [1-9]) ;;
      [1-9][0-9]) ;;
      *) echo
         echo "Let's try that again, with a NUMBER between 1 and ${nimg}."
         echo
         continue;;
    esac
    if test \( ${reply} -ge 1 \) -a \( ${reply} -le ${nimg} \); then
      more=false
    fi
    if ${more}; then
      echo
      echo "Ha ha, very clever.  Now give me a number between 1 and ${nimg}"
      echo "(or hit your interrupt key [^C or whatever] if you don't like"
      echo "the look of any of them)."
      echo
    fi
  done
  IMAGE=`echo ${images} | tr ' ' '\012' | tail +${reply} | head -1`
  CHANGES=${IMAGE}.changes.gz
  IMAGE=${IMAGE}.image.gz
fi

if ${interactive}; then echo "Installing ${IMAGE} in `pwd`"; fi

install "ln -s"      "${imgdir}/${SOURCES}" " " "${SOURCES}"
install "gunzip -dc" "${imgdir}/${IMAGE}"   ">" "squeak.image"
install "gunzip -dc" "${imgdir}/${CHANGES}" ">" "squeak.changes"

if ${startup}; then
  if test ! -x ${SQUEAK}; then
    missing "${SQUEAK}"
  fi
  if ${interactive}; then
    echo "Running ${SQUEAK}";
  fi
  exec ${SQUEAK}
fi
