#!/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 "uninstaller.sh"
# @(#)uninstaller.sh	1.5		05/03/04
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
CAT=/usr/bin/cat
CPU=`/usr/bin/uname -p`
JAVA_ARGS=""
WIZARD_ARGS=""
RLEVEL=`/usr/bin/uname -r`
rev_val=`/usr/bin/uname -r`
current_dir=`/usr/bin/pwd`
AltRoot=false
if [ ${current_dir} = "/var/sadm/prod/com.sun.studio_9" ] ; then
       UNISTALLDIR="/var/sadm/prod"
else
       UNISTALLDIR=`cd $current_dir; cd ..; pwd`
       AltRoot=true
       ALTROOT=`echo ${UNISTALLDIR} | sed s:'/var/sadm/prod':: `
fi
UNINSTALL_OPTION="-nodisplay -noconsole"
if [ ${AltRoot} = true ] ; then
    UNINSTALL_OPTION=" -nodisplay -noconsole -R ${ALTROOT}"    
fi
LOCAL_JRE="/usr/java"
LOCAL_JRE_1_2="/usr/java1.2"
LOCAL_JRE_2="/usr/j2se"
LOCAL_JRE_3="/usr/jdk/j2sdk1.4.2_02"
LOCAL_JRE_F1="/usr/j2sdk1.4.1"
LOCAL_JRE_F2="/usr/j2sdk1.4.2"
LOCAL_JRE_WHICH_JAVA=`/usr/bin/which java | sed s:'/bin/java':: `
ALT_JRE=" "
FindJre=false
current_dir=`/usr/bin/pwd`
#
# 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
}

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 51 "\nUSAGE: %s [-h] [-j] \n\
options:\n\
        -h	This uninstaller script will remove all Sun \n\
		Studio 9 software for you.\n\n" $0
        -j      Enter the  jre path
}

# Parse the arguments and set the necessary variables

GetOpts() {
    while getopts hj: c ;
    do
        case $c in
           h)  Usage
               exit 0
               ;;
           j)  ALT_JRE=$OPTARG
               if [ ! -d "${ALT_JRE}" ]; then
                      Message 106 'The argument '${ALT_JRE}' to the '-j' option is not a directory. \n'
                      exit 1
               fi
               ;;

            # Unknown
           \?) InternalError;
               exit 1
               ;;
         esac
    done
    shift `expr $OPTIND - 1`
}
ClearEnv()
{
    CLASSPATH=""                  ## Unset user variable
    THREADS_TYPE=""               ## Unset user variable
    ENV=""                        ## Unset user variable to prevent
                                  ## adverse reactions in the JRE.
    DISPLAY_ARG=""                ## Unset display argument variable
}
ValidateJRE()
{
    typeset -r java_home="$1"
    typeset -r rlevel="$2"
    typeset -i valid=0
    typeset -i invalid=1
    set `${java_home}/bin/java -version 2>&1 | /bin/head -1 | /bin/sed 's/"//g'`
    typeset -r java_ver="$3"
    if [ -n "$java_ver" ]; then
        if [[ "$java_ver" != "${java_ver#1.1}" ||
              "$java_ver" != "${java_ver#1.0}" ]]; then
             return $invalid
        else
            return $valid
        fi
    else
        return $invalid
    fi
}

LOCAL_JRE_WHICH_JAVA=`/usr/bin/which java | sed s:'/bin/java':: `
JreList=" ${ALT_JRE} ${LOCAL_JRE_1_2}  ${LOCAL_JRE_WHICH_JAVA} ${LOCAL_JRE} ${LOCAL_JRE_1_2} ${LOCAL_JRE_2} ${LOCAL_JRE_3}  ${LOCAL_JRE_F1} ${LOCAL_JRE_F2}"
ClearEnv

cd $dir_name
current_dir=`/usr/bin/pwd`
NLSPATH=${UNISTALLDIR}/com.sun.studio_9/.uninstall/locale/%L/%N:$NLSPATH
export NLSPATH
# set JAVA path
PATH=/usr/java1.2/bin:/usr/j2se/bin:/usr/java/bin:/usr/bin:/usr/sbin:$PATH
export PATH
# Parse the command line options

