#! /bin/sh

##
## Copyright 1998, The Portland Group, Incorporated.
## All rights reserved.
##
## THE PORTLAND GROUP, INCORPORATED PROPRIETARY INFORMATION
## This software is supplied under the terms of a license agreement
## or nondisclosure agreement with The Portland Group and may not be
## copied or disclosed except in accordance with the terms of that
## agreement.
## 

# XXX UPDATE this whenever updating the distribution
RELNUM="3.0-1"

quit() {
	echo "Exiting..."
	exit 1
}

list_products() {
	echo ""
	echo "This script installs PGI Workstation products for the"
	echo "Linux and Solaris86 operating systems.  Select the product"
	echo "below that you purchased, and the directory where the software"
	echo "is to be installed."
	echo ""
	echo "  1    C/C++"
	echo "  2    F77"
	echo "  3    HPF/F90/F77"
	echo "  4    F77/C/C++"
	echo "  5    HPF/F90/F77/C/C++"
	echo ""
}

get_product() {
	echo "Enter a number between 1 and 5:"
	read ans
}

change_install_dir() {
	echo ""
	echo "Default installation directory is $INSTALL_DIR."
	echo "Do you want to change the install location? [y,n]"
	read ans
}

get_install_dir() {
	echo "Specify a directory to install the product:"
	read ans
}

chmod_install_dir() {
        echo ""
        echo "Do you want the files in the install directory to be read-only? [y,n]"
        read ans
}
 
fix_linux86() {
	echo ""
	echo "Customizing $INSTALL_DIR/linux86/bin/linux86rc to your system"
	rc=$INSTALL_DIR/linux86/bin/linux86rc
	new=$rc.new
	script=/tmp/pgi.sed.$$
	# threads library
	THREADS="$INSTALL_DIR/linux86/lib/libpthread.a"
	# default values in linux86rc
	PGI_LINKER="/lib/ld-linux.so.1"
	PGI_GCCDIR="/usr/lib/gcc-lib/i586-unknown-linuxlibc1/2.7.2.2.f.2"
	PGI_GCCINC="/usr/i486-linux/include"
	PGI_G77DIR="/usr/lib/gcc-lib/i586-unknown-linuxlibc1/2.7.2.2.f.2"
	cp $rc $rc.orig
	# find gcc/g77 libs and include files (if any)
	type gcc >/dev/null 2>&1
	status=$?
	if test $status -eq 1 ; then
		echo ""
		echo "WARNING: gcc not found;"
		echo "         successful use of compilers requires existence of gcc libraries"
		echo ""
		GCCDIR=""
		GCCINC=""
	else
		specs=`gcc -v 2>&1 | awk '/Reading/ {print $4}'`
		GCCDIR=`dirname $specs`
		gcc1=`dirname $GCCDIR`
		gccrel=`basename $gcc1`
		GCCINC=/usr/$gccrel/include
		# find the linker
		LINKER=`grep dynamic $specs | sed -e 's/.*:-dynamic-linker //' -e 's/}} .*//'`
		if test "$LINKER" != "/lib/ld-linux.so.1" ; then 
			cat > $script << EOF
s@$PGI_LINKER@$LINKER@
EOF
			mv -f $THREADS.rh5 $THREADS
		else
			rm -f $THREADS.rh5
		fi
	fi
	type g77 >/dev/null 2>&1
	status=$?
	if test $status -eq 1 ; then
		echo ""
		echo "WARNING: g77 not found; option -g77libs will not be supported."
		echo ""
		G77DIR=""
	else
		g77lib=`g77 -v 2>&1 | awk '/Reading/ {print $4}'`
		G77DIR=`dirname $g77lib`
	fi
	cat >> $script << EOF1
s@$PGI_GCCDIR@$GCCDIR@
s@$PGI_GCCINC@$GCCINC@
s@$PGI_G77DIR@$G77DIR@
EOF1
	sed -f $script < $rc > $new
	mv -f $new $rc
	rm $script
}
# tarfile source
CDROM=`dirname $0`
# display legal license
more $CDROM/LICENSE
echo "Do you accept these terms? [accept,decline]"
read ans
if test "$ans" != "accept" ; then
	quit
fi
# what type of target?
if test -x /usr/bin/uname ; then
	uname=/usr/bin/uname
elif test -x /bin/uname ; then
	uname=/bin/uname
else
	# Never seen it anywhere but /bin or /usr/bin, so hopefully it's
	# in $PATH already.	
	type uname > /dev/null 2>&1
	if test $? -ne 0 ; then
		echo "install: uname not found in \$PATH environment variable"
		quit
	else
		uname=uname
	fi
fi
target=`$uname -s | tr '[A-Z]' '[a-z]'`
# GNU uname < 1.16 doesn't support -p option
if test "$target" = "linux" ; then
	target=linux86
	echo=/bin/echo
