#!/bin/sh
#################################################################################
#										#
#	NETSCAPE Platform-Independent Software Installation			#
#	Copyright (c) 1997,1998,1999 by Netscape Communications Corporation	#
#										#
#	File name:	ns-install						#
#										#
#	Revision:	1.50							#
#										#
#	Description:	This script installs Netscape software on a		#
#			workstation and creates or updates the Version		#
#			Registry to allow for the future use of Automatic	#
#			Software Download (ASD).				#
#										#
#	Usage:		To execute the script, type:				#
#										#
#				./ns-install [ -o ] [ -v ]			#
#										#
#			Where -v turns on the verbose flag for the		#
#			interesting tar commands.  The -o flag is not		#
#			supported on all versions of tar, so use it with	#
#			caution.						#
#										#
#	Exit codes:	0	Successful completion				#
#			1	Package file is missing				#
#			2	Can't create the target directory		#
#			3	Unable to register the product			#
#										#
#	Author(s):	Brian Ostrom (briano@netscape.com)			#
#										#
#	Notes:		* Will use MOZILLA_HOME as the default if it is set.	#
#										#
#################################################################################
#
PACKAGE="netscape-v47.nif"
PRODUCT="Communicator"
RELEASE="4.7"
BLDVERS="4.7.0.19990915"
#
TESTFILE="testfile.ns.$$"
REG_INFILE="nsreg.$$"
VREGISTRY="registry"
VR_PRODSTR="${PRODUCT}"
#
#
# Process any arguments.  Let users see verbose output from tar (does
# anyone even care?), and (for _some_ versions of tar) override the file
# ownership when installing as root, if they wish.
#
TAR_OPTIONS=""
while test $# -ne 0
do
	case $1 in
		-o)
			TAR_OPTIONS="${TAR_OPTIONS}o"
			;;
		-v)
			TAR_OPTIONS="${TAR_OPTIONS}v"
			;;
		*)
			;;
	esac
	shift
done
#
echo ""
echo "=================================================================="
echo ""
echo "       NETSCAPE Platform-Independent Software Installation"
echo "                 For ${PRODUCT} release ${RELEASE}"
echo "     Copyright (c) 1997-1999 by Netscape Communications Corp."
echo ""
echo "=================================================================="
echo ""
echo ""
#
#
# Make sure the package we expect is where we expect it.
#
if test ! -f ${PACKAGE}
then
	echo "ERROR: ${PACKAGE}: no such file"
	exit 1
fi
#
#
# If MOZILLA_HOME is set, assume that is the place to install.
# Otherwise, make an educated guess at the "right" default
# directory.
#
if test ! -z "${MOZILLA_HOME}"
then
	INSTALL_DIR="${MOZILLA_HOME}"
else
	if test -d /opt
	then
		INSTALL_DIR="/opt/netscape"
	else
		INSTALL_DIR="/usr/local/netscape"
	fi
fi
#
#
# See if we have to use the lame SYS-V echo command flags.
#
if test "`/bin/echo 'blah\c'`" = "blah\c"
then
	EFLAG="-n"
	ENDER=""
else
	EFLAG=""
	ENDER="\c"
fi
ECHO="/bin/echo ${EFLAG}"
#
#
# Even though the user has already used gzip to extract the file (or
# we wouldn't be here), make sure we can find it.
#
GZIP="gzip"
flag=1
while test ${flag} -eq 1
do
	${GZIP} -h >/dev/null 2>&1
	if test $? -ne 0
	then
		echo ""
		echo "Unable to locate 'gzip' in your path.  Please provide the fully-qualified"
		echo "directory in which it can be found."
		echo ""
		${ECHO} "Directory containing the 'gzip' binary: ${ENDER}"
		read GZIP_DIR
		GZIP="${GZIP_DIR}/gzip"
	else
		flag=0
	fi
done
#
echo ""
echo "Please specify the directory path under which the software will be"
echo "installed.  The default directory is ${INSTALL_DIR}, but you may"
echo "install anywhere you wish (if you have permission to do so)."
echo ""
flag=1
while test ${flag} -eq 1
do
	${ECHO} "Location for ${PRODUCT} software [${INSTALL_DIR}]: ${ENDER}"
	read TARGET
	if test -z "${TARGET}"
	then
		TARGET="${INSTALL_DIR}"
	fi
	if test ! -z "`echo ${TARGET} | grep '~'`"
	then
		TARGET=`echo ${TARGET} | sed "s:~:${HOME}:"`
	fi
