#!/bin/ksh 
#set -x
# @(#) 1.14 batch_installer_core  -last change made 03/24/03  SMI -pj
#
#*****************************************************************************
#									     #
# Copyright 12/17/02  Sun Microsystems, Inc. All rights Reserved                  #
# Use is subject to license terms.					     #
#									     #
#*****************************************************************************
TMP=/var/tmp
/usr/bin/mkdir -p $TMP
full_name=$0
dir_name=`/usr/bin/dirname $full_name`
install_dir=./.install
CPU=`/usr/bin/uname -p`
SERIAL_NUMBER=no
DO_CHANGE_DIR=no
DO_GET_T=yes
DO_GET_S=no
DO_GET_N=no
DO_CHANGE_ROOT=no
DO_GET_LOCALE=no
DEFAULT=yes
QUALIFY=no
NEW_DIR="/opt"
STATEFILE=statefile_core
C_KITS=compiler_collection
INSTALLER=install_core
OPTION="-noconsole -nodisplay -state $TMP/$STATEFILE"
DEFAULT_SERIAL="SCC080-MS9BDI9NU-029524692"
JAVA_CHANGE_ROOT=/a/usr/j2se/bin/java
cd $dir_name
current_dir=`/usr/bin/pwd`
#
#									     #
#  This script is the batch installer for non user intervention installation # 
#  PROTOTYPE                                                                 #
#*****************************************************************************
# 
# Set and export a Java VM minimal path.
# The  java  path will check 1.2 -> 1.3.x -> 1.4.x -> 1.1.6 ->default
rev_val=`/usr/bin/uname -r`

if  [ $rev_val = "5.7" ] ; then
    if [ -d /usr/java1.2 ] ; then
         /usr/java1.2/bin/java -version | /usr/bin/grep 1.2.1 2>/dev/null 2>&1 1>/dev/null
         if [ $? -ne 0 ]; then
            PATH=/usr/java1.1/bin:/usr/j2se/bin:/usr/java/bin:/usr/bin:/usr/sbin:$PATH
         else
            PATH=/usr/java1.2/bin:/usr/j2se/bin:/usr/java/bin:/usr/bin:/usr/sbin:$PATH
         fi
    fi
    /usr/bin/clear
else
         PATH=/usr/java1.2/bin:/usr/j2se/bin:/usr/java/bin:/usr/bin:/usr/sbin:$PATH
fi
export PATH

#
# Make sure the user is root or exit
# Use the "su" command to ask for password and run the installer
#
#
# L10N Message translation utility
#

Message()
{
  DTDSPMSG=/usr/dt/bin/dtdspmsg
  # 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
}
export NLSPATH=${current_dir}/.install/locale/%L/%N:$NLSPATH
# Internal error
#

InternalError()
{
    Message 75 "Error: Illegal input on command line.  Check for wrong option used.\n" 
    Usage
    exit 3
}


#======================================================================================

# Printout the usage of this script

Usage() {
Message 26 "USAGE: %s [-s <serial_number>] [-d <dirname>|-R <root_path>] [-h] \n\
options:\n\
	-s <serial_number>  Installs the permanent serial number. Without this\n\
                            option, 60-day trial serial number is generated \n\
		   	    by default.\n\
        -d <dirname>        Installs directory dirname\n\
        -R <root_path>      Specifies the absolute path for the root system\n\
                            and product\n\
        -h                  Displays usage\n\n" $0
}

#======================================================================================
DEFAULT_SERIAL="SCC080-MS9BDI9NU-029524692"
JAVA_CHANGE_ROOT=/a/usr/j2se/bin/java
#
#  Unset user Java variable

   CLASSPATH=""                  ## Unset user variable
   JAVA_HOME=""                  ## Unset user variable
   THREADS_TYPE=""               ## Unset user variable
   ENV=""                        ## Unset user variable to prevent
                                 ## adverse reactions in the JAVA
#


cd $dir_name
current_dir=`/usr/bin/pwd`

# update directory in statefile
if [ -f $TMP/$STATEFILE ]; then
        /usr/bin/rm -rf $TMP/$STATEFILE;
fi
/usr/bin/cp ${current_dir}/.install/$STATEFILE $TMP/$STATEFILE
/usr/bin/chmod 666 $TMP/$STATEFILE

# Do get the new directory from user
#  update in $STATEFILE 

