#!/bin/ksh -p
#
# ident "@(#)M29AdminGUI.ksh	1.7 07/06/06 SMI"
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# DESCRIPTION: Install script for Sun Ray Web Administration (Admin GUI)
#
# 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
#

#
# IsAdminGUIConfigured()
#
# Description:
#   Check whether the Admin GUI is configured and a Tomcat path (catalina.home)
#   has been specified (all other properties have suitable defaults).
#
# Parameters:
#   (none)
#

IsAdminGUIConfigured() {
    if [ -f "${SRWA_CFG}" ]; then
        CATALINA_HOME="`grep '^catalina.home=' "${SRWA_CFG}" | cut -f2 -d'='`"
        if [ -n "$CATALINA_HOME" ]; then
            return 0
        fi
    fi
    return 1
}

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

Module_Init() {
    _SW_INSTALLED="no"
    
    if AnyPackageInstalled $SRWA_PKG_LIST ; then
        _SW_INSTALLED="yes"
    fi
    
    if IsInstallRequired ; then
        _DO_INSTALL="yes"
        if [[ ${_SW_INSTALLED} = "yes" ]]; then
            _DO_REMOVE="yes"
        fi

        if IsAdminGUIConfigured ; then
            AddPostInitMessage "Upgrade\t [ data for $SRWA_PROD_NAME ]"
        fi
        

    elif IsPreserveRequired ; then
        if [[ ${_SW_INSTALLED} = "yes" ]]; then
            if IsAdminGUIConfigured; then
                AddPostInitMessage "Preserve [ data for $SRWA_PROD_NAME ]"
            fi
        fi
    
    elif IsUninstallRequired ; then
        if [[ ${_SW_INSTALLED} = "yes" ]]; then
            _DO_REMOVE="yes"
        fi
    fi
    return 0
}

#
# Module_Preserve()
#
# Description:
#   Preserve Admin GUI Configuration  
#
# Parameters:
#   (none)
#

Module_Preserve() {
    if IsAdminGUIConfigured; then
        SaveFiles $SRWA_CFG
        if [[ $? != "0" ]]; then
            return 2
        fi
    fi
    return 0
}

#
# Module_Remove()
#
# Description:
#    Remove Admin GUI Packages
#
# Parameters:
#   (none)
#

Module_Remove() {
    RemoveMsg "$G_UT_PROD_NAME" "$G_SR_CURRENT_VERSION $SRWA_PROD_NAME"
    if ! RemoveProduct $G_ADMIN_FILE $SRWA_PKG_LIST; then
        Note "$SRWA_PROD_NAME not successfully removed"
        return 1
    fi
    
    return 0
}

#
# Module_Install()
#
# Description:
#    Install Admin GUI Packages
#
# Parameters:
#   (none)
#

Module_Install() {
    typeset vflag=""
    typeset I_PKGS=""

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

    InstallMsg "$G_UT_PROD_NAME" "$G_UT_VERSION $SRWA_PROD_NAME"

    # cleanup old instances
    RemoveProduct $G_ADMIN_FILE $SRWA_PKG_LIST || \
	Error "failed to completely remove $SRWA_PROD_NAME"

    case "$G_OS" in
        SunOS) I_PKGS="$SRWA_PKG_LIST";;
        Linux) I_PKGS="$SRWA_RPM_LIST";;
    esac

    if ! InstallProduct $G_ADMIN_FILE ${UT_PKG_DIR} $I_PKGS; then
        Error "$SRWA_PROD_NAME not successfully installed"
    fi

    return 0
}

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

Module_Restore() {
    if GetPreservedFilePath "$SRWA_CFG"; then
        cp -p "${_RETURN_VAL}" "$SRWA_CFG"
        echo "Restoration of $SRWA_PROD_NAME data complete"
    fi
    
    if ! IsAdminGUIConfigured; then
	fmt <<-!

		Use 'utconfig -w' to specify Tomcat installation directory
		and to complete $SRWA_PROD_NAME configuration.
    
	!
    fi
    return 0
}

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

Module_Abort() {
    return 0
}

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

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
#

typeset -r SRWA_PROD_NAME="Admin GUI"
typeset -r UT_PKG_DIR=$(GetPackagePath ${G_SUNRAY_SERVER_DIR})
typeset -r ETC_OPT_UT="/etc/opt/SUNWut"
typeset -r SRWA_CFG="${ETC_OPT_UT}/webadmin/webadmin.conf"

typeset -r SRWA_PKG_LIST="\
   SUNWutwa \
   SUNWutwar \
   SUNWutwh \
   SUNWutwl"

typeset LINUX_PKG_EXT="-4.0-?*.i?86.rpm"
typeset PkgName
typeset RPMS=""
for PkgName in $SRWA_PKG_LIST
do
   RPMS="$RPMS ${PkgName}${LINUX_PKG_EXT}"
done
typeset -r SRWA_RPM_LIST="$RPMS"

#
# END
#

#
# FRAMEWORK CODE 
#

. ${G_MEDIA_DIR}/support_lib/framework_lib