#
	flag=0
	if test ! -d ${TARGET}
	then
		echo ""
		${ECHO} "${TARGET}: No such directory.  Do you wish to create it? (y/n)[y] ${ENDER}"
		read check
		if test "${check}" = "n" -o "${check}" = "N"
		then
			flag=1
		else
			#
			# The -p flag may cause an error on some HP-UX systems.
			# Removing the -p will work for 'basename TARGET' as long
			# as 'dirname TARGET' exists.
			#
			mkdir -p ${TARGET}
			if test $? -ne 0
			then
				echo "ERROR: Problem creating ${TARGET}"
				exit 2
			fi
		fi
	else
		echo ""
		echo "Existing '${TARGET}' directory found."
		echo ""
		echo "The existing contents may be modified or replaced if you install in"
		echo "this directory.  If you choose not to install in '${TARGET}',"
		echo "you will be prompted for a different directory."
		echo ""
		${ECHO} "Do you wish to install in '${TARGET}'? (y/n)[y] ${ENDER}"
		read check
		if test "${check}" = "n" -o "${check}" = "N"
		then
			flag=1
		fi
	fi
#
	if test ${flag} -eq 0
	then
		(echo "Write test." 2>/dev/null > ${TARGET}/${TESTFILE}) 2>/dev/null
		if test $? -ne 0
		then
			echo ""
			echo "You do not have write permission in ${TARGET}."
			echo "Change the permissions, or select another directory, and try again."
			echo ""
			flag=1
		else
			/bin/rm -f ${TARGET}/${TESTFILE}
		fi
	fi
done
#
#
# Begin the installation.
#
echo ""
echo ""
echo "Installing ${PRODUCT} files..."
#
#
# Make a list of the files in this package, so we can check for
# existing files with the same names and save them.
#
FILELIST="`${GZIP} -dc ${PACKAGE} | tar -tvf - | awk '{print $NF}'`"
#
#
# Keep a log of this installation for reference.
#
LOGFILE="ns-install.log-$$"
date > ${LOGFILE}
echo "Files installed:" >> ${LOGFILE}
echo "================" >> ${LOGFILE}
#
#
# Check for existing files and move them out of the way.
#
if test -d ${TARGET}/java/classes
then
	echo " ==> moving existing '${TARGET}/java/classes' to '${TARGET}/java/classes.old'"; \
	/bin/rm -rf ${TARGET}/java/classes.old
	mv ${TARGET}/java/classes ${TARGET}/java/classes.old
fi
for i in ${FILELIST}
do
	echo "${i}" >> ${LOGFILE}
	if test ! -d ${TARGET}/${i}
	then
		(cd ${TARGET}; \
		 /bin/rm -f ${i}.old; \
		 if test -f ${i}; then \
			echo " ==> moving existing '${TARGET}/${i}' to '${TARGET}/${i}.old'"; \
			mv ${i} ${i}.old; \
		 fi)
	fi
done
#
#
# Actually install the package in TARGET.
#
${GZIP} -dc ${PACKAGE} | (cd ${TARGET}; tar -x${TAR_OPTIONS}f -)
#
/bin/rm -f ${REG_INFILE}
echo "${VR_PRODSTR},${BLDVERS},${TARGET}" > ${REG_INFILE}
#
#
# Install the Java class ARchive (JAR) files.
#
if test "${PRODUCT}" = "Communicator" -o "${PRODUCT}" = "Navigator"
then
	echo ""
	echo "Installing ${PRODUCT} Java files..."
	if test ! -d ${TARGET}/java/classes
	then
		(cd ${TARGET}; mkdir -p java/classes)
	fi
	for i in *.jar
	do
		if test -f ${TARGET}/java/classes/${i}
		then
			echo " ==> moving existing '${TARGET}/java/classes/${i}' to '${TARGET}/java/classes/${i}.old'"
			/bin/rm -f ${TARGET}/java/classes/${i}.old
			mv ${TARGET}/java/classes/${i} ${TARGET}/java/classes/${i}.old
		fi
		tar -cf - ${i} | (cd ${TARGET}/java/classes; tar -xf -)
		echo "${i}" >> ${LOGFILE}
		/bin/rm -f ${TARGET}/${i} ./${i}
		echo "${i},${BLDVERS},${TARGET}/java/classes" >> ${REG_INFILE}
	done