GetOpts $@

#
# 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 Studio 9 \n"
  Message 2 "To use this installer you will need to be the system's root user. \n"
 if [ -d $UNISTALLDIR ]; 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 "./uninstaller ${ARGS}";
        s=$?;
	t=`expr t - 1`
        echo " ";
    done
    if [ $t = 0 ]; then
        Message 4 "Access denied \n"
        exit 1;
    fi
 else
    s=0;
    Message 50 "Error: $UNISTALLDIR directory not found \n"
    exit 1;
 fi
 exit 0;
fi

#======================================================================================
#
#  Unset user Java variable
ClearEnv()
{
    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.
    DISPLAY_ARG=""                ## Unset display argument variable
}

# This function validate the JAVA passed in to see whether the version is
# a valid one supported by the underline OS.

ValidateJRE()
{
    typeset -r java_home="$1"
    typeset -r rlevel="$2"
    typeset -i valid=0
    typeset -i invalid=1
    set `${java_home}/bin/java -version 2>&1 | /bin/head -1 | /bin/sed 's/"//g'`
    typeset -r java_ver="$3"
    if [ -n "$java_ver" ]; then
        if [[ "$java_ver" != "${java_ver#1.1}" ||
              "$java_ver" != "${java_ver#1.0}" ]]; then
             return $invalid
        else
            return $valid
        fi
    else
        return $invalid
    fi
}

UpgradeJRE()
{
adminDir=.install/j2sdk_1.4.2/pkg_util/admin
pkgDir=./kits/j2sdk_1.4.2/packages
patchdir=./kits/j2sdk_1.4.2/patches
Message 110 " Adding  SUNWj3rt packages to default /usr/j2se"
pkgadd -d `pwd`/$pkgDir -n -a `pwd`/$adminDir/SUNWj3rt SUNWj3rt

cd ./kits/j2sdk_1.4.2/patches
exec ./install_cluster
    Message 111 "  Reboot ... "
    /sbin/init 6
exit
}
# J2SE pkg and patches are on companion  CD on Sparc
# However, it resdies on the same Cd on intel

ValidatePlatform()
{
 case $CPU in

  i386)
    UpgradeJRE()
    ;;
  sparc)
      if [ ! -d ./kits/$J2SEVersion ]; then
          Message 107 " get the companion CD"
          exit
      else
          UpgradeJRE
      fi
    ;;
 esac
}
LOCAL_JRE_WHICH_JAVA=`/usr/bin/which java | sed s:'/bin/java':: `

ClearEnv

JreList=" ${ALT_JRE} ${LOCAL_JRE_WHICH_JAVA} ${LOCAL_JRE} ${LOCAL_JRE_1_2} ${LOCAL_JRE_2} ${LOCAL_JRE_F1} ${LOCAL_JRE_F2}"
ClearEnv



# start uninstallation...

Message 52 "Starting the uninstallation ... \n"

rm -rf /tmp/.list*.*
cd $UNISTALLDIR;

ls | grep studio_9  > /tmp/.list.$$
ls | grep s1s9_compiler_collection >> /tmp/.list.$$
#ls com.sun.s1s9_* > /tmp/.list.$$
        ## execute all uninstaller script
for i in `/bin/cat  /tmp/.list.$$ | sed -e s/\://'`
do
      cd $i 
      if [ -f *.class ] ; then 
        /bin/ls *.class > /tmp/.list_2.$$
        for j in `/bin/cat   /tmp/.list_2.$$  | sed -e 's/\.class//'`
        do
	    echo "$j"
            java -Dlang=$LANG  -classpath "."  ${JAVA_ARGS} -mx64m ${j} ${UNINSTALL_OPTION}
        done
      fi
        cd ..;


# end of each release uninstallation
done 

Message 53 "End of the uninstallation ... \n"
# end of all product list of k2 ea1, ea2 and fcs, rainier fcs 
rm -rf /tmp/.list*.*


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