#!/bin/sh
#!scexe

##############################################################
#  (C) Copyright 2001, 2002, 2005-2007 HPDC                  #
############################################################## 

if test -n "$ZSH_VERSION"; then
    emulate sh
    NULLCMD=:
fi
_SKIP=382
_INTERFACE_VERSION=scexe-interface-1.03-22
_INSTALLER=setup

###############
# Script Die
###############
Die()
{
    rm -rf $_TMPFILE $_TMPDIR; 
    exit $@;
}

###############
# Script Unpack 
###############
Unpack()
{
    _UNPACK_DIR=$1


    #if the directory exists we can proceed. If not, create it.
    if test -d $_UNPACK_DIR; then  #if the FILE exists AND is a direcotry...
        :  #do nothing
    else
        if test -f $_UNPACK_DIR; then   #the FILE exists but isn't a directory
            echo "$0: File already exists: $_UNPACK_DIR" >&2;
            Die 3;
        else  #if the FILE does not exist...
             mkdir -p $_UNPACK_DIR || \
            {
                echo "$0: cannot create directory $_UNPACK_DIR" >&2;
                Die 3;
            }
        fi
    fi

    _TMPFILE=`mktemp $_UNPACK_DIR/sc$$.XXXXXX` || \
    {
        echo "$0: cannot create a temporary file" >&2
        Die 3;
    }

    if tail -n +$_SKIP $0 | gzip -cd > $_TMPFILE; then
        if test $_UNPACK = 1; then
        {
            #if _UNPACK is set then be verbose and don't overwrite anything.
               tar --no-same-owner -xkvf $_TMPFILE -C $_UNPACK_DIR;
        }
        else
        {
            tar --no-same-owner -xf $_TMPFILE -C $_UNPACK_DIR;
        }
        fi
    else
        echo $0: Cannot decompress $0 >&2;
        Die 3;
    fi
    
    _RESULT=0;
}

#####################
# Script RunInstaller
#####################
RunInstaller()
{
    
    _INSTALLER_DIR=$1;
    shift;
    
    _START_DIR=$PWD

    trap 'Die $_RESULT' 0  # 0 is EXIT from shell 
    cd $_INSTALLER_DIR
    ln -s $OLDPWD CWD  # create a symbolic link to the previous working directory

    #if a CPINIT script exists, source it in
    if test -f CPINIT; then
    {   
        . CPINIT
    }
    fi

      ./$_INSTALLER ${1+$ARGS}; 
      _RESULT=$?;

    #if a CPFINI script exists, source it in
    if test -f CPFINI; then
    {   
        . CPFINI
    }
    fi

    cd $_START_DIR;
}
    
###############
# Script Validate_RC
###############
Validate_RC()
{
    if [ $_HPSUM -ne 0 ]
    then
        if [ $_ISITOA -ne 0 ]
        then
            return
        fi
    else
        if [ $_ISITOA -ne 0 ]
        then
            _RESULT=$(( ($_RESULT / 20) - 1 ))
        fi
    fi
    if [ $_RESULT -eq 99 ] || [ $_RESULT -lt 0 ] || [ $_RESULT -gt 18 ]
    then
        _RESULT = 99
    fi
    return
}
        

###############
# Script Parse_XML
###############
Parse_XML()
{

    TDIR=$1
    _VERSION=
    _REVISION=
    _FILENAME=
    _COMPONENT=
    _DESCRIPT=
    _ISITOA=0
    _NOLOG=0

    local file="./[cC][Pp]0*.xml"
    local TMP=./tmpfile.txt
    local JUNK

    cd $1 &>/dev/null

    if [ ! -f $file ]
    then
        _NOLOG=1
        cd - &>/dev/null
        return
    fi
    if [ $(egrep -ic "firmware:oa" $file) -gt 0 ] || [ $(egrep -ic "firmware:.oa" $file) -gt 0 ]
    then
        _ISITOA=1
    fi
    echo `grep \<version $file|awk -F = '{print $2}'|awk '{print $1}'|cut -d '"' -f 2` > $TMP
    read _VERSION JUNK < $TMP
    echo `grep \<version $file|awk -F '=' '{print $3}'|awk '{print $1}'|cut -d '"' -f 2` > $TMP
    read _REVISION JUNK < $TMP
    _VERSION=${_VERSION}${_REVISION}
    _FILENAME=`grep -A7 \<installation\> $file|grep \<command_params\>|awk -F '>' '{print $2}'|awk -F '<' '{print $1}'|cut -d '"' -f 2`
    _COMPONENT=`grep \<filename\> $file|awk -F '>' '{print $2}'|awk -F '<' '{print $1}'|cut -d '"' -f 2`
    _DESCRIPT="`grep -m 1 \<alt_name_xlate $file|awk -F '>' '{print $2}'|awk -F '<' '{print $1}'|cut -d '"' -f 2`"


    cd - $>/dev/null
    return
}

