#!/bin/bash
#--------------------------------------------------------------------------------
#	Licensed Program Product:
#	VisualAge for Smalltalk for Linux
#
#	(C) COPYRIGHT International Business Machines Corp. 1995, 2001
#	All Rights Reserved
#
#	US Government Users Restricted Rights - Use, duplication or
#	disclosure restricted by GSA ADP Schedule Contract with
#	IBM Corp. and its licensors.
#
#	Changes:
#	2001.08.07 GWC - Linux: bash, print -> echo, typeset -> tr
#	2001.08.06 GWC - Linux,Solaris changes
#	2001.08.01 GWC - 6.0, Linux
#	2001.04.25 GWC - V5.5.2 from V5.5.1
#	2000.09.25 GWC - V5.5.1 from V5.5
#	2000.03.30 GWC - Initial V5.5 from V5.0
#
# Generated   InstallScriptsEtc  (01/28/2002 05:01:46 PM)
#--------------------------------------------------------------------------------

#--------------------------------------------------------------------------------
#	Directory Structure of cd
#	{cd}
#		nls
#		bitmaps	
#		{unix}	[this is where the setup script is]
#			bin
#
#	Where {unix} -> = install_a, install_h, install_l, install_s
#--------------------------------------------------------------------------------


#--------------------------------------------------------------------------------
# 	Verify superuser authority. It is not necessary for the user to
#	actually *be* 'root'.  It is only necessary that they have
#	root-like authority.
#--------------------------------------------------------------------------------
touchfile=/a.$$.tmp
touch $touchfile 2>/dev/null
if [  $? -ne 0  ] ; then
	echo -e "ERROR: You must have root authority to run this program."
	exit 1
fi
rm $touchfile

#--------------------------------------------------------------------------------
# Script var summary:
#
#	IMAGE		- the name of the Smalltalk icx file. (WITH the .icx,
#			but WITHOUT any leading path information.
#			Example:  setup.icx
#
#	LINKDIR		- the directory into which we create a complete
#			shadow of the CD tree using symbolic links.
#			All filename and directory name spellings and
#			case will be correct in this shadow tree.
#
#	MOUNTDIR	- the root directory of the installation CD
#
#	NAMESFILE	- The temporary file used to build the shadow
#			tree in LINKDIR
#--------------------------------------------------------------------------------


