#!/bin/sh
trap '
   echo "

***					 Help!						***
***	 Something went wrong during installation.	***
***												***
***	 Please correct the problem, try again, or	***
***	 consult Ithaca Software at (415) 523-5900	***

(Aborted)
"				1>&2; trap 0; exit 2' 0 

trap "exit 2" 1 2

if test -r /bin/sun && /bin/sun
then
	SYSTEM="Sun"
else
	echo "This machine doesn't look like a	Sun!" 1>&2
	exit 2
fi

export SYSTEM

FLOATSWITCH=""
export FLOATSWITCH
VERSION=`awk '/HOOPS_VERSION/  \
    {printf ("%s.%s", substr($3,1,1), substr($3,2)) }' \
    hoops-source/hoops.h`
version=`awk '/HOOPS_VERSION/  \
    {printf ("%s%s", substr($3,1,1), substr($3,2)) }' \
    hoops-source/hoops.h`

if /bin/sparc
then
	INCDIR=/usr/include
	LIBDIR=/usr/lib
	LINTDIR=/usr/lib/lint
elif /bin/sun3 || (test -r /bin/sun3x && /bin/sun3x)
then
	INCDIR=/usr/include
	LIBDIR=/usr/lib
	LINTDIR=/usr/lib/lint
elif /bin/i386
then
	INCDIR=/usr/local/include
	LIBDIR=/usr/local/lib
	LINTDIR=/usr/local/lib/lint

	if mkdir /usr/local/lib >/dev/null 2>&1
	then
		chmod +r /usr/local/lib >/dev/null 2>&1
	fi

	if mkdir /usr/local/include >/dev/null 2>&1
	then
		chmod +r /usr/local/include >/dev/null 2>&1
	fi

	if mkdir /usr/local/lint >/dev/null 2>&1
	then
		chmod +r /usr/local/lint >/dev/null 2>&1
	fi
else
	echo "This machine doesn't look like a supported Sun workstation!" 1>&2
	exit 2
fi

echo "


						Greetings!
		
	 This is the Hoops $VERSION / $SYSTEM installation procedure.
"|page

DEFAULT=1

THREECHOICE="Run"

while true 
do
echo "
You can:

		1) Install Hoops as a regular system utility,

		2) De-install Hoops as a regular system utility,

		3) $THREECHOICE the Demos,

		4) Clean out the installation files, 

		5) Exit this procedure.

"
echo -n "Which would you like to do? [$DEFAULT] "
read ans
echo ""

case "$ans" in
	""|$DEFAULT)
		ans=$DEFAULT 

		case $ans in
			1)	DEFAULT=3 ;;
			3)	DEFAULT=4 ;;
			4)	DEFAULT=5 ;;
		esac ;;
esac

case $ans in

1)		if test `whoami` != root 
		then
			echo "Sorry; you are now logged in as \"`whoami`\"; you must be"
			echo "logged in as root to copy things to system directories."
			continue
		fi
		case "$FLOATSWITCH" in
			"") FLOATSWITCH=`sh -$- hoops-demo/getfloat.sh`;;
		esac
		echo "
	You can:

		1) Install Hoops with BOTH X11 and SunView drivers,

		2) Install Hoops with ONLY the X11 driver,

		3) Install Hoops with ONLY the SunView driver.

		"
	echo -n "Which would you like to do? [1] "
		read ans
		cd hoops-source
		rm -rf libhoops${FLOATSWITCH}.a
		rm -rf libhoops${FLOATSWITCH}.so*
		case $ans in
			""|1)
				echo ""
				echo "Installing Hoops with BOTH X11 and SunView drivers."
				echo ""
				echo "This will take several minutes."
				ar x hoops${FLOATSWITCH}.a
				rm -rf sunfake.o x11fake.o ;;
			2)
				echo ""
				echo "Installing Hoops with ONLY the X11 driver."
				echo ""
				echo "This will take several minutes."
				ar x hoops${FLOATSWITCH}.a
				rm -rf sundrive.o x11fake.o ;;
			3)
				echo ""
				echo "Installing Hoops with ONLY the SunView driver."
				echo ""
				echo "This will take several minutes."
				ar x hoops${FLOATSWITCH}.a
				rm -rf sunfake.o x11drive.o ;;
		esac
		echo ""
		echo -n "Creating the appropriate Dynamic Hoops Library ... "
		ld -o libhoops${FLOATSWITCH}.so.0.$version -assert pure-text *.o
		echo "done."
		echo ""
		echo -n "Creating the appropriate Static Hoops Library ... "
		ar crl libhoops${FLOATSWITCH}.a ????*.o
		ranlib libhoops${FLOATSWITCH}.a
		rm -f *.o __.SYMDEF >/dev/null 2>& 1
		echo "done."
		echo ""
		echo "Installing Hoops..."
		echo 
		echo
		echo -n "Copying libhoops.so to $LIBDIR ... "
		cp libhoops${FLOATSWITCH}.so* $LIBDIR
		chmod +r $LIBDIR/libhoops.so*
		/etc/chown root $LIBDIR/libhoops.so*
		echo "done."
		echo -n "Copying libhoops.a to $LIBDIR/libhoops.a ... "
		cp libhoops${FLOATSWITCH}.a $LIBDIR/libhoops.a
		chmod +r $LIBDIR/libhoops.a
		/etc/chown root $LIBDIR/libhoops.a
		ranlib $LIBDIR/libhoops.a
		echo "done."
		echo -n "Configuring ld cache for shareable libraries ... "
		/usr/etc/ldconfig
		echo "done."
		echo
		echo -n "Copying hc.h and hf.h to $INCDIR ... "
		cp h[cf].h $INCDIR
		chmod +r $INCDIR/h[cf].h 
		/etc/chown root $INCDIR/h[cf].h 
		echo "Done."
		echo
		echo -n "Copying the lint library to $LINTDIR... "
		cp llib-lhoops.ln $LINTDIR/llib-lhoops.ln
		cp lintlib.ccc $LINTDIR/llib-lhoops
		chmod +r $LINTDIR/llib-lhoops*
		/etc/chown root $LINTDIR/llib-lhoops*
		cd ..
		echo "Done."
		echo 
		echo 
		echo "You will want to set a value for \"HOOPS_PICTURE\" in your .login"
		echo "or .profile file - please see the release notes for details."
		echo 
		echo "Installation complete."
		echo ;;

2)		if test `whoami` != root 
		then
			echo "Sorry; you are now logged in as \"`whoami`\"; you must be"
			echo "logged in as root to delete things from the system."
			continue
		fi
		echo -n "Removing Hoops from the system: are you sure? [no] "
		read ans
		case $ans in
			y|Y|yes|Yes|YES) 
				echo -n "Working... "
				rm -rf	$INCDIR/h[cf].h \
						$LIBDIR/libhoops.a \
						$LIBDIR/libhoops.so* \
						$LINTDIR/llib-lhoops.ln \
						$LINTDIR/llib-lhoops 
				echo "Done."	;;

			*)	echo "(Aborted)";;
		esac
		echo ;;

3)		sh -$- hoops-demo/demo.sh;;

4)		echo -n "Cleaning up: are you sure? [no] "
		read ans
		case $ans in
			y|Y|yes|Yes|YES) 
				echo -n "Working... "
				rm -rf hoops-install hoops-source hoops-demo
				echo "All gone."
				echo
				break;;

			*)	echo "(Aborted)";;
		esac
		echo ;;

5|"")	break;;

*)		echo "\"$ans\" isn't one of the choices!"
		echo;;

esac
done
echo

trap 0
