#!/bin/ksh
#
# This script will run a Web Start install.class file.  This Java install.class
# file is found in the ".install" directory directly under this installer 
# scripts directory. 

full_name=$0
prg_name=`/usr/bin/basename $full_name`
dir_name=`/usr/bin/dirname $full_name` 
host_name=`/usr/bin/uname -n`

DTDSPMSG=/usr/dt/bin/dtdspmsg
CPU=`/usr/bin/uname -p`

a1=$1
a2=$2 
a3=$3 
a4=$4 
a5=$5 
a6=$6 
a7=$7 
a8=$8 
a9=$9  

  install_dir=./.install
#
# L10N Message translation utility
#

Message()
{
  # Message() - display message from installer.cat
  #   $1 - message id
  #   $2 - fallback message text

  if [ -x $DTDSPMSG ]; then
        $DTDSPMSG -s 10 installer.cat $1 "$2" "$3"
  else
        echo "$2"
  fi
}

cd $dir_name

export NLSPATH=`/usr/bin/pwd`/$install_dir/locale/$CPU/%L/%N:$NLSPATH

#
# Login the user as a root user
# Use the "su" command to ask for password and run the installer 
#

set `/bin/id`
if [ $1 != "uid=0(root)" ]; then
  echo
  Message 1 "Solaris Web Start installer \n"
  Message 2 "To use this installer you will need to be the system's root user. \n"
  echo 
  if [ -d $install_dir ]; then
    if [ ! -z $DISPLAY ]; then
      export tmp_file="/tmp/installer_auth_$USER_$DISPLAY"
      touch $tmp_file 
      chmod 600 $tmp_file 
      /usr/openwin/bin/xauth extract - $DISPLAY > $tmp_file
      export XAUTHORITY="/.Xauthority"
    fi
    s=1;
    t=3;
    while [ $s = 1 -a ! $t = 0 ]; do 
        Message 3 "Please enter this system's root user password \n"
	if [ -z $DISPLAY ]; then
          su root -c "./installer $a1 $a2 $a3 $a4 $a5 $a6 $a7 $a8 $a9";
	else
          su root -c "/usr/openwin/bin/xauth merge $tmp_file;\
                      /bin/rm $tmp_file;\
                      ./installer $a1 $a2 $a3 $a4 $a5 $a6 $a7 $a8 $a9";
	fi
	s=$?; 
	t=$(($t-1)); 
        echo " ";
    done
    if [ $t = 0 ]; then
        Message 10 "Access denied \n"
    fi
  else
    s=0;
    Message 9 "Error: $install_dir directory not found \n"
  fi
  exit
fi

#
# User is now root.  
# Change directory to the .install area
#

cd $install_dir

#
# Look for a Java Runtime Environment 
#

jre_dir_cpu=`/usr/bin/pwd`/JRE/$CPU

#
# Force use of 1.1
#
if [ -x /usr/java1.1/bin/java ]; then
  export JAVA_HOME=/usr/java1.1	  ## Found a java on system, use it
  JRE_HOME=""
else 
  if [ -d $jre_dir_cpu ]; then
				  ## Found a jre on CD, use it 
    CLASSPATH=""                  ## Unset user variable
    JAVA_HOME=""                  ## Unset user variable
    THREADS_TYPE=""               ## Unset user variable
    ENV=""                        ## Unset user variable to prevent
                                  ## adverse reactions in the JRE.
    export JRE_HOME=$jre_dir_cpu
  fi
fi

if [ ! -x $JAVA_HOME/bin/java -a ! -x $JRE_HOME/bin/jre ]; then
  /usr/java/bin/java	## No Java was found.  Execute the default java 
			## command to produce a meaningfull L10N message 
			## and then exit.
  exit 1
fi

#
# If a Solaris Product Registry is not currently on this system,
# then first install the registry before continuing further.
#

prodregpkgdir=./Web_Start_2.0

if [ -d $prodregpkgdir/`uname -p`/SUNWws ]; then
  if [ -f /usr/bin/wsreg ]; then
      echo
      Message 4 "Looking for Solaris Product Registry \n"
      echo
      Message 5 "Old /usr/bin/wsreg registry start command was found [SUNWws package] \n"  
      Message 6 "Replacing it with new /usr/bin/prodreg start command. \n" 
      $prodregpkgdir/wsrm
  fi

  if [ -f /usr/bin/prodreg ]; then
    if [[ `/usr/bin/prodreg version` < "2.0.5" ]]
    then
      echo
      Message 4 "Looking for Solaris Product Registry \n"
      echo
      Message 7 "Upgrading Solaris Product Registry's /usr/bin/prodreg command [SUNWws package] \n"
      $prodregpkgdir/wsrm
      $prodregpkgdir/wsadd
    fi
  else
    echo
    Message 4 "Looking for Solaris Product Registry \n" 
    echo
    Message 8 "Installing Solaris Product Registry's /usr/bin/prodreg command [SUNWws package] \n"
    $prodregpkgdir/wsadd
  fi
fi

echo

#
# Now start execute the Java install.class file.  It will display the
# Web Start install wizard and the user will continue the installation 
# via its Web Start wizard panels. 
#

if [ -x $JAVA_HOME/bin/java ]; then
  $JAVA_HOME/bin/java -classpath "." install \
	$a1 $a2 $a3 $a4 $a5 $a6 $a7 $a8 $a9
else if [ -x $JRE_HOME/bin/jre ]; then
        $JRE_HOME/bin/jre -Dlang=$LANG -cp ".:$JRE_HOME" install \
	      $a1 $a2 $a3 $a4 $a5 $a6 $a7 $a8 $a9
     fi 
fi
