#!/bin/ksh
#
# Copyright 2003 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# mozinstall
#
# Simple script to install Mozilla packages for S8.
#
# This script should be run as root, from the 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 and this version is newer than the 
# version currently installed on the system,  set JREPKG flag to 1 and add SUNWj3rt
# into the list (note: SUNWj3rt has to be installed before SUNWmoznav since
# SUNWmoznav's post installation script will create libjavaoji.so link to it).
#

if [ ! -d ./SUNWj3rt ]; then
    JREPKG=0
else
    NEWJREVER=`cat ./SUNWj3rt/pkginfo | grep VERSION | sed s/"VERSION="//`
    INSJREVER=`/bin/pkginfo -l SUNWj3rt | grep VERSION | nawk '{print $2}'`
    if [ $INSJREVER == $NEWJREVER ]; then
      JREPKG=0
    else
      /usr/bin/echo ""
      /usr/bin/echo "Different JRE versions detected."
      /usr/bin/echo "    System Installed version is: $INSJREVER"
      /usr/bin/echo "    Version provided is: $NEWJREVER"
      /usr/bin/echo "Do you want to removed Installed version and replace with version provided? [n]: \c"
                read answer
                case $answer in
                    [yY] | [yY][eE][sS] )
                        JREPKG=1
                        ;;
		    *)
			JREPKG=0
			;;
                esac
    fi
fi;

#
# if SUNWgnome-base-libs directory exist, set GTKPKG flag to 1 and add SUNWgnome-base-libs
# SUNWgnome-base-libs-root SUNWgnome-base-libs-share SUNWlibpopt SUNWlibpopt-share
# SUNWpng SUNWTiff SUNWjpg to the list 
#

if [ ! -d ./SUNWgnome-base-libs ]; then
    GTKPKG=0
else
    /bin/pkginfo -q SUNWgnome-base-libs
    FOUNDPKG=$?
    if [ $FOUNDPKG -eq 0 ]; then
      GTKPKG=0
    else
      GTKPKG=1
    fi
echo "GTKPKG=$GTKPKG"
fi;


if  [ "$JREPKG" == "1" ] && [ "$GTKPKG" == "1" ]; then
    PKG_LIST="SUNWj3rt SUNWgnome-base-libs-root SUNWgnome-base-libs-share SUNWlibpopt-share SUNWlibpopt SUNWpng SUNWjpg SUNWTiff SUNWgnome-base-libs SUNWmoznav SUNWmozmail SUNWmozchat SUNWmozgm"
elif [ "$JREPKG" == "0" ] && [ "$GTKPKG" == "1" ]; then
    PKG_LIST="SUNWgnome-base-libs-root SUNWgnome-base-libs-share SUNWlibpopt-share SUNWlibpopt SUNWpng SUNWjpg SUNWTiff SUNWgnome-base-libs SUNWmoznav SUNWmozmail SUNWmozchat SUNWmozgm"
  elif [ "$JREPKG" == "1" ] && [ "$GTKPKG" == "0" ]; then
    PKG_LIST="SUNWj3rt SUNWmoznav SUNWmozmail SUNWmozchat SUNWmozgm"
    else
    PKG_LIST="SUNWmoznav SUNWmozmail SUNWmozchat SUNWmozgm"
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 SUNWmoznav/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 /usr
#
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 Mozilla for Sun [/usr]: \c"
	else
		/usr/bin/echo "like to install Mozilla for Sun [/usr]: \c"
	fi;
	read installdir
	if [ "$installdir" = "" ]; then
		if [ $TEST9 = 0 ]; then	
			installdir=/usr
		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 Mozilla for Sun is installed
#
MOZINSTALLED=0

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

if [ "$GTKPKG" == "1" ]; then
 /bin/pkginfo | grep SUNWgnome-base-libs
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  MOZINSTALLED=1   
 fi
 
 /bin/pkginfo | grep SUNWgnome-base-libs-root
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  MOZINSTALLED=1   
 fi

 /bin/pkginfo | grep SUNWgnome-base-libs-share
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  MOZINSTALLED=1   
 fi

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

 /bin/pkginfo | grep SUNWlibpopt-share
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  MOZINSTALLED=1   
 fi

 /bin/pkginfo | grep SUNWpng | grep -v SUNWpngx
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  MOZINSTALLED=1   
 fi

 /bin/pkginfo | grep SUNWTiff | grep -v SUNWTiffx
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  MOZINSTALLED=1   
 fi

 /bin/pkginfo | grep SUNWjpg | grep -v SUNWjpgx
 FOUNDPKG=$?
 if [ $FOUNDPKG -eq 0 ]; then
  MOZINSTALLED=1   
 fi
fi

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

if [ $MOZINSTALLED -eq 1 ]; then
 echo ""
 echo "*** Previously installed packages will be removed. ***"
fi
#
# Check for required disk space.
#
if [ $MOZINSTALLED = 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 Mozilla for Sun 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 Gnome Integration package? [n] \c"
#read response
#case $response in
#    [yY][eE][sS] | [yY])
#         PKG_LIST="$PKG_LIST SUNWmozgm" ;;
#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 Mozilla for Sun
#
if [ $MOZINSTALLED -eq 1 ]; then
	echo ""
	echo "Removing old version of Mozilla for Sun..."
	
	if  [ "$JREPKG" == "1" ] && [ "$GTKPKG" == "1" ]; then
	   MOZ_PKGS="SUNWgnome-base-libs SUNWgnome-base-libs-root SUNWgnome-base-libs-share SUNWlibpopt SUNWlibpopt-share SUNWpng SUNWTiff SUNWjpg SUNWj3rt SUNWmozS SUNWmoz SUNWmozgm SUNWmozchat SUNWmozmail SUNWmoznav"
	elif [ "$JREPKG" == "0" ] && [ "$GTKPKG" == "1" ]; then
	   MOZ_PKGS="SUNWgnome-base-libs SUNWgnome-base-libs-root SUNWgnome-base-libs-share SUNWlibpopt SUNWlibpopt-share SUNWpng SUNWTiff SUNWjpg SUNWmozS SUNWmoz SUNWmozgm SUNWmozchat SUNWmozmail SUNWmoznav"
	  elif [ "$JREPKG" == "1" ] && [ "$GTKPKG" == "0" ]; then
	   MOZ_PKGS="SUNWj3rt SUNWmozS SUNWmoz SUNWmozgm SUNWmozchat SUNWmozmail SUNWmoznav"
             else
	       MOZ_PKGS="SUNWmozS SUNWmoz SUNWmozgm SUNWmozchat SUNWmozmail SUNWmoznav"
	fi;


	for pkg in $MOZ_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 Mozilla for Sun..."

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;
        ;;
    SUNWgnome-base-libs | SUNWgnome-base-libs-root | SUNWgnome-base-libs-share | SUNWlibpopt | SUNWlibpopt-share | SUNWpng | SUNWTiff | SUNWjpg)
	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;
        ;;
    SUNWmozgm)
	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 "Mozilla for Sun installation complete"
