#! /bin/sh
# Smalltalk package installer

request_help=
request_version=
no_load=
files=

prefix=/usr
exec_prefix=/usr

while [ -n "$1" ]; do
  case $1 in
  	--version)	 request_version=yes	;;
  	--help)		 request_help=yes	;;
  	-n|--noload)	 no_load=yes		;;
  	*)		 files="$files $1"	;;
  esac
  shift
done

if [ -n "$request_version" ]; then
  echo GNU Smalltalk install-pkg, version 1.95.1
  exit 0
fi

if [ -z "$files" -o -n "$request_help" ]; then
  cat <<\EOF
Syntax: gst-package [--version] [--help] [-n|--noload] files...

gst-package requires write access to the `packages' file in the GNU
Smalltalk image directory, and installs the packages whose description
is contained contained in the given files.  The format of the files is:

    package-name
       prerequisites
       files-to-load (relative to the start-dir, as specified below)
       start-dir (relative to the kernel path)
       
The image path is $(datadir)/gnu-smalltalk.
The kernel path is $(datadir)/gnu-smalltalk/kernel.

EOF
  # If no options were passed, return an error.
  test -z "$files" && test -z "$request_help" && exit 1
  exit 0
fi


cat $files >> $(datadir)/gnu-smalltalk/packages

if [ -z "$no_load" ]; then
  for file in $files; do
    pkgs="$pkgs `gawk '
	BEGIN { FS="#"; n = 3 }
	length ($1) > 0 {
	  n++;
	  if (n == 4) { name = $1; n = 0; }
	  if (n == 3) { printf "%s ", name; }
	}' $file`"
  done

  test -n "$pkgs" && xargs /usr/bin/gst -qK Load.st -a
fi
