#!/bin/ksh
#set -x
#@(#) installer 1.101 -last change made 11/01/02  -szusin
#@(#) 1.118 installer,  -last change made 12/09/02 -peggyj
# 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. 

# Set and export a Java VM minimal path.
# The  java  path will check 1.2.x-> 1.4.x -> default
# 
full_name=$0
prg_name=`/usr/bin/basename $full_name`
dir_name=`/usr/bin/dirname $full_name` 
host_name=`/usr/bin/uname -n`
install_dir=./.install
DTDSPMSG=/usr/dt/bin/dtdspmsg
CAT=/usr/bin/cat
CPU=`/usr/bin/uname -p`
JAVA_ARGS=""
WIZARD_ARGS=""
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
#
# Separate the arguments into "-D" and non "-D" arguments
#
ARGS=$@
for arg in $ARGS
do
        echo ${arg} | /usr/bin/egrep -s '^-D'  2>/dev/null 2>&1 1>/dev/null
        if [ $? -eq 0 ]; then
                JAVA_ARGS="${JAVA_ARGS} $arg"
        else
                WIZARD_ARGS="${WIZARD_ARGS} $arg"
        fi
done


#
# 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
current_dir=`/usr/bin/pwd`
export NLSPATH=${current_dir}/.install/locale/%L/%N:$NLSPATH

#
# Check system configuration before perform the installation
#
MIN_MEM=128
MIN_SWAP=256
SWAP_ERR=0;
MEM_ERR=0;
CURRENT_MEM=`/usr/sbin/prtconf | /bin/grep "Memory size" | /bin/awk '{print $3}'`
USED_SWAP=`LC_ALL=C /usr/sbin/swap -s | /bin/awk '{print $9}' | /bin/sed -e 's/k//g'`
AVAIL_SWAP=`LC_ALL=C /usr/sbin/swap -s | /bin/awk '{print $11}' | /bin/sed -e 's/k//g'`
#AVAIL_SWAP=$((($AVAIL_SWAP+$USED_SWAP)/1024))
AVAIL_SWAP=$(($AVAIL_SWAP+$USED_SWAP))
AVAIL_SWAP=$(($AVAIL_SWAP/1024))
if [ $AVAIL_SWAP -lt $MIN_SWAP ]; then
        SWAP_ERR=1;
fi
if [ $CURRENT_MEM -lt $MIN_MEM ]; then
        MEM_ERR=1;
fi
if [ $SWAP_ERR = 1 -o  $MEM_ERR = 1 ]; then
     Message 30 "This installation requires at least 128 MB memory and 256 MB swap space.\n"  
   if [ $SWAP_ERR = 1 -a $MEM_ERR = 1 ]; then
        Message 31 "This system only has %s MB memory " $CURRENT_MEM
	Message 32 "and %s MB swap space.\n" $AVAIL_SWAP
   else
        if [ $MEM_ERR = 1 -a $SWAP_ERR != 1 ]; then
             Message 33 "This system only has %s MB memory.\n" $CURRENT_MEM;
          
        else
           if [ $MEM_ERR != 1 -a $SWAP_ERR = 1 ]; then
                Message 34 "This system only has %s MB swap space.\n" $AVAIL_SWAP;         
           fi
        fi
    fi

    Message 35 "\nRestart the install process after providing the required space. \n"
    Message 36 "If you continue, installation performance might be adversely impacted \n"
    Message 37 "or this system might hang. \n"             
    Message 38 "\nWould you like to continue? [y/n]:"
    CONT=no
    while [ "${CONT}" = "no" ]; do
          read response
          case $response in
              y|Y) CONT=yes;
                     ;;
              n|N)
                   Message 11 "Please check the Sun ONE Studio 8 Compiler Collection Installation Guide.  Exiting ... \n";
                   exit 0;;
              *)   Message 12  "Please rerun and enter 'y' or 'n'\n";
                   exit 0;;
              esac
        done

fi
#echo "Continue the installation ...."
# look for product download units (*tar) in the same directory
# can be combined or chuncked tarfiles)

ExtractTar() {
     if [ -f  *tar.Z ]; then
           /usr/bin/uncompress *.tar.Z  >/dev/null 2>&1 1>/dev/null
     fi
     /usr/bin/rm -rf /tmp/tar.list  
     /usr/bin/ls  | /usr/bin/grep .tar > /tmp/tar.list  
     /bin/chmod 777 /tmp/tar.list
     Message 43 "Extracting the tar files...\n"
     for i in  `cat /tmp/tar.list `
     do
          /usr/bin/tar xfBpl $i  
          if [ $? -ne 0 ] ; then
                Message 41 "Exiting...  The tar file has a checksum error. Delete the file and download the file again from the product website.\n"
                exit 1;
          fi
          /usr/bin/rm $i
      done
     #clear
}