else
	processor=`$uname -p | tr '[A-Z]' '[a-z]'`
	case "$target-$processor" in
	sunos-*86 )
	        target=solaris86
	        echo=/usr/ucb/echo
	        ;;
	* )
		echo ""
		echo "install: products can only be installed on Linux or Solaris86"
		echo "         operating systems."
		quit
	esac
fi
# Sanity check: make sure $CDROM/$target exists.
if test ! -d $CDROM/$target ; then
	echo ""
	echo "install: system is $target; but $CDROM/$target does not exist."
	echo "         Incorrect tarfile downloaded?"
	quit
fi

list_products
get_product
# get partfile
case $ans in 
1 )	PART="$CDROM/.parts/$target-CC" ;;
2 )	PART="$CDROM/.parts/$target-F77" ;;
3 )	PART="$CDROM/.parts/$target-HPF" ;;
4 )	PART="$CDROM/.parts/$target-F77-CC" ;;
5 )	PART="$CDROM/.parts/$target-HPF-CC" ;;
* )	echo "install: Unknown product '$ans'.  Please run the install script again"
	echo "and select a number between 1 and 6."
	quit
	;;
esac

# if this happens the tarfiles were improperly built
if test ! -f $PART ; then
	echo "install: internal error (missing parts file $PART)"
	quit
fi
# where to install?  Try to create dir if doesn't exist.  Warn about installs
# that overlay previously-installed directory.
INSTALL_DIR=/usr/pgi
change_install_dir
if test "$ans" = "y" -o "$ans" = "yes" ; then
	get_install_dir
	INSTALL_DIR=$ans
fi
echo ""
if test "$INSTALL_DIR/.parts/license.dat" = "`pwd`/.parts/license.dat"; then
	echo "install: cannot install products in same directory as unpacked ftp tarfile"
	quit
fi
if test -d $INSTALL_DIR ; then
	touch $INSTALL_DIR/write_test > /dev/null 2>&1
	if test $? -eq 1 ; then
		echo "install: no write permission in installation directory ($INSTALL_DIR)"
		quit
	else
		rm -f $INSTALL_DIR/write_test
	fi
else
	mkdir $INSTALL_DIR > /dev/null 2>&1
	if test $? -eq 0 ; then
		echo "Note: install created $INSTALL_DIR directory"
	else
		echo "install: mkdir $INSTALL_DIR: permission denied"
		echo "install: installation directory ($INSTALL_DIR) not created"
		quit
	fi
fi
if test -f "$INSTALL_DIR/license.dat" ; then
	echo "Note: $INSTALL_DIR seems to contain a previous installation."
	echo "      This installation may fail to install files properly."
	echo "      You may see messages from tar about file permissions, etc."
	echo ""
fi
# install
echo "Installing software into $INSTALL_DIR (this may take some time)."
trap "" 2 3 
# XXX yuck
for i in $target $target/bin $target/include $target/lib ; do
	if test ! -d $INSTALL_DIR/$i ; then
		mkdir $INSTALL_DIR/$i 
	fi
done
cd $CDROM
for i in `cat $PART` ; do
	$echo -n \#
	tar cf - $i | (cd $INSTALL_DIR; tar xf - )
done
echo ""
# Linux requires changes to linux86rc and libs
if test $target = "linux86" ; then
	fix_linux86
fi
# license stuff
echo ""
mv $INSTALL_DIR/.parts/license.dat $INSTALL_DIR/license.dat
echo "Creating temporary license."
( cd $INSTALL_DIR; ./$target/bin/mkinst )
echo ""
PGI=$INSTALL_DIR
export PGI
unset LM_LICENSE_FILE # otherwise pglicinfo gets wrong license file
flexlm=`$target/bin/lmutil hostid | awk -F\" '/The FLEXlm/ {print $2}'`
hname=`uname -n`
cat > $INSTALL_DIR/info <<EOF
        FLEXlm hostid: $flexlm
        Hostname: $hname
        Installation: $INSTALL_DIR
	PGI Release: $RELNUM
EOF
# save installation info to file then print it
( cd $INSTALL_DIR; \
    $target/bin/pglicinfo 2>&1 | sed -e '/Hostid:/r info' > license.info )
cat $INSTALL_DIR/license.info
echo ""
echo "The above information was saved to $INSTALL_DIR/license.info."
echo "You will need to send it to PGI or your authorized PGI distributor"
echo "in order to receive your permanent license."
# cleanup
rm -f $INSTALL_DIR/install $INSTALL_DIR/README
rm -rf $INSTALL_DIR/.parts
rm -f $INSTALL_DIR/info
chmod_install_dir
if test "$ans" = "y" -o "$ans" = "yes" ; then
        chmod -R a-w $INSTALL_DIR
fi
echo ""
echo "Installation complete."
exit 0

