#!/bin/sh
#################################################################################
#										#
#	NETSCAPE Conference Platform-Independent Software Installation		#
#	Copyright (c) 1997 by Netscape Communications Corporation		#
#										#
#	File name:	nsconf-install						#
#										#
#	Version:	0.1						        #
#										#
#	Description:	This script installs Netscape Conference on a Unix      #
#			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:				#
#										#
#				./nsconf-install [ -v ]				#
#										#
#	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)      		#
#                       Sriram Kini (kini@netscape.com)                         #
#										#
#	Notes:		* Will use MOZILLA_HOME as the default if it is set.	#
#                         If not, currently queries user for dogbert location.  #
#										#
#################################################################################
#
PACKAGE="nsconf-execs.tar.gz"
PRODUCT="Conference"
RELEASE="1.0"
BLDVERS="1.0.0.97162"

TESTFILE="testfile.ns.$$"

echo ""
echo "=================================================================="
echo ""
echo "       NETSCAPE CONFERENCE Platform-Independent Software Installation"
echo "                 For ${PRODUCT} release ${RELEASE}"
echo "       Copyright (c) 1997 by Netscape Communications Corp."
echo ""
echo "=================================================================="
echo ""
echo ""

#
# 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}"

if test -z "${MOZILLA_HOME}"
then
  echo "MOZILLA_HOME not set."
  echo "We strongly reccomend that you set the environment variable "
  echo "MOZILLA_HOME to the directory in which you installed Communicator."
  echo "Do you wish to continue installation?(y/n)[n]"$ENDER

  read choice
  if test "${choice}" = "y" -o "${choice}" = "Y"
  then
    echo "Continuing installation....."
  else
    echo "Please set your MOZILLA_HOME and try again."
    exit 1
  fi
fi

#
# First find out what our architecture is
#
case `uname` in
   SunOS )
         vers=`uname -r`
         if [ `echo "$vers" | cut -f1 -d.` -ge 5 ]; then
               mtag=sol24
         else
            mtag=sunos
         fi 
         ;;

   HP-UX )
	if [ `uname -r | cut -f2 -d.` -ge 10 ]; then
      	   mtag=hpux10;
    	else
      	   mtag=hpux;
    	fi 
        ;;

   AIX ) mtag=aix ;;

   OSF1 ) mtag=axp ;;

   IRIX ) mtag=irix ;;

   * ) 
         echo "**************************************************" >&2 ; 
         echo "* Conference Install Error:                      *" >&2 ; 
         echo "*                                                *" >&2 ; 
         echo "* Unable to determine machine architecture.      *" >&2 ; 
         echo "* This architecture is probably not supported.   *" >&2 ; 
         echo "**************************************************" >&2 ; 
         exit 1 ;;
esac

#
# Do not set tar option verbose, unless -v flag is specified.
#
case $1 in
	-v)
		v="v"
		;;

	*)
		v=""
		;;
esac


#
# Make sure the packages  we expect are 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.
#
if test ! -z "${MOZILLA_HOME}"
then
	INSTALL_DIR="${MOZILLA_HOME}"/nsconf
        NETSCAPE_DIR="${MOZILLA_HOME}"
else
	INSTALL_DIR="/usr/local/netscape/nsconf"
	NETSCAPE_DIR=/usr/local/netscape
fi


#
# 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}."
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
	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
			mkdir ${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 continue with the installation 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 "$0: 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 (this will take a few minutes)..."

#
# 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="nsconf-install.log-$$"
date > ${LOGFILE}
echo "Files installed:" >> ${LOGFILE}
echo "================" >> ${LOGFILE}

#
# Check for existing files and move them out of the way.
#
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${v}f -)