Do_Change_Dir() {
   /usr/bin/cat $TMP/$STATEFILE | /usr/bin/sed -e "s;defaultInstallDirectory = /opt;defaultInstallDirectory = ${NEW_DIR};" -e "s;currentInstallDirectory = /newdir; currentInstallDirectory = ${NEW_DIR};" > $TMP/$STATEFILE.new
  /usr/bin/cp $TMP/$STATEFILE.new $TMP/$STATEFILE
   OPTION=" ${OPTION}"
  /usr/bin/rm $TMP/$STATEFILE.new
  DEFAULT=no
}

Do_Change_FD6u2Dir() {
   /usr/bin/cat $TMP/$STATEFILE | /usr/bin/sed -e "s;defaultFD6u2InstallDirectory = ;defaultFD6u2InstallDirectory = ${NEW_FD6u2DIR};" -e "s;currentFD6u2InstallDirectory = /newdir; currentFD6u2InstallDirectory = ${NEW_FD6u2DIR};" > $TMP/$STATEFILE.new
  /usr/bin/cp $TMP/$STATEFILE.new $TMP/$STATEFILE
   OPTION=" ${OPTION}"
  /usr/bin/rm $TMP/$STATEFILE.new
  DEFAULT=no
}
 
Do_Change_Root() {
  OPTION_CHANGE="-R $CHANGE_ROOT"
  OPTION="${OPTION} ${OPTION_CHANGE} "
  DEFAULT=no
  } 

Do_Get_Locale() {
  /usr/bin/locale | /usr/bin/grep ja 2>/dev/null 2>&1 1>/dev/null
  if [ $? -eq 0 ]; then
    OPTION_LOCALE=" -locale ja "
    OPTION=" ${OPTION} ${OPTION_LOCALE} "
    DEFAULT=no
  fi
  }

Do_Get_TnB() {
   cd ${current_dir}/.install/compiler_collection/.trynbuy;
   PATH_JAR=`/usr/bin/pwd`
   if [ -f ${JAVA_CHANGE_ROOT} ] ; then
     ${JAVA_CHANGE_ROOT}  -classpath $PATH_JAR/batch.jar:$PATH_JAR TrynBuy  > /a/var/tmp/trynbuy
     NEW_DEFAULT_SERIAL=`/usr/bin/cat /a/var/tmp/trynbuy`
     /usr/bin/rm -f /a/var/tmp/trynbuy
   else
     java  -classpath $PATH_JAR/batch.jar:$PATH_JAR TrynBuy  > /var/tmp/trynbuy
     NEW_DEFAULT_SERIAL=`/usr/bin/cat /var/tmp/trynbuy`
     /usr/bin/rm -f /var/tmp/trynbuy
   fi
}

# find WorkshopSerialLicense in statefile. We do need a serial number
Do_Need_Serial() {
   /usr/bin/cat ${current_dir}/.install/$STATEFILE | /usr/bin/grep -w WorkshopSerialLicense  > $TMP/serial.$$
   if   [` /usr/bin/cat $TMP/serial.$$ | /usr/bin/wc -l ` -eq 0 ]; then
       SERIAL_NUMBER=no
   else
       SERIAL_NUMBER=yes
   fi
/usr/bin/rm $TMP/serial.$$
}

# Initial WorkshopSerialLicense in statefile is empty.
Do_Get_Trial() {
    Do_Get_TnB;
   /usr/bin/cat $TMP/$STATEFILE | /usr/bin/sed -e "s;WorkshopSerialLicense = $DEFAULT_SERIAL;WorkshopSerialLicense = ${NEW_DEFAULT_SERIAL};" > $TMP/$STATEFILE.new
  /usr/bin/cp $TMP/$STATEFILE.new $TMP/$STATEFILE
  /usr/bin/rm $TMP/$STATEFILE.new
  OPTION="${OPTION}"
  SERIAL_NUMBER=yes
  DEFAULT=no
}

Do_Get_Serial() {
   /usr/bin/cat $TMP/$STATEFILE | /usr/bin/sed -e "s;WorkshopSerialLicense = $DEFAULT_SERIAL;WorkshopSerialLicense = ${NEW_SERIAL_NUMBER};" > $TMP/$STATEFILE.new
  /usr/bin/cp $TMP/$STATEFILE.new $TMP/$STATEFILE
  OPTION=" ${OPTION}"
  /usr/bin/rm $TMP/$STATEFILE.new
  DEFAULT=no
  SERIAL_NUMBER=yes
}

Do_Default() {
  OPTION=" ${OPTION}"
  } 

#======================================================================================

