#!/bin/sh
# 
# inisqueak -- setup a directory for use with Squeak
# 
#   Copyright (C) 1996-2004 by Ian Piumarta and other authors/contributors
#                              listed elsewhere in this file.
#   All rights reserved.
#   
#   This file is part of Unix Squeak.
# 
#      You are NOT ALLOWED to distribute modified versions of this file
#      under its original name.  If you modify this file then you MUST
#      rename it before making your modifications available publicly.
# 
#   This file is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#   FITNESS FOR A PARTICULAR PURPOSE.
#   
#   You may use and/or distribute this file ONLY as part of Squeak, under
#   the terms of the Squeak License as described in `LICENSE' in the base of
#   this distribution, subject to the following additional restrictions:
# 
#   1. The origin of this software must not be misrepresented; you must not
#      claim that you wrote the original software.  If you use this software
#      in a product, an acknowledgment to the original author(s) (and any
#      other contributors mentioned herein) in the product documentation
#      would be appreciated but is not required.
# 
#   2. You must not distribute (or make publicly available by any
#      means) a modified copy of this file unless you first rename it.
# 
#   3. This notice must not be removed or altered in any source distribution.
# 
#   Using (or modifying this file for use) in any context other than Squeak
#   changes these copyright conditions.  Read the file `COPYING' in the
#   directory `platforms/unix/doc' before proceeding with any such use.
# 
# Author: Ian.Piumarta@INRIA.Fr
# 
# Last edited: 2003-08-31 07:31:35 by piumarta on cartman.inria.fr

MAJOR=39
VERSION=3.9a-7024

prefix=/opt/local
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
imgdir=/opt/local/share/squeak
plgdir=/opt/local/lib/squeak/3.9-8

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
