#!/bin/bash

#
# This is a script which will allow one to run various versions
# of netscape without having to install it in /usr/local/bin
# or what not.
#
#   syntax
#
#      run_netscape -v version [arglist which is passed to netscape]
#
# Author: Stephen Adler
# e-mail: stephen.adler@bnl.gov
# Date: July 20, 1996
#
#  added option so that one can pass parameters to netscape
#  Date: Sept 3rd, 1996
#
#  modified to be general enough to be installed as an rpm package
#  To change the version number, use the -v qualifier
#  run_netscape [-v version]
#
#  update: June 2nd, 1997
#  by: Steve Adler
#  comment: Updated to run with netscape 4.0b5. Removed links
#  to 4.0b2 or earlier 4.0bx versions. Got rid of the need for
#  the local-Netscape.... directory. Added code to save bookmark
#  file incase netscape crashes and deletes it.
#
#  update: June 14th, 1997
#  fixed two minor bugs with 4.0b5. If no bookmark file exists and/or
#  no policyMoz40p1.jar exists, then copy them into ~/.netscape
#  from $netscape_home/$nsv. Also, I'm not sure about how to handle
#  the ~/.netscape/registry file. It seems to be created no matter what.
#  I coded the wrapper such that only root should create this registry.
#  I'll leave the regestry code in for now.
#
#  update: August 10, 1997
#  Fixed bug found by Gordon Chamberlin, glac@visualizetech.com.
#  CLASSPATH was misspelled which forced resetting CLASSPATH
#  such that CLASSPATH was permanently changed after running this
#  wrapper. Changed the log file to .netscape.bla.bla from
#  netscape.bla.bal. Also, if someone mistypes the version number
#  a list of installed versions are printed to aid the user.
#  The two mods listed above were also suggested by Gordon.
#
#  update: Nov 11, 1997
#  Complaint by Brad Alexander <storm@michelob.wustl.edu> about not
#  being able to load plugins. This is do to my redefinition of
#  NPX_PLUGIN_PATH so as to avoid another bug caused by libnullplugin.so.
#  If communicator, (or navigator I guess), loads libnullplugin.so,
#  then communicator crashes on unresolved plugin types. My work
#  around was to screw around with NPX_PLUGIN_PATH. This disabled
#  the ability to load plugins. So, I've changed this a bit by
#  having the wrapper look for .netscape/plugins and also
#  by scanning the file ~/.nswraprc to load environment variables
#  which are used by the pluggins.
#
#  update; Nov 12, 1997
#  navigator/communicator 4.04 is out. I'm adding support for it
#  and dropping the installation of some of the beta versions
#  of netscape.
#
#  update: Nov 17, 1997
#  Fixed bug found by Levente Farkas, lfarkas@ohsh.u-szeged.hu.
#  CLASSPATH was not unset for navigator/communicator 4.04 and
#  java Applets not working if JDK was installed.
#
#  update: Nov 18th. Added support for 128 bit encription US version
#  of netscape software (communicator, navigator etc.) Fix provided
#  by adam@lynx.msc.cornell.edu. (The file which really needed fixing
#  was untar_netscape)
#
#  update: Dec 29th, 1997. Added support for RedHat 5.0, glibc.
#  This was needed for netscape 3.0x. Netscape 4.04 seems to work
#  just fine. Also, fixed bug reported by Larry Bauer
#  <larrq@eeyore.bus.ualberta.ca> regarding the way I picked up
#  the version of netscape. (ie. 3. -> ^3 etc.)
#

#
# Config symbols
#
# $netscape_home defines the root directory where netscape should
# live
# $def_nsv defines the lastest version of netscape


netscape_home=/usr/lib/X11/netscape
nsvlist='4.04 4.04n 4.03 4.03n 3.04 3.01 4.02b7'
flist='communicator-v404-{us,export}.x86-unknown-linux2.0.tar.gz navigator-v404-{us,export}.x86-unknown-linux2.0.tar.gz communicator-v403-{us,export}.x86-unknown-linux2.0.tar.gz navigator-v403-{us,export}.x86-unknown-linux2.0.tar.gz netscape-v304-{us,export}.x86-unknown-linux-elf.tar.gz netscape-v301-{us,export}.x86-unknown-linux-elf.tar.gz communicator-v402b7-{us,export}.x86-unknown-linux2.0.tar.gz'
def_nsv=none

let j=0
for i in $nsvlist; do
  let j=j+1
  ffound=$(echo $flist | cut -f$j -d' ')
  if [ -d /usr/lib/X11/netscape/$i ]; then
    def_nsv=$i
    break
  fi
done

#
# The following code searches for the -v option and builds
# a new argument list with the -v removed
#