#
	#
	# Try to properly deal with the font.properties files, to hopefully
	# avoid users having to set MOZILLA_JAVA_FONT_PROPERTY_PATH, or
	# just assuming Java font-handling is broken.
	#
	if test ! -z "`ls /bin/uname`"
	then
		MOVE_FONTS="TRUE"
		case `/bin/uname` in
			AIX)
				PLATFORM="aix"
				;;
			HP-UX)
				PLATFORM="hpux"
				;;
			IRIX | IRIX64)
				PLATFORM="irix"
				;;
			*)
				MOVE_FONTS="FALSE"
				;;
		esac
		if test "${MOVE_FONTS}" = "TRUE"
		then
			mkdir ${TARGET}/java/classes/sun
			chmod 755 ${TARGET}/java/classes/sun
			mv ${TARGET}/java/classes/font.prop* ${TARGET}/java/classes/sun
			mv ${TARGET}/java/classes/${PLATFORM}/* ${TARGET}/java/classes/.
		fi
	fi
#
	#
	# Is this a virgin install?  If so, create the .netscape dir
	# and put the default bookmark file in it.
	#
	if test ! -d ${HOME}/.netscape
	then
		# New install, or upgrade from 0.x or 1.x.
		mkdir ${HOME}/.netscape
		cp ${TARGET}/bookmark.htm ${HOME}/.netscape/bookmarks.html
		chmod 644 ${HOME}/.netscape/bookmarks.html
		echo "${HOME}/.netscape/bookmarks.html" >> ${LOGFILE}
	fi
fi
#
echo ""
echo "Installing additional component files..."
for pkg in *.nif
do
	if test "${pkg}" = "${PACKAGE}"
	then
		continue
	fi
	#
	# Don't need to save the old files for these components,
	# since the new ones are backwards-compatible.
	#
	case ${pkg} in
		nethelp*)
			/bin/rm -rf ${TARGET}/nethelp
			echo "nethelp/" >> ${LOGFILE}
			;;
		spellchk*)
			/bin/rm -rf ${TARGET}/spell
			echo "spell/" >> ${LOGFILE}
			;;
	esac
	${GZIP} -dc ${pkg} | (cd ${TARGET}; tar -x${TAR_OPTIONS}f -)
	echo "${pkg},${BLDVERS},${TARGET}" >> ${REG_INFILE}
	/bin/rm -f ${pkg}
done
#
#
# Make sure libjsd.so can be found.
#
if test -f ${TARGET}/libjsd.so
then
	if test ! -d ${TARGET}/lib
	then
		mkdir ${TARGET}/lib
	fi
	ln -s ../libjsd.so ${TARGET}/lib/libjsd.so
fi
#
#
# Install the vreg binary in the TARGET directory.
#
if test -f ${TARGET}/vreg
then
	/bin/rm -f ${TARGET}/vreg.old
	mv ${TARGET}/vreg ${TARGET}/vreg.old
	# Handle the case where TARGET == . (meaning we just moved the file we just installed).
	if test ! -f ./vreg
	then
		mv ${TARGET}/vreg.old ${TARGET}/vreg
	else
		mv ./vreg ${TARGET}/vreg
	fi
else
	mv ./vreg ${TARGET}/vreg
fi
#
#
# Register the product so Automatic Software Download can work.
# Just keeps track of the product name and version number for
# comparison purposes later when newer versions become available.
# Not necessary to run the product, but ASD will not be possible
# without it....
#
echo ""
echo "Registering ${PRODUCT} ${RELEASE}..."
${TARGET}/vreg ${TARGET}/${VREGISTRY} ${REG_INFILE} >/dev/null 2>&1
if test $? -ne 0
then
	echo "ERROR: Installation completed, but unable to update/create the product"
	echo "       version registry."
	echo ""
	echo "       If you are installing ${PRODUCT} from a platform other than"
	echo "       that for which this package is intended, 'vreg' will not be"
	echo "       able to run.  If this is the case, you may run 'vreg' manually"
	echo "       (to enable ASD) on the appropriate platform using the following"
	echo "       commands:"
	echo ""
	awk '{printf("\techo \"%s\" >>/tmp/infile\n", $0)}' ${REG_INFILE}
	echo "	${TARGET}/vreg ${TARGET}/${VREGISTRY} /tmp/infile"
	echo ""
	/bin/rm -f ns-install ${PACKAGE} ${REG_INFILE} core
	exit 3
fi
#
echo ""
echo ""
echo "The Netscape ${PRODUCT} software installation is complete."
echo ""
echo ""
#
#
# Get rid of the trash.
#
/bin/rm -f ns-install ${PACKAGE} ${REG_INFILE}
#
echo ""
exit 0