#
# Copy the liveaudio plugin into it's proper place. If $MOZILLA_HOME
# is defined, then it gets put under $MOZILLA_HOME/plugins; else it
# prompts the user for a location.
#
if test -z "${MOZILLA_HOME}"
then
  if test -z "${NPX_PLUGIN_PATH}"
  then
    PLUGIN_INSTALL=$HOME/.netscape/plugins    
  else
    PLUGIN_INSTALL=`echo $NPX_PLUGIN_PATH | cut -f 1 -d ':'` 
  fi

  echo "Installing LiveAudio plugin...."
  echo "Please specify the directory path in which to install the LiveAudio"
  echo "plugin.  The default directory is ${PLUGIN_INSTALL}."
  echo ""
  flag=1
  while test ${flag} -eq 1
  do
    ${ECHO} "Location for LiveAudio [${PLUGIN_INSTALL}]: ${ENDER}"
    read PLUGIN_TARGET
    if test -z "${PLUGIN_TARGET}"
    then
      PLUGIN_TARGET="${PLUGIN_INSTALL}"
    fi
    flag=0
    if test ! -d ${PLUGIN_TARGET}
    then
      echo ""
      ${ECHO} "${PLUGIN_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
	mkdir ${PLUGIN_TARGET}
	if test $? -ne 0
	then
	  echo "ERROR: Problem creating ${PLUGIN_TARGET}"
	  exit 2
	fi
      fi
     else
       echo ""
       echo "Existing '${PLUGIN_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 '${PLUGIN_TARGET}',"
       echo "you will be prompted for a different directory."
       echo ""
       ${ECHO} "Do you wish to continue with the installation in '${PLUGIN_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 "$0: Write test." 2>/dev/null > ${PLUGIN_TARGET}/${TESTFILE}) 2>/dev/null
       if test $? -ne 0
       then
         echo ""
	 echo "You do not have write permission in ${PLUGIN_TARGET}."
	 echo "Change the permissions, or select another directory, and try again."
	 echo ""
	 flag=1
       else
         /bin/rm -f ${PLUGIN_TARGET}/${TESTFILE}
       fi
     fi
  done

  #
  # Generate a livemedia.ini file that spells out where the software is
  #
  if [ ! -d $HOME/.netscape ]
  then
    mkdir $HOME/.netscape
  fi

  echo "[$mtag]" > $HOME/.netscape/livemedia.ini
  echo "Home = $TARGET" >> $HOME/.netscape/livemedia.ini
else
  if [ ! -d $MOZILLA_HOME/plugins ]
  then
    mkdir $MOZILLA_HOME/plugins
  fi
  PLUGIN_TARGET=$MOZILLA_HOME/plugins
fi  

if [ -f $PLUGIN_TARGET/LiveAudio.sl ]
then
  echo "Found existing plugin file $PLUGIN_TARGET/LiveAudio.sl"
  mv $PLUGIN_TARGET/LiveAudio.sl $PLUGIN_TARGET/LiveAudio.sl.old
fi
  
ln -s $TARGET/plugins/liveaudio/*/LiveAudio.sl $PLUGIN_TARGET

if [ -f $PLUGIN_TARGET/SoundPlayer.class ]
then
  echo "Found existing plugin file $PLUGIN_TARGET/SoundPlayer.class"
  mv $PLUGIN_TARGET/SoundPlayer.class $PLUGIN_TARGET/SoundPlayer.class.old
fi
ln -s $TARGET/plugins/liveaudio/SoundPlayer.class $PLUGIN_TARGET


#
# Make it so that the user can run Conference if MOZILLA_HOME is in the PATH
#
if test ! -z "${MOZILLA_HOME}"
then
  if [ -d  $MOZILLA_HOME/nsconference ]
  then
    echo "Moving $MOZILLA_HOME/nsconference to nsconference.old"
    mv $MOZILLA_HOME/nsconference $MOZILLA_HOME/nsconference.old
  fi

  echo '#!/bin/sh' >$TARGET/nsconf.parent
  echo "cd $TARGET; exec ./nsconference >/dev/null 2>&1" >>$TARGET/nsconf.parent

  cp $TARGET/nsconf.parent $MOZILLA_HOME/nsconference
  chmod 755 $MOZILLA_HOME/nsconference
else
  echo "You must add $TARGET to your PATH in order for Communicator to be"
  echo "able to launch Netscape Conference."
fi

#
# Netscape Conference will not register itself since no updates are planned.
# Period.
#

echo ""
echo "The Netscape ${PRODUCT} software installation is complete."
echo "If you're running Communicator, restart it to launch Conference."
echo ""
echo ""

#
# Get rid of the trash.
#
/bin/rm -f $0 ${PACKAGE} 

echo ""
exit 0












