#!/bin/sh
# set -x
#	SID	"@(#)install.sh	3.5 - 95/11/10"

DEST_DIR=`pwd`
SOURCE_DIR=`pwd`
if [ -w $SOURCE_DIR ]; then
	SAVE_TAR="YES"
else
	SAVE_TAR="NO"
fi

progName=`basename $0`

usage() {
	echo "$progName [-x] [-r|-s] [-d destination]"
}

showexpl() {
	usage
	cat <<!

Install ANGOSS

-x		display this explanation
-d		Where to install, default is current directory
-r		Remove tar files when finished installing
-s		Save (or rather don't remove) tar files (default)

Note 1: You should be the user who will own the SmartWare files.
Note 2: If you are restoring from a read only directory, the files are copied
before decompressing.  In this case, the -r/-s refers to the copied files.

Example which installs ANGOSS in current directory, keeping tar files:

install -s

!
}

# setarg varName argv         - argv is like -c parm ...
# sets varName to value parm, exiting if there are not enough values
#
setarg() {
	varName=$1; shift
	optName=$1; shift
	if [ $# -ge 1 ]; then
		eval $varName=$1
	else
		echo Missing argument to $optName >&2
		usage 2
		exit 1
	fi
}

if [ $# = 0 ] ; then
	showexpl
	exit 1
fi

while [ $# -ge 1 ] ; do
	case $1 in
	-x)		showexpl; exit;;
 	-d)		setarg DEST_DIR $*;;
	-s)		SAVE_TAR="YES";;
	-r)		SAVE_TAR="NO";;
	*) 	shift;;
	esac
	shift
done

PKGS="base X11 char dev dicts fonts grphconv printers plotters samples system tutorial util"

if [ $SAVE_TAR = YES ] ; then
	echo "Install SmartWarePLUS into $DEST_DIR - Saving tar files..."
elif [ $SAVE_TAR = NO ] ; then
	echo "Install SmartWarePLUS into $DEST_DIR - Deleting tar files..."
else
	showexpl
	exit 1
fi

COMPRESS="compress"
UNCOMPRESS="uncompress"
COMP_EXT="Z"

if [ -f /bin/uname ]; then
	case `uname -s` in
		"Linux"*)
		COMP_EXT="gz"
		UNCOMPRESS="gunzip"
		COMPRESS="gzip"
	esac
fi

if [ ! -w $SOURCE_DIR ]; then
	if [ $DEST_DIR = $SOURCE_DIR ] ; then
		echo "You must specify a RW destination when installing from a RO directory"
		exit 1
	fi
	SOURCE_RO="YES"
else
	SOURCE_RO="NO"
fi

if [ ! -d $DEST_DIR ] ; then
	echo "Making directory $DEST_DIR"
	mkdir $DEST_DIR
	if [ ! -d $DEST_DIR ] ; then
		echo "Directory creation failed!"
		exit 1
	fi
fi

if [ ! -w $DEST_DIR ]; then
	echo "You must specify a Read/Write destination directory"
	exit 1
fi

for pkg in $PKGS; do
	if [ -r $pkg.tar.$COMP_EXT ]; then
		if [ $SOURCE_RO = YES ] ; then
			echo "Copying $pkg.tar.$COMP_EXT to $DEST_DIR"
			cp $pkg.tar.$COMP_EXT $DEST_DIR
			if [ -r $pkg.perms.$COMP_EXT ]; then
				echo "Copying $pkg.perms.$COMP_EXT to $DEST_DIR"
				cp $pkg.perms.$COMP_EXT $DEST_DIR
			fi
			if [ -r $pkg.perms ]; then
				echo "Copying $pkg.perms to $DEST_DIR"
				cp $pkg.perms $DEST_DIR
			fi
			PKG_DIR=$DEST_DIR
		else
			PKG_DIR=$SOURCE_DIR
		fi
		echo "Uncompressing $PKG_DIR/$pkg.tar.$COMP_EXT ... "
		$UNCOMPRESS $PKG_DIR/$pkg.tar.$COMP_EXT

		cd $DEST_DIR
		echo "Installing $pkg.tar ... "
		tar xf $PKG_DIR/$pkg.tar
		if [ "$SAVE_TAR" = "NO" ]
		then
			echo "Removing $PKG_DIR/$pkg.tar ... "
			rm -f $PKG_DIR/$pkg.tar
		else
			echo "Recompressing $PKG_DIR/$pkg.tar ... "
			$COMPRESS $PKG_DIR/$pkg.tar
		fi
		if [ -r $PKG_DIR/$pkg.perms.$COMP_EXT ]; then
			echo "Uncompressing $pkg.perms.$COMP_EXT ... "
			$UNCOMPRESS $PKG_DIR/$pkg.perms.$COMP_EXT
		fi
		if [ -r $PKG_DIR/$pkg.perms ]; then
			echo "Setting file and directory permissions for $pkg ... "
			for i in `cat $PKG_DIR/$pkg.perms`;do
				f=`echo $i | cut -f 1 -d:`
				perm=`echo $i | cut -f 2 -d:`
				chmod $perm $f
			done
			if [ "$SAVE_TAR" = "NO" ]
			then
				echo "Removing $PKG_DIR/$pkg.perms ... "
				rm -f $PKG_DIR/$pkg.perms
			else
				echo "Recompressing $PKG_DIR/$pkg.perms ... "
				$COMPRESS $PKG_DIR/$pkg.perms
			fi
		fi
		echo "done $pkg"
		cd $SOURCE_DIR
	else
		echo "Warning! Missing Package $pkg..."
	fi
done

echo "Done!"
echo ""
echo "Please see readdemo.txt for help getting started with SmartWare PLUS."
echo ""

if [ ! -f $DEST_DIR/xfontinstall ] ; then
	if [ ! $DEST_DIR = $SOURCE_DIR ] ; then
		cp $SOURCE_DIR/xfontinstall $DEST_DIR
	fi
	echo "Now, if you run X11, become root and run xfontinstall while in $DEST_DIR"
else
	echo "Now, if you run X11, become root and run the xfontinstall script"
fi