/usr/bin/ls | /usr/bin/grep -w volstart >/dev/null 2>&1 1>/dev/null
if [ $? -ne 0 ]; then 
    /usr/bin/ls | /usr/bin/grep .tar  >/dev/null 2>&1 1>/dev/null
    if [ $? -eq 0 ]; then
	/usr/bin/touch ./mytmp  1>/dev/null 2>&1 1>/dev/null
        if [ $? -ne 0 ]; then
                Message 42 "Exiting... You do not have write permission for this directory. Add write permission on the directory. \n"
	        /usr/bin/rm -rf ./mytmp
                exit 1;
        else
		ExtractTar;
        fi
	/usr/bin/rm -rf ./mytmp
else
	if [ ! -d .install ]; then
               Message 40 "Exiting...  The installer did not find the component tar files. Download the desired component tar files from the product website.\n" 
               exit 1;
        fi
     fi
fi 

#
# Make sure the user is root or exit
# Use the "su" command to ask for password and run the installer
#
set `/bin/id`
if [ $1 != "uid=0(root)" ]; then
  echo
  Message 1 "Sun ONE Studio 8, Compiler Collection \n"
  Message 2 "To use this installer you will need to be the system's root user. \n"
 if [ -d $install_dir ]; then
    s=1;
    t=3;
    while [ $s = 1 -a ! $t = 0 ]; do
        Message 3 "Please enter this system's root user password \n"
        su root -c "./installer ${ARGS}";
        s=$?;
        t=$(($t-1));
        echo " ";
    done
    if [ $t = 0 ]; then
        Message 4 "Access denied \n"
	exit 1;
    fi
 else
    s=0;
    Message 5 "Error: $install_dir directory not found \n"
    exit 1;
 fi
 exit 0;
fi

#
# User is now root.  
# Change directory to the .install area
#
# if SolarisNativeToolkit left on the system, remove them

if [ -f /tmp/SolarisNativeToolkit ]; then
   /usr/bin/rm -rf /tmp/SolarisNativeToolkit;
fi

cd $install_dir

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

prodregpkgdir=./Web_Start_3.0
OLDPRODPKG=SUNWwsr
NEWPKG1=SUNWwsr2
NEWPKG2=SUNWwsrv

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
    echo
    Message 6 "The installer requires Solaris Product Registry's /usr/lib/libwsreg.so\n";
    Message 7 "library to run (package SUNWwsr2).\n";
    echo
    Message 8 "Install the SUNWwsr2 package? (older version SUNWwsr\n";
    Message 9 "will be removed first if found) <y|n>:";
    CONT=no
    while [ "${CONT}" = "no" ]; do
        read response
        case $response in
        y|Y) CONT=yes; 
                     /usr/bin/pkginfo -q $OLDPRODPKG;
                     if [ $? -eq 0 ]; then
                           /usr/sbin/pkgrm -n $OLDPRODPKG;
                     fi;
                     $prodregpkgdir/wsadd;;
        n|N)   
               Message 10 "Installer requires /usr/lib/libwsreg.so [SUNWwsr2] to run.\n";

               Message 11 "Please check the Sun ONE Studio 8, Compiler Collection Installation Guide. Exiting ... \n";
                     exit 0;;
        *)     Message 12 "Please rerun and enter 'y' or 'n'\n";
               Message 13 "This script must be run in the foreground.\n";
               exit 1;;
esac
  done
    fi
fi
echo
#explicitly go after J2SE path
# path  1.2.x -> 1.3.x -> 1.4.x

   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

#Ask_Continue is a function to ask user for continue installation.
#  if yes, return 0
#  if no, exit from installation
#  
Ask_Continue() {
        Message 14 " Do you want to continue? <y|n>:\n "
        CONTINUE=no
        while [ "${CONTINUE}" = "no" ]; do
          read  keyin
          case $keyin  in
          y|Y) CONTINUE=yes;
               return 0;
          ;;
          n|N)  
          Message 11 "Please check the Sun ONE Studio 8, Compiler Collection Installation Guide. Exiting ... \n";
          echo
          exit 0
          ;;
          *)
           Message 12 "Please rerun and enter 'y' or 'n'\n";
          esac
       done
}
# To clean up the kryptonite connector or K2 EA1/EA2 path.
if [ -f /var/tmp/fd6u2 ] ; then
   /usr/bin/rm -rf /var/tmp/fd6u2;
fi
 
  #
  # 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.
     cd compiler_collection
     rev_val=`uname -r`
     # If Solaris 9, then installer access /usr/j2se/bin/java 
     # otherwise, installer access based on the defined java PATH.
     if  [ "5.9" != $rev_val ] ; then
       java -Dlang=$LANG  -classpath "."  ${JAVA_ARGS} -mx64m install ${WIZARD_ARGS}
     else 
       /usr/j2se/bin/java  -Dlang=$LANG  -classpath "."  ${JAVA_ARGS} -mx64m install ${WIZARD_ARGS}
     fi 

#To clean up the fd6u2 path if we have one.
if [ -f /var/tmp/fd6u2 ] ; then
   /usr/bin/rm -rf /var/tmp/fd6u2;
fi

/usr/bin/rm -rf /tmp/tar.list