#--------------------------------------------------------------------------------
#	Get/Set the CD-ROM drive mount point.
#	
#	If exactly one parameter is passed in, then it will be used
#	as the mount point.  Otherwise we will use our parent
#	directory. [Relative to where this script was started from.]
#--------------------------------------------------------------------------------
if [  $# -eq 1  ] ; then
	MOUNTDIR=$1
else
	MOUNTDIR=`(cd ..; pwd)`       
fi

IMAGE=setup.icx
LINKDIR=/tmp/vastcd-6.0.3 
NAMESFILE=/tmp/f$$.tmp	# tmpfile for link names

# -- remove any existing version of the namesfile
trap "rm -rf $NAMESFILE $BINDIR ; exit" INT QUIT TERM EXIT


#--------------------------------------------------------------------------------
#	Function: 	strip_iso9660
#	calls:		
#	Convert filenames by clipping off the ;# and .# suffixes
#--------------------------------------------------------------------------------
function strip_iso9660
{
	lcname=$(echo $1 | tr '[:upper:]' '[:lower:]')
	lcname=${lcname%\;*}
	lcname=${lcname%\.}
	echo $lcname
} 

#--------------------------------------------------------------------------------
#	Function: 	makelinks
#	calls:		strip_iso9660
#
#	Create link tree under $PWD from $MOUNTDIR 
#--------------------------------------------------------------------------------
function makelinks
{
   while read name
	 do
	   linkname=`strip_iso9660 $name`
	   if [ -d $MOUNTDIR/$name ]
		 then
			   mkdir $linkname 2>/dev/null
		 else
			   ln -s $MOUNTDIR/$name $linkname 2>/dev/null
	   fi
	 done
}

#--------------------------------------------------------------------------------
#	Function: 	dolink
#	calls:		makelinks
#
# 	Perform some checks, then create a tree of links 
# 	under $LINKDIR which maps onto $MOUNTDIR. We do
# 	this to get around the different CDROM device driver conventions
#	on various platforms.  For example, an ISO 9660 CD created on our
#	windows machine will look different on the unix platforms:
# 
#	Windows 	AIX			Solaris			HP
#	-------------	------------		------------		------------
# dir	MYDIR		mydir			mydir			MYDIR
# file	FOO.TXT		foo.txt			foo.txt			FOO.TXT;1
# file	FOO		foo.			foo.			FOO.;1
# 
# In the linking process we will consistenly use lowercase file and
#		directory names without any of the added '.' or ';1' noise.
#--------------------------------------------------------------------------------
function dolink    
{
	# Always recreate the link tree, just for safety.
	if [ -d $LINKDIR ] ; then
			rm -rf $LINKDIR
	fi
	echo -e "\nMaking link directory: $LINKDIR\c"
	mkdir $LINKDIR
	if [ $? -ne 0 ]       # bail out if directory couldn't be made
	then 
		echo -e ""
		echo -e "Press return to continue...\c"
		read PAUSEKEY
		exit 1
	fi
	chmod 755 $LINKDIR
	
	echo -e "\nStarting linkage process...\c"

	cd $MOUNTDIR
	# filter out files having a dot prefix... just in case.
	# Also, filter out the client directory since we are
	#	no longer zipping the client subdirectories.
	find . \
		\( -name client -o -name CLIENT \) -prune \
		-o ! -name ".*" -print > $NAMESFILE  

	# Also map the license directory for our client installs.
	find . \
		\( -name 'try.*' -o -name 'license.*' -o  -name 'TRY.*' -o -name 'LICENSE.*' \) \
		-print >> $NAMESFILE  


	echo -e "\nMaking the symbolic links...\c"
	cd $LINKDIR
	
	# make sure that the path between cwd and license exists.
	mkdir client
	mkdir client/base-manager-srt
	mkdir client/base-manager-srt/common_all
	mkdir client/base-manager-srt/common_all/license
	makelinks < $NAMESFILE

	echo -e "\nMapping completed.\n"
	rm $NAMESFILE
}

#--------------------------------------------------------------------------------
#	Function: 	set_default_locale
#		
#	Circumvent LANG=C 
#--------------------------------------------------------------------------------
function set_default_locale
{
	if [ X$LANG = X ] || [ $LANG = C ] 
	then
		loc=`locale -a | grep -i en_us | head -1`
		export LANG=$loc
	fi
}

#--------------------------------------------------------------------------------
#	Function: 	vacd_install
#	calls:		set_default_locale
#
#	Exec VisualAge for Smalltalk Installer
#--------------------------------------------------------------------------------
function vacd_install
{
	#
	# - - - Note: We will be running directly from the LINKDIR shadow tree.
	#
	
	#
	# - - - Workaround for CommonPrinting run-time bug.
	#
	export XPPATH=.
	

	#  Using chmod on the link to es doesn't work on linux. Replace
	#   the link (to es) with a copy of the real file and then use
	#   chmod to make the copy of es executeable.

	rm $LINKDIR/install_l/bin/es
	cp $MOUNTDIR/install_l/bin/es $LINKDIR/install_l/bin
	chmod 755 $LINKDIR/install_l/bin/es

	#
	# - - - Start the installer app
	#
	set_default_locale

	#
	# - - - Assumption: our bin directory ({cd}/install_?/bin) is first in $PATH
	#
	# - - - Also, do not exec 'es' -- we need "trap" to run on EXIT
	#
	# - - - Send stdout and stderr to a file.
	# - - - Pass in the mount dir as a parameter in case the
	#			install code needs to find the CDROM itself.
	es -mcd -no_break -i$IMAGE -z.us.$MOUNTDIR >/tmp/vastInstall-output 2>&1
}

#------------------------------------------------------------
# Linux
#
# Main Program Starts Here...
#------------------------------------------------------------
#
# - - - Setup platform-specific:
#         *) shared library search path,
#         *) executable search path,
#         *) cdrom install source directory.
#

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LINKDIR/install_l/bin
export PATH=$PATH:$LINKDIR/install_l/bin


clear
echo -e "
VisualAge for Smalltalk Install
--------------------------------------"

dolink
chmod -R a+x $LINKDIR/install_l

echo -e "Installer startup..."
cd $LINKDIR/install_l
vacd_install

# Remove the link tree
cd /tmp
rm -rf $LINKDIR
