#!/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`

JAVA_ARGS=""
WIZARD_ARGS=""

#
# Separate the arguments into "-D" and non "-D" arguments
#
ARGS=$@
for arg in $ARGS
do
	echo ${arg} | /usr/bin/egrep -s '^-D'
	if [ $? -eq 0 ]; then
		JAVA_ARGS="${JAVA_ARGS} $arg"
	else
		WIZARD_ARGS="${WIZARD_ARGS} $arg"
	fi
done


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 ${ARGS}";
	else
          su root -c "/usr/openwin/bin/xauth merge $tmp_file;\
                      /bin/rm $tmp_file;\
                      ./installer ${ARGS}";
	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

if [ -x /usr/java/bin/java ]; then
  export JAVA_HOME=/usr/java	  ## 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`/SUNWwsr ]; 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.1.0" ]]
    then
      echo
      Message 4 "Looking for Solaris Product Registry \n"
      echo
      Message 7 "Upgrading Solaris Product Registry's /usr/bin/prodreg command [SUNWwsr 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 [SUNWwsr 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 "." ${JAVA_ARGS} install ${WIZARD_ARGS}
else if [ -x $JRE_HOME/bin/jre ]; then
        $JRE_HOME/bin/jre -Dlang=$LANG -cp ".:$JRE_HOME" ${JAVA_ARGS} install \
	      ${WIZARD_ARGS}
     fi 
fi
