#!/bin/sh
#  ezpkg - JE install, remove, setup packages -
#  Copyright (C) 1995 Takashi MANABE (manabe@Roy.dsl.tutics.tut.ac.jp)
#
#  EZinst is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program 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.
#  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

SHDIR=/install
DOINST=$SHDIR/doinst.sh
EZVAR=/var/lib/ezinst
PKGS=$EZVAR/installed
SCRS=$EZVAR/scripts

# extract packages and run doinst.sh
installPackages() {
    while [ $# != 0 ]; do
	av="$1"; shift
	if [ -f $av ]; then
	    rm -f $DOINST
	    echo installing $av
	    base=`basename $av .tgz`
	    tar -zxvlpf $av -C / > /tmp/$base
	    if [ $? = 0 ]; then
		mv /tmp/$base $PKGS
		if [ -f $DOINST ]; then
		    echo running doinst.sh
		    (cd /; /bin/sh $DOINST -install)
		    mv $DOINST $SCRS/$base.p
		fi
	    else
	    echo $av
		rm -f $DOINST /tmp/$base
		exit 1
	    fi
	else
	    exit 1
	fi
    done
}

removePackages() {
    while [ $# != 0 ]; do
	av="$1"; shift
	remove=$SCRS/$av.p
	pkg=$PKGS/$av
	if [ -f $pkg ]; then
	    rm -f `cat $pkg`
	    if [ -f $remove ]; then
		echo running $remove
		(cd /; /bin/sh $remove -remove)
		rm -f $remove
	    fi
	fi
    done
}

setupPackages () {
    if [ -d $SHDIR ]; then
	cd $SHDIR
	for setup in `ls *.s`
	do
	    echo running $setup
	    /bin/sh $setup -setup
	    mv $setup $SCRS/$setup
	done
    fi
}

case "$1" in
-setup) setupPackages $* ;;
-remove) removePackages $* ;;
*) installPackages $* ;;
esac
exit
