#!/bin/sh

###############################################################################
##
##              DELL INC. PROPRIETARY INFORMATION
##
##  This software is supplied under the terms of a license agreement or
##  nondisclosure agreement with Dell Inc. and may not be copied or disclosed 
##  except in accordance with the terms of that agreement.
##
##  Copyright (c) 2005 Dell Inc. All Rights Reserved.
## 
##  Abstract/Purpose:
##
##  Environment:
##    Linux
##
###############################################################################

# Determine if the application has been launched from an xterm or XWindows
[ -z "${TERM}" ] && TERM=vt100
case ${TERM} in 
    nutc* | xterm* | ansi* | vt100* | screen*) # Do nothing
        ;;
    dumb* | linux*)
        [ -n "${DISPLAY}" -a -n "${XAUTHORITY}" ] && {
            tty --quiet || exec xterm +ls -e ${0} ${@} || ${0} ${@}
        }
        ;;
    *) exec xterm +ls -e ${0} ${@}
        ;;
esac

export PATH # works around problems with subshells
typeset -r _SUU_APP_NAME="Dell Server Update Utility"
typeset -r _SUU_CMD_NAME=$(basename ${0})
typeset -r _SUU_CMD_WORKDIR=$(dirname ${0})
typeset -r _CURRDIR=${PWD}

# width for console text output formatting
which tput > /dev/null 2>&1 && _TMP_WIDTH=$(tput cols)
which tput > /dev/null 2>&1 || _TMP_WIDTH=100
typeset -ri _WIDTH="${_TMP_WIDTH}"

# Global constants - readonly variables
typeset -ri _TRUE=0
typeset -ri _FALSE=1
typeset -ri _EXIT_DELAY=1 # gives customers a chance to read output
                          # on temporary terminals 

# Global Variables
typeset -i _SUU_GUI=${_FALSE}
typeset -i _SUU_X_WINDOWS=${_FALSE}

# Prints a formatted message
msgFmt() {
    typeset opts=""
    [ "$(echo -e)" != "-e" ] && opts="-e"
    typeset line=""
    for arg in "${@}"; do
        line=${line}${arg}
    done
    echo ${opts} "${line}" | fmt -s -w ${_WIDTH}
}

# Prints a formatted error message to stderr then exits false
msgErr() {
    msgFmt "${_SUU_CMD_NAME} Error: \n" "${@}" >&2
    exit -1
}

# Function is the interrupted exit point for the script
interruptScript() {
    exit ${1}
}

# Function is the common entry point for the script
enterScript() {
   
    # Make sure all the dependencies are available on the system
    typeset -r _DEPS="fmt basename dirname tput sleep echo expr sleep"
    which ${_DEPS} >/dev/null 2>&1 ||
        msgErr "Cannot find utilities on the system to execute SUU. " \
        "Make sure the following utilities are in the path:\n\t${_DEPS}"
}

# Function is the common exit point for the script
endScript() {
    typeset -i iRc=${?}
    sleep ${_EXIT_DELAY}
    trap '' EXIT

   
    PATH=${_OLD_PATH}
    export PATH

    exit ${iRc}
}

checkGUI() {
    [ -n "${DISPLAY}" -a -n "${XAUTHORITY}" ] && _SUU_X_WINDOWS=${_TRUE}

    if [ "${_SUU_X_WINDOWS}" = ${_FALSE} -a ${_SUU_GUI} = ${_TRUE} ] ; then
        msgErr "When using the \"-g\" or \"-gui\" options, the "\
               "${_SUU_APP_NAME} must be run from within X Windows."
    fi

 
    _SUU_XHOST=${_TRUE}
    xhost 2>&1 | grep -i refused 2>&1 > /dev/null && _SUU_XHOST=${_FALSE}

    if [ ${_SUU_XHOST} = ${_FALSE} -a ${_SUU_GUI} = ${_TRUE} ] ; then
        msgErr "You do not have permission to attach to the X Windows "\
               "display ${DISPLAY}"
    fi
}

setEnv() {
    _LINUX_ARCH=`arch`
    if [ ${_LINUX_ARCH} = x86_64 ] ; then
        _JAVA_PATH=${_SUU_CMD_WORKDIR}/java/linux/amd64/bin
    else
        _JAVA_PATH=${_SUU_CMD_WORKDIR}/java/linux/i386/bin
    fi
    _SUU_ETC=${_SUU_CMD_WORKDIR}/etc
    _JAVA_LIB=${_SUU_CMD_WORKDIR}/lib
    _JAVA_APP=com.dell.suu.app.SUUMain
    _OLD_PATH=${PATH}
    _APP_NAME=-Dapp.name=DellSUU
    _APP_COMMAND=-Dapp.cmd=${0}
    _APP_HOME=-Dapp.home=${_SUU_CMD_WORKDIR}/
    _APP_DEBUG_OPTIONS=-DdebugEnabled=false
    _SUU_PATH=${_SUU_CMD_WORKDIR}/bin/Linux
    _JAVA_LOGGING=-Djava.util.logging.config.file=suuapp.properties
    CLASSPATH=${_SUU_CMD_WORKDIR}/:${_SUU_ETC}:${_SUU_ETC}/guiproperties:${_JAVA_LIB}/DellSUU.jar:${_JAVA_LIB}/xercesImpl.jar:${_JAVA_LIB}/xml-apis.jar:${_JAVA_LIB}/OMCF.jar:${_JAVA_LIB}/images.jar
    export CLASSPATH
    PATH=${_JAVA_PATH}:${_SUU_PATH}:${PATH}
    export PATH
}

appendOption(){
        if [ -z "${_SUU_OPTIONS}" ] ; then
            _SUU_OPTIONS="${@}"
        else
            _SUU_OPTIONS="${_SUU_OPTIONS}" "${@}"
        fi
}

###############################################################################
# Main() Script
###############################################################################
# Handle Abnormal exit conditions
trap "endScript" EXIT
trap "interruptScript 99" HUP INT QUIT BUS SEGV PIPE TERM # 1 2 3 10 11 13 15

enterScript

_SUU_GUI=${_TRUE}
appendOption "-gui"

checkGUI
setEnv

if [ -z "${_SUU_LOG_DIR}" ] ; then
${_JAVA_PATH}/java ${_APP_NAME} ${_APP_HOME} ${_APP_COMMAND} ${_APP_DEBUG_OPTIONS} ${_JAVA_LOGGING} ${_JAVA_APP}\
     ${_SUU_OPTIONS} ${_SUU_LOG_OPTION}
else
${_JAVA_PATH}/java ${_APP_NAME} ${_APP_HOME} ${_APP_COMMAND} ${_APP_DEBUG_OPTIONS} ${_JAVA_LOGGING} ${_JAVA_APP}\
     ${_SUU_OPTIONS} ${_SUU_LOG_OPTION} "${_SUU_LOG_DIR}"
fi

PATH=${_OLD_PATH}
export PATH
