#!/bin/ksh
#
# Copyright 2000-2002 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# nsinstall
#
# Simple script to install Netscape packages for S7/S8.
#
# This script should be run as root, from a netscape package directory.
#

if [ $? != 0 ]; then
	echo "This script must be run as root"
	exit 1
fi

TEST9=0
for arg
do

	case "$arg" in
	-test9)
		TEST9=1
	;;
	esac
done
#
# set java jre version
#
JREVERSION=1.4.1

#
# Setting Package install list
#
## if SUNWj3rt directory exists, set JREPKG flag to 1 and add SUNWj3rt
## into the list (note: SUNWj3rt has to be installed before SUNWnsb since
## SUNWnsb's post installation script will create libjavaoji.so link to it).
##
## if SUNWgtku directory exist, set GTKPKG flag to 1 and add SUNWgcmn SUNWGlib
## SUNWGtkr SUNWGtku to the list 
#

if [ ! -d ./SUNWj3rt ]; then
    JREPKG=0
else
    JREPKG=1
fi;

if [ $TEST9 = 0 ]; then
 if [ ! -d ./SUNWGtku ]; then
    GTKPKG=0
 else
    GTKPKG=1
 fi;
fi;

if  [ "$JREPKG" == "1" ] && [ "$GTKPKG" == "1" ]; then
    PKG_LIST="SUNWj3rt SUNWgcmn SUNWGlib SUNWGtkr SUNWGtku SUNWnsxp SUNWnsm SUNWnsb SUNWnspsm"
elif [ "$JREPKG" == "0" ] && [ "$GTKPKG" == "1" ]; then
    PKG_LIST="SUNWgcmn SUNWGlib SUNWGtkr SUNWGtku SUNWnsxp SUNWnsm SUNWnsb SUNWnspsm"
  elif [ "$JREPKG" == "1" ] && [ "$GTKPKG" == "0" ]; then
    PKG_LIST="SUNWj3rt SUNWnsxp SUNWnsm SUNWnsb SUNWnspsm"
    else
    PKG_LIST="SUNWnsxp SUNWnsm SUNWnsb SUNWnspsm"
fi;


#
# 4442354 allow user to run the script from any directory.
# to allow minimum change, perform cd $PROGRAMDIR in a transparent manner. 
#

PROGRAMDIR=`dirname $0`
cd $PROGRAMDIR

#
# Check if we are installing packages of the same arch type as we 
# are running on.
#
PKG_ARCH=`/bin/head -1 SUNWnsb/pkginfo`
MACH_ARCH=`/bin/uname -p`
MACH_ARCH="ARCH="$MACH_ARCH

if [ $PKG_ARCH != $MACH_ARCH ]
then
	echo "The packages you are trying to install are of the wrong arch type."
	exit 1
fi



#
# Get install location - default is /opt
#
gotdir=0
while [ $gotdir = 0 ]; do
	echo ""
	echo "Please enter the full path to the directory in which you would"
	if [ $TEST9 = 0 ]; then
		/usr/bin/echo "like to install Netscape [/opt]: \c"
	else
		/usr/bin/echo "like to install Netscape [/usr]: \c"
	fi;
	read installdir
	if [ "$installdir" = "" ]; then
		if [ $TEST9 = 0 ]; then	
			installdir=/opt
		else
			installdir=/usr
		fi;
	fi;

	if [ ! -d $installdir ]; then
		/usr/bin/echo $installdir" does not exist.  Create? [y]: \c"
		read answer
		case $answer in
		    [yY] | [yY][eE][sS] | "")
			mkdir -p $installdir
			if [ $? = 0 ]; then
				gotdir=1
			else
				echo "Could not create $installdir, please retry."
			fi;
			;;
		esac
	else
		gotdir=1
	fi;
done;
	

#
# See if a prior version of Netscape is installed
#
NSINSTALLED=0

echo ""
echo "Checking for previously installed packages:"
/bin/pkginfo | grep SUNWns
FOUNDPKG=$?
if [ $FOUNDPKG -eq 0 ]; then
 NSINSTALLED=1   
fi

