#! /bin/ksh -p
#
# ident "@(#)install_common.ksh	1.3	03/04/30 SMI"
#
# Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
# Derived from:
##
## "@(#)install_common.sh 1.7 01/11/30
##
## Start a Java environment to run a Solaris Web Start class archive file. 
## The class archive file name is passed in the $1 argument.
#

ORIG_ARGS=$@
full_name=$0
ARCHIVE_NAME=install
prg_name=`/usr/bin/basename $full_name`
dir_name=`/usr/bin/dirname $full_name` 
host_name=`/usr/bin/uname -n`
os_version=`/usr/bin/uname -r`
os_archive_name=

DTDSPMSG=/usr/dt/bin/dtdspmsg
CPU=`/usr/bin/uname -p`

JAVA_ARGS=""
WIZARD_ARGS="-a admin"

# If WIZARD_SC_CORE is set to non-null, this is a Sun Cluster core CD wizard
WIZARD_SC_CORE=1

# This response file name is the same as used by scinstall
SCINSTALL_SUNWEXPLO_RESPONSE=/tmp/.SUNWexplo.response

# This name is the response file directory and name used by the wizard
WIZARD_SUNWEXPLO_RESPONSE_DIR=/tmp/.SUNWexplo.response.d
WIZARD_SUNWEXPLO_RESPONSE=${WIZARD_SUNWEXPLO_RESPONSE_DIR}/SUNWexplo

cd $dir_name

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

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
}

export NLSPATH=`/usr/bin/pwd`/locale/$CPU/%L/%N:$NLSPATH

#
# If the default ARCHIVE_NAME is not found, try Solaris specific version
#
if [[ ! -f ${ARCHIVE_NAME}.class ]];  then
	case ${os_version} in
	"5.8")	os_archive_name=install8 ;;
	"5.9")	os_archive_name=install9 ;;
	*)
		Message 11 "Unsupported version of SunOS \n"
		exit 1
		;;
	esac

	ARCHIVE_NAME=${os_archive_name}
fi

# 
# If this is the Sun Cluster core CD, source in scinstall_common.
#
if [[ -n "${WIZARD_SC_CORE}" ]]; then

	typeset product_directory=
	typeset scinstall_lib=

	# product_directory
	product_directory="$(cd ../;  echo SunCluster_*)"
	if [[ -z "${product_directory}" ]] ||
	    [[ $(set -- ${product_directory};  echo $#) -gt 1 ]]; then
		Message 12 "Unexpected CD layout \n"
		exit 1
	fi

	# Set the scinstall_lib
	case ${os_version} in
	"5.8")	scinstall_lib=../${product_directory}/Sol_8/Tools/lib ;;
	"5.9")	scinstall_lib=../${product_directory}/Sol_9/Tools/lib ;;
	esac
	if [[ -z "${scinstall_lib}" ]] || [[ ! -d ${scinstall_lib} ]]; then
		Message 14 "Cannot find the Tools directory \n"
		exit 1
	fi

	# Source in the scinstall code for creating the response file.
	. ${scinstall_lib}/scinstall_common
fi

#
# Separate the arguments into "-D" and non "-D" arguments
#
ARGS=$@
for arg in $ARGS
do
	echo ${arg} | /usr/bin/egrep -s '^-D'
	if [ $? -eq 0 ]; then
		JAVA_ARGS="${JAVA_ARGS} $arg"
	else
		WIZARD_ARGS="${WIZARD_ARGS} $arg"
	fi
done

#
# Login the user as a root user
# 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 Cluster Web Start installer \n"
  Message 2 "To use this installer you will need to be the system's root user. \n"
  echo 
    if [ ! -z $DISPLAY ]; then
      export tmp_file="/tmp/installer_auth_$USER_$DISPLAY"
      touch $tmp_file 
      chmod 600 $tmp_file 
      /usr/openwin/bin/xauth extract - $DISPLAY > $tmp_file
      export XAUTHORITY="/.Xauthority"
    fi
    s=1;
    t=3;
    while [ $s = 1 -a ! $t = 0 ]; do 
        Message 3 "Please enter this system's root user password \n"
	if [ -z $DISPLAY ]; then
          su root -c "./install_common ${ORIG_ARGS}";
	else
          su root -c "/usr/openwin/bin/xauth merge $tmp_file;\
                      /bin/rm $tmp_file;\
                      ./install_common ${ORIG_ARGS}";
	fi
	s=$?; 
	t=$(($t-1)); 
        echo " ";
    done
    if [ $t = 0 ]; then
        Message 10 "Access denied \n"
    fi
    exit
fi

#
# User is now root.  
# Look for a Java Runtime Environment 
#

ClearEnv
local_jre=`/usr/bin/pwd`/JRE
local_jre_bin=$local_jre/jre/bin/$CPU

if [ -d $local_jre_bin -a ! -x /usr/java/bin/java ]; then 	
    export JAVA_HOME=$local_jre   ## Use local jre
else 
  export JAVA_HOME=/usr/java      ## Use system's jre 
fi

echo

# If the DISPLAY environment variable is not set, then
# cause the "-nodisplay" option to be given to the wizard
# so that it operates in non-graphical display mode.
[ -z "${DISPLAY}" ] && DISPLAY_ARG='-nodisplay'

#
# If this is the Sun Cluster core CD, create the response file in
# the response directory used by the wizard.
#
# The WebStart PkgUnit class takes the name of a response file directory,
# rather than the name of a response file.   It then looks in that
# directory for the name of a response file which matches the name
# of the package it is installing.
#
# This is a bit of a hack.   The user may select software components
# from the wizard which do not require this response file data.  However,
# it has the benefit of easily sharing the response file generation
# code already found in scinstall.
#
if [[ -n "${WIZARD_SC_CORE}" ]]; then

	/bin/rm -f ${SCINSTALL_SUNWEXPLO_RESPONSE}
	/bin/rm -rf ${WIZARD_SUNWEXPLO_RESPONSE_DIR}
	SUNWexplo_responsefile
	if [[ ! -f ${SCINSTALL_SUNWEXPLO_RESPONSE} ]]; then
		Message 15 "Cannot create the response file for SUNWexplo \n"
		exit 1
	fi
	(umask 0; /bin/mkdir -m 0755 ${WIZARD_SUNWEXPLO_RESPONSE_DIR})
	if [[ ! -d ${WIZARD_SUNWEXPLO_RESPONSE_DIR} ]]; then
		/bin/rm -f ${SCINSTALL_SUNWEXPLO_RESPONSE}
		Message 16 "Cannot create the response file directory for SUNWexplo \n"
		exit 1
	fi
	/bin/mv ${SCINSTALL_SUNWEXPLO_RESPONSE} ${WIZARD_SUNWEXPLO_RESPONSE}
	if [[ ! -f ${WIZARD_SUNWEXPLO_RESPONSE} ]]; then
		/bin/rm -f ${SCINSTALL_SUNWEXPLO_RESPONSE}
		/bin/rm -rf ${WIZARD_SUNWEXPLO_RESPONSE_DIR}
		Message 17 "Cannot copy the response file for SUNWexplo \n"
		exit 1
	fi
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. 
#

eval $JAVA_HOME/bin/java -classpath "." ${JAVA_ARGS} ${ARCHIVE_NAME} ${DISPLAY_ARG} ${WIZARD_ARGS}
result=$?

#
# If this is the Sun Cluster core CD, remove the response file data
#
if [[ -n "${WIZARD_SC_CORE}" ]]; then
	/bin/rm -rf ${WIZARD_SUNWEXPLO_RESPONSE_DIR}
fi

# done
exit ${result}
