#!/bin/ksh -p
#
# ident "@(#)M39CAM.ksh	1.23 05/03/16 SMI"
#
# Copyright 2001-2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# DESCRIPTION: To install the kiosk and associated bundled packages
#
# The following exported variables (initialized by the master task) are 
# available (read-only) for the module:
#
# G_PROGRAM_ID    : program name
# G_MEDIA_DIR     : pathname of the install directory on the media (CD,...)
# G_PRODUCT_DIR   : pathname of the bundles directory on the media (CD,...)
# G_ADMIN_FILE    : pathname of the admin file used by pkgadd. default provided.
# G_DEBUG         : debug on/off. Possible values "yes", "no"
# G_QUICK_INSTALL : used to force a quick install (no user inputs).
#                   possible values "yes", "no"
# G_TMP_DIR       : pathname of the temporary directory 
# G_UT_PROD_NAME  : product name
# G_UT_VERSION    : product version
# G_DAEMON_LOC    : location of OS daemon scripts
# G_LOGFILE       : pathname of the log file.
# G_PID           : PID of the master task
#

#
# Module developers to provide the following functions:
# Module_Init, Module_Preserve, Module_Remove, Module_Install, Module_Restore
# Module_Abort, Module_Exit
#

#
# Module_Init()
#
# Description:
#   initialization of the module for installation, uninstallation
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Init() {

    _SW_INSTALLED="no"

  case "$G_OS" in
    Linux) return 0;;
    SunOS)
    ;;
  esac


    if AnyPackageInstalled $KIOSK_PKG_LIST ; then
	_SW_INSTALLED="yes"
	GetCurrentSRVersion
	INSTALLED_VERSION=$_RETURN_VAL
    fi

    if IsInstallRequired ; then
	_DO_INSTALL="yes"
        if [[ ${_SW_INSTALLED} = "yes" ]]; then
           # if software is already installed, remove it before installing
           # and upgrading it.
           _DO_REMOVE="yes"
        fi
	if IsDataPreserved; then
	    #
	    # found preserved file, check to see if there is any CAM
	    # data to be upgraded.
	    #
	    GetPreservedVersion
	    typeset -r vmajor=${_RETURN_VAL%%.*}
	    typeset -r vminor=${_RETURN_VAL#*.}
	    CompareVersion $_RETURN_VAL "1.2"
	    if [[ $? -ne 2 ]]; then
		# Only print the upgrade message if upgrading from SR1.2 or
		# later.  There as no CAM prior to 1.2.
	    	AddPostInitMessage "Upgrade\t [ data for Controlled Access Mode ]"
	    fi
	elif [[ ${_SW_INSTALLED} = "yes" ]]; then
	    AddPostInitMessage "Upgrade\t [ data for Controlled Access Mode ]"
	fi
    elif IsPreserveRequired ; then
	if [[ ${_SW_INSTALLED} = "yes" ]]; then
	    AddPostInitMessage "Preserve [ data for Controlled Access Mode ]"
	fi
    elif IsUninstallRequired ; then
	_DO_REMOVE="yes"
    fi
    return 0
}

#
# Module_Preserve()
#
# Description:
#   
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Preserve() {

   SaveFiles $KIOSK_PRESERVE_FILES_ALL
   if [[ $? != 0 ]]; then
      return 2
   fi
   return 0

}

#
# Module_Remove()
#
# Description:
#    Remove Kiosk Packages
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Remove() {
  RemoveMsg "$KIOSK_PROD_NAME" "$INSTALLED_VERSION"

  if ! RemoveProduct $G_ADMIN_FILE $KIOSK_PKG_LIST; then
     Note "$KIOSK_PROD_NAME not successfully removed"
     return 1
  fi

  return 0

}

#
# Module_Install()
#
# Description:
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Install() {

    typeset vflag=""

    if [[ $G_DEBUG = "yes" ]]; then
	vflag="-v"
    fi

    # cleanup old instances
    RemoveProduct $G_ADMIN_FILE $KIOSK_PKG_LIST || \
	Error "failed to completely remove $KIOSK_PROD_NAME"

    if ! InstallProduct $G_ADMIN_FILE ${UT_DIR} $KIOSK_PKG_LIST; then
	if IsDataPreserved; then
	    fmt <<-!

	$KIOSK_PROD_NAME has not installed correctly. All data saved during the
	upgrade "Save & Restore" has been retained at the following location:
	${G_UPGRADE}

	!
	fi 
	Error "$KIOSK_PROD_NAME not successfully installed"
    fi

    return 0

}