###############
# Script InitLog
###############
InitLog()
{
    _LOGFILE=/var/cpq/Component.log

    if [ ! -f $_LOGFILE ];
    then
        if [ ! -d /var/cpq ];
        then
            /bin/mkdir -p /var/cpq
        fi
        touch $_LOGFILE;
    fi

    echo -e "\n=================================================================\n" >>$_LOGFILE
    echo -e "Installation started: `date`\n" >>$_LOGFILE
    if [ $_NOLOG -eq 0 ]
    then
        echo -e "     Installing: $_COMPONENT" >>$_LOGFILE
        echo -e "        Version: $_VERSION" >>$_LOGFILE
        echo -e "    Description: $_DESCRIPT\n\n" >>$_LOGFILE
    fi
    return
}

###############
# Script EndLog
###############
EndLog()
{
    echo -e "\n\n     Return code:    $_RESULT" >>$_LOGFILE
    echo -e "\nInstallation complete: `date`" >>$_LOGFILE
    echo -e "\n=================================================================\n" >>$_LOGFILE
    return
}

###############
# Script Main
###############

# set -x # Uncomment for debug output.

_TMPDIR=/tmp/sctmpdir$$
_TARGET_DIR=$_TMPDIR

#check to see if SCEXE_TMP is defined.
if test -n "$SCEXE_TMP"; then
{    

    if test -d "$SCEXE_TMP"; then
    {
        _TMPDIR="$SCEXE_TMP"/sctmpdir$$
    }
    else
    {
        echo "$0: The path specified by the SCEXE_TMP environment variable is not valid or does not exist";
        Die 3;
    }
    fi
}
fi

_UNPACK=0
_HPSUM=0

while true
do
    case $1 in
                     --)        shift; break                                        ;;
            --interface)        echo $_INTERFACE_VERSION; Die 0                     ;;
               --unpack)        echo "$0: Required parameter missing" >&2; Die 3    ;;
             --unpack=*)        { 
                                    _TARGET_DIR=`echo $1 | cut -b 10- -`;
                                    if test -z $_TARGET_DIR; then
                                    {
                                        echo "$0: Required parameter missing" >&2;
                                        Die 3;
                                    }
                                    else
                                    {
                                        _UNPACK=1;                             
                                    }
                                    fi
                                }
                                break                                               ;;

                  --tmp)        echo "$0: Required parameter missing" >&2; Die 3    ;;
                --tmp=*)        { 
                                    _TMPDIR=`echo $1 | cut -b 7- -`;
                                    shift;
                                    if test -z $_TMPDIR; then
                                    {
                                        echo "$0: Required parameter missing" >&2;
                                        Die 3;
                                    }
                                    fi

                                    if test -d "$_TMPDIR"; then
                                    {
                                        _TMPDIR="$_TMPDIR"/sctmpdir$$
                                    }
                                    else
                                    {
                                        echo "$0: The path specified by the --tmp option is not valid or does not exist" >&2;
                                        Die 3;
                                    }
                                    fi
                                }
                                break                                               ;;
                        *)      break                                               ;;
    esac
done

for argss in $@
do
if [ "$argss" = "-SMPJTB" ] || [ "$argss" = "--SMPJTB" ] || [ "$argss" = "/SMPJTB" ]
then
 _HPSUM=1;
fi
done
#check that all programs we need are present on the path.
_DEPS="tar gzip tail sleep rm ln mkdir mktemp echo"
which $_DEPS > /dev/null;
if test $? != 0 ; then 
    echo "$0: cannot find a required program." >&2;
    echo "Make sure these programs are on the path: $_DEPS" >&2;
    Die 3;
fi

if test $_UNPACK = 1; then
{
    Unpack $_TARGET_DIR
}
else
{
    ARGS=$@
    Unpack $_TMPDIR;
_OSVER=`uname -r | awk '{print substr($0,0,3)}'`
   _OSNAME=`uname -s`

    if [ $_OSNAME == "VMkernel" ]
    then
       if [ $_OSVER == 5.5 ]
       then
          cp -Rf $_UNPACK_DIR/ESXi55/*  $_UNPACK_DIR/
       fi
    fi

if [ $_OSNAME == "VMkernel" ]
    then
       if [ $_OSVER == 6.0 ]
       then
          cp -Rf $_UNPACK_DIR/ESXi60/*  $_UNPACK_DIR/
       fi
    fi

if [ $_OSNAME == "VMkernel" ]
    then
       if [ $_OSVER == 6.5 ]
       then
          cp -Rf $_UNPACK_DIR/ESXi65/*  $_UNPACK_DIR/
       fi
    fi

if [ $_OSNAME == "VMkernel" ]
then
     if [ $_OSVER == 6.7 ]
       then
          cp -Rf $_UNPACK_DIR/ESXi67/*  $_UNPACK_DIR/
       fi
    fi

    Parse_XML $_TMPDIR;
    if [ $_ISITOA -ne 0 ]
    then
        if [ -n "$_VERSION" ]
        then
            ARGS="-version $_VERSION $ARGS"
        fi
        if [ -n "$_FILENAME" ]
        then
            ARGS="-file $_FILENAME $ARGS"
        fi
    fi
    if [ $_ISITOA -eq 0 ]
    then
        InitLog
    fi
    RunInstaller $_TMPDIR $ARGS;
    if test $_HPSUM -eq 0 ; then
        Validate_RC
    fi
    if [ $_ISITOA -eq 0 ]
    then
        EndLog
    fi
    Die $_RESULT;
}
fi

Die $_RESULT;
