#!/bin/sh
# interactive_common_functions.intel-S2	@(#)interactive_common_functions.intel-S2	1.1		02/26/04
# begin "interactive_common_functions.solaris"
# @(#)interactive_common_functions.solaris	1.2		02/27/04
UNINSTALL_BASE=/var/sadm/prod
# end "interactive_common_functions.solaris"
# begin common_functions.solaris
# @(#)common_functions.solaris	1.7		05/25/04


sparc_cluster_info="Docs 6e07f480-9a91-108d-c1de-0800209b793b \
Studio b5ad0500-9a91-108d-c1de-0800209b793b \
NetBeans 0520fc40-9a92-108d-c1de-0800209b793b \
Perflib 4e3442c0-9a92-108d-c1de-0800209b793b \
Source 98a67d00-9a92-108d-c1de-0800209b793b"

intel_cluster_info="Docs 81b0fe80-9ae8-108d-c1de-0800209b793b \
Studio c8ae3640-9ae8-108d-c1de-0800209b793b \
NetBeans 1071cb40-9ae9-108d-c1de-0800209b793b \
Perflib 72ee5dd0-9ae9-108d-c1de-0800209b793b \
Source 61dd4cc0-9ae9-108d-c1de-0800209b793b"

tmpfile="/tmp/uuids.$$"