let j=1
version=none
nargv=""
comlin="$*"
let version_arg=0
for i in $@; do
  let j=j+1
  if [ "$i" = "-v" ]; then
    if [ $j -gt $# ]; then
      echo syntax error
      echo -v version_number
      exit
    else
      version=$(echo $comlin | cut -f$j -d' ')
    fi
    version_arg=2
  fi
  if [ $version_arg -eq 0 ]; then
    nargv="$nargv $i"
  else
    let version_arg=version_arg-1
  fi
done

if [ "$version" = "none" ]; then
  nsv=$def_nsv
else
  nsv=$version
fi

#
# I test to see if the version requested exists
#

if [ ! -d $netscape_home/$nsv ]; then
  echo Sorry, your version of netscape, $nsv, does not exist or is
  echo not installed. The versions you currently have installed are
  \ls $netscape_home | sed 's/\///' | sed 's/bin//' | sed 's/lib//'
  echo Try again using one of the versions listed above.
  echo   example
  echo  '    ' netscape -v `\ls $netscape_home | sed 's/\///' | sed 's/bin//' | sed 's/lib//' | head -1`
  exit
fi

#
#  Executing some netscape 4.0b5 specific stuff
#
#

if [ $nsv = 4.0b5 ]; then

# Make .netscape if no there
    if [ ! -d ~/.netscape ]; then
      mkdir ~/.netscape
    fi

# copy the policy file over
    if [ ! -f ~/.netscape/policyMoz40P1.jar ]; then
      if [ -f $netscape_home/$nsv/policyMoz40P1.jar ]; then
        cp $netscape_home/$nsv/policyMoz40P1.jar ~/.netscape/policyMoz40P1.jar
      else
        echo
        echo ****** Warning Warning Warning *****
        echo
        echo Could not find file $netscape_home/$nsv/policyMoz40P1.jar
        echo This means that install of netscape wrapper was updated
        echo without the file communicator file
        echo $ffound
        echo in /tmp
        echo
        echo To fix this problem, use rpm to uninstall netscape-wrapper-bnl
        echo and then reinstall it with the communicator file
        echo $ffound
        echo in /tmp
        echo
        echo continuing to startup netscape.....
      fi
    fi

# get a default bookmark file
    if [ ! -f ~/.netscape/bookmarks.html ]; then
      cp $netscape_home/$nsv/bookmark.htm ~/.netscape/bookmarks.html
    fi

# fix up registry file if run as root
    if [ ! -f ~/.netscape/registry ] && [ $(id | grep root | wc -w) != 0 ]; then
      echo Communicator,4.0.0.97149,$netscape_home/$nsv > /tmp/infile
      $netscape_home/$nsv/vreg $netscape_home/$nsv/registry /tmp/infile
      cp $netscape_home/$nsv/registry ~/.netscape/registry
      rm /tmp/infile
    fi

fi

#
# Before starting netscape, I use the following text to
# make a backup copy of the book mark files. This text
# was provided by stig@hackvan.com
#

if [ -f ~/.netscape/bookmarks.html ]; then
  ( cd ~/.netscape
    cp bookmarks.html bookmarks.html.~`date +'%y%m%d'`.$$~
    ls -t bookmarks.html.~*~ | tail +10 | xargs rm -f xyzzy$$ )
elif [ $nsv != 3.01 ]; then
    if [ ! -d ~/.netscape ]; then
	mkdir ~/.netscape
    fi
    cp $netscape_home/$nsv/bookmark.htm ~/.netscape/bookmark.html
fi

#
# Next we scan in environment variables from ~/.nswraprc
#

if [ -f ~/.nswraprc ]; then
  . ~/.nswraprc
fi

#
# Now we startup netscape depending on the verion requested
#

if [ -z $CLASSPATH ]; then
  old="$CLASSPATH"
fi

if [ $(echo $nsv | grep ^3 | wc -l) = 1 ]; then
  export XENVIRONMENT=$netscape_home/$nsv/Netscape.ad
  export XKEYSYMDB=$netscape_home/$nsv/XKeysymDB
  export CLASSPATH=$netscape_home/$nsv/java_301:.
# if /usr/i486-linux-libc5 exists, this is a clue to
# having redhat 5.0 installed. (anyone know of a better
# way of doing this?
  if [ -d /usr/i486-linux-libc5 ]; then
    export LD_PRELOAD=$netscape_home/lib/gnumalloc.glibc.so
  else
    export LD_PRELOAD=$netscape_home/lib/gnumalloc.libc5.so
  fi
  $netscape_home/$nsv/netscape $nargv
fi

if [ $(echo $nsv | grep ^4 | wc -l) = 1 ]; then
  unset CLASSPATH
  export MOZILLA_HOME=$netscape_home/$nsv
  if [ -z $NPX_PLUGIN_PATH ] && [ ! -d $HOME/.netscape/plugins ]; then
    if [ ! -d $HOME/.netscape/noplugins ]; then
      mkdir $HOME/.netscape/noplugins
    fi
    export NPX_PLUGIN_PATH=$HOME/.netscape/noplugins
  elif [ -z $NPX_PLUGIN_PATH  ] && [ -d $HOME/.netscape/plugins ]; then
    export NPX_PLUGIN_PATH=$HOME/.netscape/plugins
  fi
  $netscape_home/$nsv/netscape $nargv
fi

if [ -z $old ]; then
  export CLASSPATH="$old"
else
  CLASSPATH=
  export -n CLASSPATH
fi