#
# Module_Restore()
#
# Description:
#    Restore saved configuration data
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Restore() {

    typeset path
    typeset file
    typeset USER_CONF="/var/opt/SUNWbb/users.conf"
    typeset USER_TEMP="/var/opt/SUNWbb/users.temp" 

    for file in $KIOSK_PRESERVE_FILES_ALL ; do
	if GetPreservedFilePath "$file"; then
	    path=${_RETURN_VAL}
	    cp -p "$path" "$file"
	fi   
    done

    if [[ -e $USER_CONF ]]
    then
	# bbmkuser will fail if users.conf is already there. so
	# it must be moved so that we can still retrieve the configuration. 
	mv $USER_CONF $USER_TEMP
	CreateKioskConfig
	if [[ $? = 0 ]]; then
		rm $USER_TEMP
	fi
    fi

    if [ -f $UT_VAR_DIR/kiosk/kiosk.conf ]
    then 
        if [ -f /var/opt/SUNWut/kiosk/config/dtsession ]
        then
            mv /var/opt/SUNWut/kiosk/config/dtsession /tmp
            /opt/SUNWut/lib/create_files
            if [ -f /var/opt/SUNWut/kiosk/config/000.dtsession ]
            then
                grep -v '^enable=' /tmp/dtsession > \
                    /var/opt/SUNWut/kiosk/config/dtsession
                grep '^enable=' /var/opt/SUNWut/kiosk/config/000.dtsession >> \
                    /var/opt/SUNWut/kiosk/config/dtsession
                rm /var/opt/SUNWut/kiosk/config/000.dtsession
            fi
            /opt/SUNWut/lib/create_arrays
        fi
    fi

    echo "Restoration of Controlled Access Mode data complete"

    return 0

}

CreateKioskConfig() {
        RemoveStaleUsers
        STATUS=$?
                          
        if [[ $STATUS = 0 ]]; then
                 print "\nRestoring Controlled Access Mode configuration ...\n"
                /opt/SUNWbb/bin/bbmkuser -c `more $USER_TEMP`
                RET=$?
                if [[ $RET != 0 ]]; then
			PrintKioskErrorMsg
			STATUS=1
		fi
        fi
	return $STATUS
}

RemoveStaleUsers() {  
	typeset STATUS=1
                        
	stale_users=`/opt/SUNWbb/bin/bbmkuser -t 2> /dev/null`
	if [[ $? = 0 ]]; then
		return 0
	fi
	/opt/SUNWbb/bin/bbmkuser -r
	if [[ $? != 0 ]]; then
		PrintKioskErrorMsg
	else
		STATUS=0
	fi              
	return $STATUS
}

PrintKioskErrorMsg() {
cat <<-!

	Controlled Acccess Mode configuration failed!  Please remove all 
	users with the comment field 'ControlledAccessUser' from the 
	/etc/passwd file and run utconfig to reconfigure Controlled 
	Access Mode.
	!
}

#
# Module_Abort()
#
# Description:
#    Abort procedure
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Abort() {

    return 0

}

#
# Module_Exit()
#
# Description:
#    Exit procedure (normal termination)
#
# Parameters:
#   (none)
#
# Globals used:
#

Module_Exit() {

    return 0

}

#
# END
#

#
# MAIN STARTS HERE
#

trap "exit 2" HUP INT QUIT TERM

#
# include libraries
#

. ${G_MEDIA_DIR}/support_lib/upgrade_lib
. ${G_MEDIA_DIR}/support_lib/iu_lib
. ${G_MEDIA_DIR}/support_lib/module_lib
. ${G_MEDIA_DIR}/support_lib/util_lib

export _EVENT=$1
export _MODULE_NAME=$(basename $0)
export _VARS_LIST=""
export _EXIT_CODE=0
export _RETURN_VAL=0
export _VAR_STORAGE_FILE="${G_TMP_DIR}/.${G_PROGRAM_ID}.${_MODULE_NAME#???}"

DeclareModuleVar _DO_INSTALL="no"
DeclareModuleVar _DO_REMOVE="no"
DeclareModuleVar _SW_INSTALLED="no"
DeclareModuleVar _SW_COMPATIBLE="yes"

#
# BEGIN: Developers module variables definition here
#

DeclareModuleVar INSTALLED_VERSION=""

typeset -r KIOSK_PROD_NAME="Controlled Access Mode"
typeset -r UT_DIR=$(GetPackagePath ${G_SUNRAY_SERVER_DIR})
typeset -r UT_ETC_DIR="/etc/opt/SUNWut"
typeset -r UT_VAR_DIR="/var/opt/SUNWut"
typeset -r UT_BB_VAR_DIR="/var/opt/SUNWbb"

typeset -r KIOSK_PRESERVE_FILES_ALL="
				$UT_VAR_DIR/kiosk/kiosk.conf \
				$UT_BB_VAR_DIR/users.conf"

typeset -r KIOSK_PKG_LIST="SUNWutkir SUNWutkio"

#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