if [ "$GTKPKG" == "1" ]; then
 /bin/pkginfo | grep SUNWgcmn
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  NSINSTALLED=1   
 fi
 
 /bin/pkginfo | grep SUNWGlib
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  NSINSTALLED=1   
 fi

 /bin/pkginfo | grep SUNWGtkr
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  NSINSTALLED=1   
 fi

 /bin/pkginfo | grep SUNWGtku
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  NSINSTALLED=1   
 fi
fi

if [ "$JREPKG" == "1" ]; then
 /bin/pkginfo | grep SUNWj3rt
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  NSINSTALLED=1   
 fi
fi

if [ $NSINSTALLED -eq 1 ]; then
 echo ""
 echo "*** Previously installed packages will be removed. ***"
fi
#
# Check for required disk space.
#
if [ $NSINSTALLED = 0 ]; then
	if [ `df -k $installdir | sed '1d' | awk '{ print $4 }'` -lt 160000 ]
	then
		echo "You must have at least 160 Mb free in "$installdir
		exit
	fi
fi;

#
# Make sure the needed patches are installed
#
./checkpatches.sh
if [ $? = 1 ]; then
	echo ""
	/usr/bin/echo "Do you want to continue with the install? [y] \c"
	read response
	case $response in
	  [nN][oO] | [nN] )
		exit;;
	esac	
fi;


#
# Check if proper JRE exist
# first check pkg; then check default location
# if cannot find, do exit
#

CURR_PWD=`pwd`
JREPATH=`./checkJRE.ksh $CURR_PWD`

if [ "$JREPATH" == "" ]; then

    /usr/bin/echo "The system does not appear to have proper jre, $JREVERSION. Please abort the installation and install jre first. To down load jre, please go to http://java.sun.com."
    /usr/bin/echo "If you have installed jre, please refer to README for future information."
   /usr/bin/echo "Do you like to continue the instation? [n]"
    read response
    case $response in
    [yY] | [yY][eE][sS])
	echo ""
	;;
    [nN][oO] | [nN] | "" | *)
	/usr/bin/echo "installation abort..."
	exit;;
    esac

fi;

#
# See if the user wants to install any of the optional packages.
#
echo ""
echo "This script will install the default Netscape files and libraries."
echo "You may choose to install optional packages at this time."
echo ""

echo ""
/usr/bin/echo "Would you like to install the optional source packages? [n] \c" 
read response
case $response in
    [yY][eE][sS] | [yY])
	if [ "$GTKPKG" == "1" ]; then
	   PKG_LIST="$PKG_LIST SUNWnsMS SUNWGlibS SUNWGtkS" 
	else
	   PKG_LIST="$PKG_LIST SUNWnsMS" 
	fi ;;
esac

#
# Verify the PKG_LIST
#
echo ""
echo "Installing the following packages in "$installdir":"
echo "     "$PKG_LIST
/usr/bin/echo "Continue? [y] \c"
read response
case $response in
    [nN][oO] | [nN])
	exit;;
esac

umask 077
mkdir -p /tmp/instdir.$$
if [ $? != 0 ]; then
        echo "Error cannot create directory"
        exit 1
fi


#
# define a administartion file for pkgadd
#
cat >/tmp/instdir.$$/admin.add.$$ <<EOF
basedir=$installdir
mail=
runlevel=quit
conflict=nocheck
setuid=nocheck
action=nocheck
partial=nocheck
instance=unique
idepend=nocheck
rdepend=nocheck
space=quit
EOF

#
# define a administartion file for pkgrm
#
cat >/tmp/instdir.$$/admin.rm.$$ <<EOF
basedir=default
mail=
runlevel=quit
conflict=nocheck
setuid=nocheck
action=nocheck
partial=nocheck
instance=unique
idepend=nocheck
rdepend=nocheck
space=quit
EOF


#
# define a administartion file for install packages to the pkginfo default 
# location 
#
cat >/tmp/instdir.$$/admin.pkginfo.$$ <<EOF
basedir=default
mail=
runlevel=quit
conflict=nocheck
setuid=nocheck
action=nocheck
partial=nocheck
instance=unique
idepend=nocheck
rdepend=nocheck
space=quit
EOF


