#!/bin/sh
# batch_common_functions.intel-S2	@(#)batch_common_functions.intel-S2	1.1		02/26/04
# begin "batch_common_functions.solaris"
# @(#)batch_common_functions.solaris	1.4		04/14/04
UNINSTALL_BASE=/var/sadm/prod
P_KITS=studiose
# end "batch_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"
# batch_installer_studio.sh
# @(#)batch_installer_studio.sh	1.5		05/18/04
STATEFILE=statefile_ide
INSTALLER=install_ide
NEEDS_SERIAL=yes
WORKINGDIR=.install/${P_KITS}
MY_CLUSTER=Studio
# batch_body.solaris	@(#)batch_body.solaris	1.17		05/26/04
#*****************************************************************************
#									     #
# Copyright 01/26/04  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`
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"
C_KITS=compiler_collection
OPTION="-noconsole -nodisplay -state $TMP/$STATEFILE"
DEFAULT_SERIAL="SSS090-MS9BDI9NU-030288989"
JAVA_CHANGE_ROOT=/a/usr/j2se/bin/java
cd $dir_name
current_dir=`/usr/bin/pwd`
SERIAL_NUMBER="no"
#
#									     #
#  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
PATH=/usr/java1.2/bin:/usr/j2se/bin:/usr/java/bin:/usr/bin:/usr/sbin:$PATH
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
}
NLSPATH=${current_dir}/.install/locale/%L/%N:$NLSPATH
export 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 39 "USAGE: %s [-d <dirname>|-R <root_path>] [-h] \n\
options:\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
}

#======================================================================================
#
#  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_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/${P_KITS}/.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)  
		if [ "$NEEDS_SERIAL" = "yes" ]; then
			DO_GET_T=no
			DO_GET_S=yes
			NEW_SERIAL_NUMBER=$OPTARG
		else
			Message 22 "\nThis product does not require a serial number, ignoring argument... \n"
		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`
}


# 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 $NEEDS_SERIAL = "yes" ] ; then
     Do_Get_Trial;
fi

# update serial number in statefile
#
if [ $DO_GET_S = "yes" -a $NEEDS_SERIAL = "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

# Marketing requirement: remove old versions unless installing to
# a different directory.

oldies=`detect_previous_versions`
if [ ! -z "$oldies" ]; then
	if [ "$DO_CHANGE_ROOT" != "yes" ]; then
		run_old_uninstallers "$oldies"
	fi
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
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

clusters=`detect_mercury $CHANGE_ROOT`
echo $clusters | grep $MY_CLUSTER > /dev/null 2>&1
if [ $? = "0" ]; then
	Message 800 "Error: conflicting cluster already installed.  Exiting.\n"
	exit 1
fi

cd ${current_dir}/$WORKINGDIR
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"


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

# cleaning up all tmp files
/usr/bin/rm $TMP/$STATEFILE
exit 0