helper_detect_mercury()
{
	touch $tmpfile
	found_fcs="no"
	found_clusters=""

	altroot=$2
	set $1

	while [ $# -gt 0 ]
	do
		prodreg info $altroot -u $2 > $tmpfile
		if [ $? = "0" ]; then
			grep 'com\.sun\..*_10$' $tmpfile > /dev/null 2>&1
			if [ $? = "0" ]; then
				found_fcs=yes
				found_clusters="$found_clusters $1"
			fi
		fi
		shift 2
	done


	rm -f $tmpfile

	if [ "$found_fcs" = "yes" ]; then
		echo "$found_clusters"
	fi
}

# usage: detect_mercury [altroot]
detect_mercury()
{
	if [ $# -gt 0 ]; then
		args="-R $1"
	else
		args=""
	fi

	myproc=`uname -p`
	if [ "$myproc" = "sparc" ]; then
		uids="$sparc_cluster_info"
	else
		uids="$intel_cluster_info"
	fi

	# 5.8 is earliest supported version.  5.10 onwards have the
	# fully functional prodreg command.
	myversion="`uname -r`"
	if [ "$myversion" = "5.8" -o "$myversion" = "5.9" ]; then
		helper_detect_mercury_grep "$uids" "$args"
	else
		helper_detect_mercury "$uids" "$args"
	fi
}

# end common_functions.solaris
# begin "common_functions"
# @(#)common_functions	1.25		06/11/04
UNINSTALL_DIR=${UNINSTALL_BASE}/com.sun.studio_9
PREVIOUS_VERSIONS="studio_9_ea1 studio_9_ea2"

# Detect previous versions (eg EA, CA etc) OF THIS PRODUCT.
# Initial trivial version - look for uninstallers.
# Note this is not efficient, but we don't expect large
# numbers of previous versions.
detect_previous_versions()
{
 	detected=""
	previous=""

	# get the list of all uninstallers
	candidates=`ls ${UNINSTALL_BASE} 2> /dev/null`

	# filter for ones which are for *this* product
	for j in $PREVIOUS_VERSIONS
	do
          	for i in $candidates
		do
                  	match=`echo "$i" | grep "$j"` 2> /dev/null
			if [ ! -z "$match" ];
			then
                            	previous="$previous $i"
			fi
		done
	done

	# check for at least one uninstaller class in the directory
	# (because the dir gets left behind after you run the uninstaller,
	# but the uninstall class gets deleted).
	if [ ! -z "$previous" ]; then
		for i in $previous
		do
                  	if [ -f ${UNINSTALL_BASE}/"$i"/*.class ]; then
				detected=$detected" $i"
			fi
		done
	fi

        # return the detected ones.
	echo $detected
}


# run each of the "old" uninstallers previously detected
run_old_uninstallers()
{
	save_dir=`pwd`
	cd ${UNINSTALL_BASE}
	for i
	do
		cd $i
		./uninstaller
		cd ..
	done
	cd "$save_dir"
}

interactive_choose_action()
{
	Message 998 "A previous version of this product was detected on the system.\n"
	Message 997 "Select [1] to uninstall it (recommended)\n"
	Message 995 "Select [2] to exit from this installer.\n"
	Message 994 "Enter choice: "
	read choice
	case $choice in
	1) run_old_uninstallers $*
	   ;;
	2) Message 992 "Remember to select a new directory in the directory selection panel!"
	   ;;
	3) exit 1
	   ;;
	*) Message 993 "Invalid selection!\n"
	   interactive_choose_action
	   ;;
	esac
}

interactive_choose_action_fcs()
{
	Message 998 "A previous installation of this version of this product was detected \non the system.\n"
	Message 992 "Concurrent installations are not supported, and attempting to install \nmay fail and break the existing installation of your product.\n"
	Message 991 "Select [1] to exit from this installer.\n"
	Message 990 "Select [2] to attempt to install anyway.\n"
	Message 994 "Enter choice: "
	read choice
	case $choice in
	1) exit 1
	   ;;
	2) return
	   ;;
	*) Message 993 "Invalid selection!\n"
	   interactive_choose_action_fcs
	   ;;
	esac
}

doChangeDir()
{
	WIZARD_ARGS="$WIZARD_ARGS -d $1"
}


extract_rootdir()
{
	while [ $# -ne 0 ]; do
		if [ $1 = "-R" ]; then
			echo $2
			return
		fi
		shift
	done
}

helper_detect_mercury_grep()
{
	found_fcs="no"
	found_clusters=""

	version=_9_1_10
	if [ "`uname -s`" = "SunOS" ]; then
		regfile=/var/sadm/install/productregistry
		# for tail -n <num> to work...
		PATH=/usr/xpg4/bin:$PATH
	else
		regfile=/var/tmp/productregistry
	fi

	altroot=$2
	set $1

	# work around lack of prodreg command...
	while [ $# -gt 0 ]
	do
		line=`grep -n "<compid>$2" ${altroot}${regfile} 2> /dev/null | sed -e 's/:.*//' | head -n 1`
		if [ ! -z "$line" ]; then
				un_line_num=`expr $line + 2`
				un_line=`head -n $un_line_num ${altroot}${regfile} | tail -n 1`
				echo $un_line | grep $version > /dev/null 2>&1
				if [ $? = '0' ]; then
					found_clusters="$found_clusters $1"
				fi
		fi
		shift 2
	done



	if [ ! -z "$found_clusters" ]; then
		echo "$found_clusters"
	fi
}
# end "common_functions"
# begin "installer.sh"
# @(#)installer.sh	1.6		05/17/04
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`
P_KITS=studiose
WEB_TAR=studio

EA1=false
PRODUCT_NAME="Sun Studio 9"


PATH=/usr/java1.2/bin:/usr/j2se/bin:/usr/java/bin:/usr/bin:/usr/sbin:$PATH
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`
NLSPATH=${current_dir}/.install/locale/%L/%N:$NLSPATH
export 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=`expr $AVAIL_SWAP + $USED_SWAP`
AVAIL_SWAP=`expr $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 $PRODUCT_NAME 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 chunked 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 *.tar | grep $WEB_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: %s has a checksum error. Delete the file and download the file again from the product website.\n" $i
                exit 1;
          fi
     done
     Message 44 "Do you want to remove the download tar files? [y/n]: "
     CONT=no
          while [ "${CONT}" = "no" ]; do
              read response
              case $response in
                    y|Y) CONT=yes;
			 for i in  `cat /tmp/tar.list `
			 do
     	    		     /usr/bin/rm $i
			 done
                     ;;
                    n|N) CONT=no;
			mkdir save_tarfiles
			for i in  `cat /tmp/tar.list `
			do
			    /usr/bin/mv $i  save_tarfiles
			done
			Message 45 "Moved download tar files to ./save_tarfiles directory.\n"
                    break;;
                   *)   Message 12  "Please rerun and enter 'y' or 'n'\n";
                     ;;
              esac
          done
     #clear
}

/usr/bin/ls | /usr/bin/grep -w volstart  2>/dev/null 2>&1 1>/dev/null
if [ $? -ne 0 ]; then 
    /usr/bin/ls | /usr/bin/grep .tar  2>/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 "$PRODUCT_NAME \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 "$0 ${ARGS}";
        s=$?;
	t=`expr $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 $PRODUCT_NAME 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 $PRODUCT_NAME Installation Guide. Exiting ... \n";
          echo
          exit 0
          ;;
          *)
           Message 12 "Please rerun and enter 'y' or 'n'\n";
          esac
       done
}
 
### possibly run the old uninstallers...
oldies=`detect_previous_versions`
if [ ! -z "$oldies" ]; then
	interactive_choose_action $oldies
fi

### detect current version
ROOTDIR=`extract_rootdir $WIZARD_ARGS`
peers=`detect_mercury $ROOTDIR`
if [ ! -z "$peers" ]; then
	interactive_choose_action_fcs
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 $current_dir/.install
     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/java1.2/bin/java  -Dlang=$LANG  -classpath "."  ${JAVA_ARGS} -mx64m install ${WIZARD_ARGS}
     fi 

cd $current_dir/.install;
if [ ! -f $UNINSTALL_DIR/uninstaller ] ; then
   mkdir -p $UNINSTALL_DIR 
   mkdir -p $UNINSTALL_DIR/.uninstall
   /usr/bin/cp -rp ./.uninstall/uninstaller $UNINSTALL_DIR;
   /usr/bin/cp -rp ./locale  $UNINSTALL_DIR/.uninstall ;
fi

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

# end "installer.sh"
# interactive_body.solaris	@(#)interactive_body.solaris	1.1		02/26/04