# Parse the arguments and set the necessary variables

GetOpts() {
    while getopts s:d:hR: c ;
      do
        case $c in
           s)  NEW_SERIAL_NUMBER=$OPTARG
		if [ $SERIAL_NUMBER = "yes" ]; then
               	     DO_GET_S=yes
               	     DO_GET_T=no
                     WIZARD_ARGS="${OPTION}"
	        else
    		     Message 23 "Error: The product does not require a serial number.\n"
		     Usage;
		     exit 1;
		fi
               ;;

           d)  NEW_DIR=$OPTARG
               DO_CHANGE_DIR=yes
               ;;
           R)  CHANGE_ROOT=$OPTARG
               DO_CHANGE_ROOT=yes
               Do_Change_Root
               WIZARD_ARGS="${OPTION}"
               ;;
           h)  Usage
               exit 0
               ;;
            # Unknown
           \?) InternalError;
               exit 1
               ;;
         esac
      done
      shift `expr $OPTIND - 1`
}

#if [ "$*" = "" ] ; then
#   InternalError
#fi
#======================================================================================

# Check to see this component need Serial Number or not.
#
Do_Need_Serial;

# Parse the command line options
# Batch installer read the command  line arguements

GetOpts $@

set `/bin/id`
if [ $1 != "uid=0(root)" ]; then

  echo
   Message 2 " To use this installer you will need to be the system's root user. Exiting... \n "
  exit 1
fi

#default is get try&buy serial number
#
if [ $DO_GET_T = "yes" -a $SERIAL_NUMBER = "yes" ] ; then
     Do_Get_Trial;
fi

# update serial number in statefile
#
if [ $DO_GET_S = "yes" -a $SERIAL_NUMBER = "yes" ] ; then
     Do_Get_Serial;
fi

# update directory in statefile
# if -d arg, then new dir, otherwise default /opt
Do_Change_Dir;
 WIZARD_ARGS="${OPTION}"
# Auto detect default locale
# if ja, then -locale ja otherwise default en
Do_Get_Locale
WIZARD_ARGS="${OPTION}"

if [ ${DEFAULT} = yes ]; then
     Do_Default
     WIZARD_ARGS="${OPTION}"
fi
   if [ -f ${JAVA_CHANGE_ROOT} -a  ${DO_CHANGE_ROOT} = yes ] ; then
      QUALIFY=yes
    fi

# 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

cd ${current_dir}
# Change directory to the .install area
#
#
# 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 a Solaris Product Registry is not currently on this system,
# then first install the registry before continuing further.
# temp support. will replace when pkg become available
#prodregpkgdir=./SDK_REG_3.0
#cp -rp $prodregpkgdir/sparc/libwsreg.so /usr/lib
cd ${current_dir}/.install

prodregpkgdir=./Web_Start_3.0
OLDPRODPKG=SUNWwsr
NEWPKG1=SUNWwsr2
needpkgs=0

/usr/bin/pkginfo -q $NEWPKG1
if [ $? -ne 0 ]; then
    needpkgs=1
fi

if [ -d $prodregpkgdir/`uname -p`/SUNWwsr2 ]; then
  if [ "$needpkgs" = "1" ]; then
       /usr/bin/pkginfo -q $OLDPRODPKG;
       if [ $? -eq 0 ]; then
          /usr/sbin/pkgrm -n $OLDPRODPKG;
       fi;
      $prodregpkgdir/wsadd;
  fi
fi
# back up dynamic hierarchy installer
# end of DO_CHANGE_ROOT

# if java1.4  exists in /a/usr/j2se/bin/java, then this is a jumpstart process
#JAVA_CHANGE_ROOT=/a/usr/j2se/bin/java

cd ${current_dir}/.install/${C_KITS}
Message 100 " Starting the batch-mode installation ...\n"
Message 101 " The Binary Code License Agreement will display before installation begins.\n"
if [ -f ${JAVA_CHANGE_ROOT} ] ; then
  ${JAVA_CHANGE_ROOT}  -Dlang=$LANG  -classpath "."  ${JAVA_ARGS} -mx64m ${INSTALLER}  ${WIZARD_ARGS}
  else
  java  -Dlang=$LANG  -classpath "."  ${JAVA_ARGS} -mx64m ${INSTALLER} ${WIZARD_ARGS}
fi
Message 102 " The installation is complete.\n"
# cleaning up all tmp files
/usr/bin/rm $TMP/$STATEFILE
exit 0