#
# Check for previously installed Netscape
#
if [ $NSINSTALLED -eq 1 ]; then
	echo ""
	echo "Removing old version of Netscape..."
	
	# Note: The NS_PKGS list included the older SUNWns* package since
	# the user could have older packages.
	# Note: SUNWj3rt is removed by seperate segement of code.
	 
	if  [ "$JREPKG" == "1" ] && [ "$GTKPKG" == "1" ]; then
	   NS_PKGS="SUNWGlibS SUNWGtkS SUNWns6MS SUNWnsMS SUNWns6ls SUNWns6bw SUNWns6pl SUNWns6tb SUNWns6jp SUNWns6 SUNWj3rt SUNWns6p SUNWnspsm SUNWns6b SUNWnsb SUNWns6m SUNWnsm SUNWns6xp SUNWnsxp SUNWns6l SUNWGtku SUNWGtkr SUNWGlib SUNWgcmn"
	elif [ "$JREPKG" == "0" ] && [ "$GTKPKG" == "1" ]; then
	   NS_PKGS="SUNWGlibS SUNWGtkS SUNWns6MS SUNWnsMS SUNWns6ls SUNWns6bw SUNWns6pl SUNWns6tb SUNWns6jp SUNWns6 SUNWns6p SUNWnspsm SUNWns6b SUNWnsb SUNWns6m SUNWnsm SUNWns6xp SUNWnsxp SUNWns6l SUNWGtku SUNWGtkr SUNWGlib SUNWgcmn"
	  elif [ "$JREPKG" == "1" ] && [ "$GTKPKG" == "0" ]; then
	   NS_PKGS="SUNWns6MS SUNWnsMS SUNWns6ls SUNWns6bw SUNWns6pl SUNWns6tb SUNWns6jp SUNWns6 SUNWj3rt SUNWns6p SUNWnspsm SUNWns6b SUNWnsb SUNWns6m SUNWnsm SUNWns6xp SUNWnsxp SUNWns6l"
             else
	       NS_PKGS="SUNWns6MS SUNWnsMS SUNWns6ls SUNWns6bw SUNWns6pl SUNWns6tb SUNWns6jp SUNWns6 SUNWns6p SUNWnspsm SUNWns6b SUNWnsb SUNWns6m SUNWnsm SUNWns6xp SUNWnsxp SUNWns6l"
	fi;


	for pkg in $NS_PKGS
	do
		/bin/pkginfo -q ${pkg}.\*
		FOUNDPKG=$?
		if [ $FOUNDPKG -eq 0 ]; then
#			echo "Removing old ${pkg}"
			/usr/sbin/pkgrm -n -a /tmp/instdir.$$/admin.rm.$$ ${pkg}.\* 2>&1
		fi;
	done
fi

echo ""
echo "Installing Netscape..."

for pkg in $PKG_LIST
do
case $pkg in
    SUNWj3rt)
	if [ -d ./SUNWj3rt ]; then
	    echo "Adding ${pkg}"
	    /usr/sbin/pkgadd -n -a /tmp/instdir.$$/admin.pkginfo.$$ -d `pwd` $pkg 2>&1
	fi;
        ;;
    SUNWgcmn | SUNWGlib | SUNWGtkr | SUNWGtku)
	if [ -d ./${pkg} ]; then
	    echo "Adding ${pkg}"
	    /usr/sbin/pkgadd -n -a /tmp/instdir.$$/admin.pkginfo.$$ -d `pwd` $pkg 2>&1
	fi;
        ;;
    SUNWGlibS | SUNWGtkS)
	if [ -d ./${pkg} ]; then
	    echo "Adding ${pkg}"
	    /usr/sbin/pkgadd -n -a /tmp/instdir.$$/admin.pkginfo.$$ -d `pwd` $pkg 2>&1
	fi;
        ;;
    *)
	echo "Adding ${pkg}"
	/usr/sbin/pkgadd -n -a /tmp/instdir.$$/admin.add.$$ -d `pwd` $pkg 2>&1
	;;
    esac
done

/bin/rm -rf /tmp/instdir.$$

echo "Netscape installation complete"
