#!/bin/ksh

# Zapp install script for AIX 4.1

# ----------------------------------
#    Must be root
# ----------------------------------
if [ `whoami` != "root" ]
then
         echo ""
         echo "must be root to install^G^G..."
         echo "please become root and try again..."
         echo "exiting."
         exit 1
fi

# ----------------------------------
#    Check the tar file
# ----------------------------------
if [[ $# != "1" ]]
then
         echo ""
         echo "Usage: ZappInstall <compressed_tar_file>"
         echo ""
         echo "exiting."
         exit 1
fi


# ----------------------------------
#    Get absolute file name
# ----------------------------------
if [[ `echo $1|cut -c1` = "/" ]]
then
	FILE=$1		#absolute
else
	FILE=`pwd`/$1 	# relative
fi
 

# ----------------------------------
#    Check for previous installation
# ----------------------------------
if [[ -d /usr/lpp/OpenGL/tools/Zapp ]]
then
	echo "\n\nA previous version of Zapp has been detected."
	echo "\nPress ENTER to continue or ^C to ABORT."
	read junk
	/bin/rm -r /usr/lpp/OpenGL/tools/Zapp
fi


# ----------------------------------
#    Expand tar file
# ----------------------------------

cd /usr/lpp/OpenGL/tools
echo "\n\nExpanding tar file\n"

isCompressed=`file $FILE | grep -c compressed`
if [[ $isCompressed = "0" ]]
then
	#echo "File $1 is NOT compressed"
	tar -xvf $FILE
else
	#echo "File $1 is compressed"
	zcat $FILE | tar -xvf- 
fi


# ----------------------------------
#    Set the file permissions
# ----------------------------------

cd /usr/lpp/OpenGL/tools
chmod -R a-x Zapp
chmod -R a+r Zapp
chmod -R u+w Zapp
chmod  a+x Zapp Zapp/bin Zapp/util Zapp/build Zapp/code Zapp/doc
find Zapp -name 'Zapp*'  -exec chmod a+x {} ';'
find Zapp -name '*.awk'  -exec chmod a+x {} ';'
find Zapp -name '*.html' -exec chmod a-x {} ';'
find Zapp -name '*.c'    -exec chmod a-x {} ';'


# ----------------------------------
#    Add links into /usr/bin
# ----------------------------------

ln -sf /usr/lpp/OpenGL/tools/Zapp/bin/Zapp         /usr/bin/Zapp
ln -sf /usr/lpp/OpenGL/tools/Zapp/util/ZappClean   /usr/bin/ZappClean
ln -sf /usr/lpp/OpenGL/tools/Zapp/util/ZappIndent  /usr/bin/ZappIndent
ln -sf /usr/lpp/OpenGL/tools/Zapp/util/ZappSplit   /usr/bin/ZappSplit
ln -sf /usr/lpp/OpenGL/tools/Zapp/util/ZappSwap   /usr/bin/ZappSwap
ln -sf /usr/lpp/OpenGL/tools/Zapp/doc/ZappDoc     /usr/bin/ZappDoc  


# ----------------------------------
#    Build libZGL.a           
# ----------------------------------

if [[ -r /usr/lpp/OpenGL/tools/Zapp/build/buildfile ]]
then
	echo "\n\nBuilding libZGL.a\n"
	cd /usr/lpp/OpenGL/tools/Zapp/build
	make -f buildfile all
	slibclean
	echo "\n\nDone.\n"
else
	echo "Unable to build libZGL.a"
	echo "Installation failed."
	exit 1
fi




