#!/bin/ksh -p
#
# ident "@(#)gl_defs.ksh	1.31 12/11/21 SMI"
#
# Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
#

#
# global variables definition
#
# NOTE: 
# These variables must be set by the master task:
#
# G_MEDIA_DIR which contains the pathname of modules, support_lib directories
#             (otherwise master task cannot source this library)
# G_PRODUCT_DIR which contains the pathname of the Sun Ray bundle area (SWS,
#             SunDs, ...)

# find local temporary directory location
if [[ -d /var/tmp ]]; then
   export G_TMP=/var/tmp
elif [[ -d /tmp ]]; then
   export G_TMP=/tmp
elif [[ -d /usr/tmp ]]; then
   export G_TMP=/usr/tmp
else
   echo "$0: Error, cannot find a temporary directory"
   exit 1
fi

export G_PROGRAM_ID="$(basename $0)"
export G_MODE="install"
export G_ADMIN_FILE="${G_MEDIA_DIR}/admin_default"
export G_JRE=""
export G_DEBUG="no"
export G_QUICK_INSTALL="no"
export G_IPSABLE=false

#
# pre-defined variables
#
case "$G_OS" in
  SunOS)
	if [[ $(uname -r) != "5.10" ]]; then
		G_IPSABLE=true
	fi
	export G_ADM_LOG_DIR="/var/adm/log"
	;;
  Linux)
	# on Linux, the journal for utinstall goes into /var/log,
	# but the journal for other utilities goes into /var/log/SUNWut
	# which is installed and removed by the SUNWutr package.
	if [[ ${G_PROGRAM_ID} = "utinstall" ]]; then
		export G_ADM_LOG_DIR="/var/log"
	else
		LINUX_LOG_DIR="/var/log/SUNWut"
		if [[ -d ${LINUX_LOG_DIR} ]]; then
			export G_ADM_LOG_DIR=${LINUX_LOG_DIR}
		else
			export G_ADM_LOG_DIR="/var/log"
		fi
	fi
	;;
esac

export G_TIME_STAMP="$(date '+%Y_%m_%d_%H:%M:%S')"
export G_PID="$$"
#
# attach pid and timestamp to G_TMP and create the temporary working
# directory with umask of 077.
export G_TMP_DIR="${G_TMP}/${G_PROGRAM_ID}.${G_PID}.${G_TIME_STAMP}"
TMPUMASK=`umask`
if ! (umask 077; mkdir ${G_TMP_DIR}); then
    print -u2 "${G_PROGRAM_ID}: unable to create temporary working directory."
    exit 1
fi
umask $TMPUMASK

export G_UT_PROD_NAME="Sun Ray Server Software"
export G_UT_VERSION="4.5"
export G_LOGFILE="${G_ADM_LOG_DIR}/${G_PROGRAM_ID}.${G_TIME_STAMP}.log"
export G_UPGRADE_DIR="${G_TMP}/SUNWut.upgrade"

# when restoring the server, use the data from an old preserved tarfile
# instead of the current configuration if this file exists.
export G_OLD_PRESERVED_FILE="${G_UPGRADE_DIR}/USE_OLD_PRESERVED"

export G_DAEMON_LOC="/etc/init.d"

export G_ACTIVITYM_PID=""
export G_MESSAGE_FILE="${G_TMP_DIR}/${G_PROGRAM_ID}.${G_PID}.message"

# Sun Ray Server Software directory
export G_SUNRAY_SERVER_DIR="Sun_Ray_Core_Services_4.5"

# architecture type to be installed.
export G_INSTALL_ARCH=$(uname -p)
if [[ $G_INSTALL_ARCH = "unknown" ]]; then
	G_INSTALL_ARCH=$(uname -m)
fi

# OS type to be installed.
export G_INSTALL_OS=$(uname -s)

# Current installed Sun Ray version.  It will be empty if not installed.
export G_SR_CURRENT_VERSION=$($G_MEDIA_DIR/utprodinfo -p SUNWutr VERSION 2>&- \
	| cut -d_ -f1)

export G_IPS_UNINSTALL_PKGS=""
export G_IPS_INSTALL_PKGS=""
if $G_IPSABLE; then
	# For uninstall, use the local metapackage deps.
	# For install/upgrade/update, use the one in the repository.
	if G_IPS_UNINSTALL_PKGS=$(pkg contents -H -t depend -o fmri SUNWut-srss 2>/dev/null); then
		# use echo to strip newlines
		G_IPS_UNINSTALL_PKGS=$(echo $(echo "$G_IPS_UNINSTALL_PKGS" | sed 's/@.*//'))
	else
		# Meta package is not installed
		G_IPS_UNINSTALL_PKGS=""
	fi
	if ! $G_UNINSTALL; then
		# use echo to strip newlines
		G_IPS_INSTALL_PKGS=$(echo $(pkg contents -H -t depend -o fmri -r SUNWut-srss | sed 's/@.*//'))
	fi
fi
