#!/bin/sh
#***************************************************************************
#  Tuning Manager
#  Copyright (C) 2007, Hitachi, Ltd.
#***************************************************************************
#***************************************************************************
#  : install.sh
#  : PP install command
#  : SOLARIS
#***************************************************************************
PATH=/usr/bin:$PATH
export PATH
LANG=C
export LANG
LC_ALL=C
export LC_ALL

OPT1="$1"

AWK_CMD=/usr/bin/awk
BASENAME_CMD=/usr/bin/basename
CAT_CMD=/usr/bin/cat
CP_CMD=/usr/bin/cp
CHMOD_CMD=/usr/bin/chmod
CHOWN_CMD=/usr/bin/chown
DATE_CMD=/usr/bin/date
DIRNAME_CMD=/usr/bin/dirname
ECHO_CMD=/usr/bin/echo
EGREP_CMD=/usr/bin/egrep
GREP_CMD=/usr/bin/grep
ID_CMD=/usr/xpg4/bin/id
MKDIR_CMD=/usr/bin/mkdir
PRINTF_CMD=/usr/bin/printf
PWD_CMD=/usr/bin/pwd
RMDIR_CMD=/usr/bin/rmdir
SED_CMD=/usr/bin/sed
TR_CMD=/usr/bin/tr

PKGADD_CMD=/usr/sbin/pkgadd

FIRST_CHAR=""
FIRST_CHAR=`$ECHO_CMD "$0" | $GREP_CMD "^/"`
if [ -z "$FIRST_CHAR" ] ; then
    $ECHO_CMD "This command must be executed with specifying full path." 1>&2
    exit 1
fi

$PWD_CMD 2> /dev/null 1> /dev/null
RET=$?
if [ $RET -ne 0 ] ; then
    $ECHO_CMD "$PWD_CMD failed( exit code : $RET )." 1>&2
    $ECHO_CMD "The current directory is not correct." 1>&2
    $ECHO_CMD "The installation failed." 1>&2
    exit 1
fi

if [ "`$DIRNAME_CMD "$0"`" = "/" ] ; then
    $ECHO_CMD "You cannot execute \"/install.sh\"." 1>&2
    $ECHO_CMD "The installation failed." 1>&2
    exit 1
fi

JP1PC_HOME=/opt/jp1pc
AGTP=$JP1PC_HOME/agtp
GETPINFO=$JP1PC_HOME/bin/getpinfo
JPCCTRL=$JP1PC_HOME/tools/jpcctrl
JPCSTOP=$JP1PC_HOME/tools/jpcstop
OVSTOP=/opt/OV/bin/ovstop
JPDAT_FILE=$JP1PC_HOME/agte/agent/Japanese.dat

PPNO_KIND=2
LIC_PACKAGE=HTMAgent
INIT_MSG_VERSION_STR=""
START_LOCATION="`$PWD_CMD`"
SCRIPT=`$BASENAME_CMD "$0"`
CD_DIRECTORY=`$ECHO_CMD "$0" | $SED_CMD "s/\/$SCRIPT\$//g"`
BASE_CUR_VR=""
BASE_CUR_VR_VVRR=""
THIS_VR="058000"
THIS_VR_VVRR=`$ECHO_CMD $THIS_VR | $AWK_CMD '{ print substr( $0, 1, 4 ) }'`
PRG_NAME="HiCommand Tuning Manager - Agent"
MGR_NAME="HiCommand Tuning Manager"
if [ $PPNO_KIND -eq 1 ] ; then
    PREMISE_COMMON_VR="0810"
else
    PREMISE_BASE_VR=`$CAT_CMD "$CD_DIRECTORY"/AGTP/SOLARIS/P9D2C/AJ81/pplistd | $AWK_CMD '{ print substr( $0, 25, 6 ) }' | $AWK_CMD '{ print($1) }'`
    PREMISE_MGR_VR="057000"
fi
BASE_COMMON_VR=""
PGMCODE_AGTP="1518"
INSTALL_AGTP=1
# INSTALL_AGTP=1:install Base
# INSTALL_AGTP=0:not install Base
EXEC_USER_ID=""


#####################
#### SUB ROUTINE ####
#####################
ABORT()
{
    if [ -d /var/tmp/htm_install.lck ] ; then
        $RMDIR_CMD /var/tmp/htm_install.lck
        RET=$?
        if [ $RET -ne 0 ] ; then
            $ECHO_CMD "$RMDIR_CMD failed( exit code : $RET ) : /var/tmp/htm_install.lck" 1>&2
        fi
    fi
    $ECHO_CMD "The installation failed." 1>&2
    if [ -d "$START_LOCATION" ] ; then
        cd "$START_LOCATION"
    fi
    exit 1
}

TRAP()
{
    $ECHO_CMD "signal received." 1>&2
    ABORT
}

checkZero()
{
    if [ `$ECHO_CMD "$1" | $EGREP_CMD -c "^.+$"` -eq 0 ] ; then
        return 1
    fi
    return 0
}

FormatVersion()
{
    szVersion=$1
    STRVV=""
    STRR1=""
    STRR2=""
    STRZZ=""
    STRVV=`$ECHO_CMD $szVersion | $AWK_CMD '{ print substr($1,1,2) }' | $AWK_CMD '{printf"%d",$1}'`
    STRR1=`$ECHO_CMD $szVersion | $AWK_CMD '{ print substr($1,3,1) }'`
    STRR2=`$ECHO_CMD $szVersion | $AWK_CMD '{ print substr($1,4,1) }'`
    STRZZ=`$ECHO_CMD $szVersion | $AWK_CMD '{ print substr($1,5,2) }'`
    $ECHO_CMD "$STRVV.$STRR1.$STRR2-$STRZZ"
}

# Sub routine: Version_Check() #######################################################
Version_Check() {
#
#  Version_Check arg1 arg2
#     arg1:This PP VVRRSS        (Current)
#     arg2:Installed PP VVRRSS   (Current)
#  return 0 :new or equal Version
#  return 1 :old Version
#  return 2 :Parameter Error
#


    if [ $# -ne 2 ]; then
        return 2
    fi


    WORKAREA1=`$ECHO_CMD "$1" | $AWK_CMD '{ print substr($1,1,2) }'`
    WORKAREA2=`$ECHO_CMD "$2" | $AWK_CMD '{ print substr($1,1,2) }'`
    if [ "$WORKAREA1" -lt "$WORKAREA2" ] ; then

        return 1

    elif [ "$WORKAREA1" -gt "$WORKAREA2" ] ; then

        return 0
    fi

    WORKAREA1=`$ECHO_CMD "$1" | $AWK_CMD '{ print substr($1,3,2) }'`
    WORKAREA2=`$ECHO_CMD "$2" | $AWK_CMD '{ print substr($1,3,2) }'`
    if [ "$WORKAREA1" -lt "$WORKAREA2" ] ; then

        return 1

    elif [ "$WORKAREA1" -gt "$WORKAREA2" ] ; then

        return 0
    fi

    WORKAREA1=`$ECHO_CMD "$1" | $AWK_CMD '{ if ( $1 ~ /....[\/0-9]./) { print substr($1,5,2) } }'`
    WORKAREA2=`$ECHO_CMD "$2" | $AWK_CMD '{ if ( $1 ~ /....[\/0-9]./) { print substr($1,5,2) } }'`
    if [ "$WORKAREA1" != "" ] && [ "$WORKAREA2" != "" ] ; then
        WORKAREA=`$ECHO_CMD "$WORKAREA1 $WORKAREA2" | $AWK_CMD '{ if( $1 < $2 ) { print $1 } }'`
        if [ "$WORKAREA" = "$WORKAREA1" ] ; then
            return 1
        fi

        WORKAREA=`$ECHO_CMD "$WORKAREA1 $WORKAREA2" | $AWK_CMD '{ if( $1 > $2 ) { print $1 } }'`
        if [ "$WORKAREA" = "$WORKAREA1" ] ; then
            return 0
        fi
    else
        if [ "$WORKAREA1" = "" ] && [ "$WORKAREA2" != "" ] ; then
            return 1
        fi

        if [ "$WORKAREA1" != "" ] && [ "$WORKAREA2" = "" ] ; then
            return 0
        fi
    fi

    return 0

}

PRE_PROC()
{
    if [ $PPNO_KIND -eq 3 ] ; then
        # 51000023
        if [ -d $JP1PC_HOME ] ; then
            if [ -f $JP1PC_HOME/inst.ini ] ; then
                if [ ! -f $JP1PC_HOME/HPTunMgr.dat ] ; then
                    $ECHO_CMD ">       Installation of this product stops because there is a product that cannot coexist with it." 1>&2
                    return 1
                fi
            fi
        fi
    else
        # 51000024
        if [ -f $JP1PC_HOME/HPTunMgr.dat ] ; then
            $ECHO_CMD ">       Installation of this product stops because there is a product that cannot coexist with it." 1>&2
            return 1
        fi
    fi

    if [ $PPNO_KIND -eq 2 ] ; then
        if [ -f $JP1PC_HOME/inst.ini ] ; then
            if [ ! -f $JP1PC_HOME/TunMgr.dat ] ; then
                if [ -f $JP1PC_HOME/instagtp.ini ] ; then
                    $ECHO_CMD ">       Installation of this product stops because there is a product that cannot coexist with it." 1>&2
                    return 1
                fi
                if [ -f $JP1PC_HOME/instview.ini ] ; then
                    $ECHO_CMD ">       Installation of this product stops because there is a product that cannot coexist with it." 1>&2
                    return 1
                fi
            fi
        fi
        # 51000051 Exclusive install
        if [ -f $JPDAT_FILE ] ; then
            $ECHO_CMD ">       Installation of this product stops because there is a product that cannot coexist with it." 1>&2
            return 1
        fi
    elif [ $PPNO_KIND -eq 1 ] ; then
        if [ -f $JP1PC_HOME/inst.ini ] ; then
            if [ -f $JP1PC_HOME/TunMgr.dat ] ; then
                $ECHO_CMD ">       Installation of this product stops because there is a product that cannot coexist with it." 1>&2
                return 1
            fi
        fi
        # 51000051 Exclusive install
        if [ -f $JP1PC_HOME/instagte.ini ] ; then
            if [ ! -f $JPDAT_FILE ] ; then
                $ECHO_CMD ">       Installation of this product stops because there is a product that cannot coexist with it." 1>&2
                return 1
            fi
        fi
    fi

    if [ $PPNO_KIND -ne 1 ] ; then
        # 40000335
        if [ -f $JP1PC_HOME/instagtu.ini -a ! -f $JP1PC_HOME/instagte.ini ] ; then
            $ECHO_CMD ">     $PRG_NAME $INIT_MSG_VERSION_STR cannot be installed, because the following products are installed." 1>&2
            $ECHO_CMD ">" 1>&2
            $ECHO_CMD ">       program ID   : `$GREP_CMD ProductID $JP1PC_HOME/instagtu.ini | $AWK_CMD -F= '{print $2}'`" 1>&2
            $ECHO_CMD ">       program name : `$GREP_CMD ProgramName $JP1PC_HOME/instagtu.ini | $AWK_CMD -F= '{print $2}'`" 1>&2
            $GREP_CMD "LogicalVersion=" $JP1PC_HOME/instagtu.ini 1> /dev/null
            if [ $? -eq 0 ] ; then
                $ECHO_CMD ">       version      : `$GREP_CMD LogicalVersion $JP1PC_HOME/instagtu.ini | $AWK_CMD -F= '{print $2}'`" 1>&2
            else
                $ECHO_CMD ">       version      : `$GREP_CMD EXCurrentVersion $JP1PC_HOME/instagtu.ini | $AWK_CMD -F= '{print $2}'`" 1>&2
            fi
            return 1
        fi
    fi

    if [ $PPNO_KIND -eq 1 ] ; then
        if [ ! -f $JP1PC_HOME/instagtp.ini ] ; then
            if [ ! -f $JP1PC_HOME/instmgr.ini ] ; then
                $ECHO_CMD ">     The prerequisite product Performance Management - Base [$PREMISE_COMMON_VR] or Manager [$PREMISE_COMMON_VR] is not installed." 1>&2
                return 1
            else
                BASE_COMMON_VR=`$CAT_CMD $JP1PC_HOME/instmgr.ini | $GREP_CMD EXCurrentVersion | $AWK_CMD -F= '{print $2}'`
                Version_Check $BASE_COMMON_VR $PREMISE_COMMON_VR
                RET=$?
                case $RET in
                    0)
                        $ECHO_CMD ">     The prerequisite product Performance Management - Manager is installed." 1>&2
                        $ECHO_CMD ">     A current version [$BASE_COMMON_VR]" 1>&2
                        $ECHO_CMD ">     A premise version [$PREMISE_COMMON_VR]" 1>&2
                        ;;
                    1)
                        $ECHO_CMD ">     The prerequisite product Performance Management - Manager [$PREMISE_COMMON_VR] is not installed." 1>&2
                        $ECHO_CMD ">     A current version [$BASE_COMMON_VR]" 1>&2
                        $ECHO_CMD ">     A premise version [$PREMISE_COMMON_VR]" 1>&2
                        return 1
                        ;;
                    2)
                        $ECHO_CMD ">     The environment is invalid." 1>&2
                        return 1
                        ;;
                esac
            fi
        else
            BASE_COMMON_VR=`$CAT_CMD $JP1PC_HOME/instagtp.ini | $GREP_CMD EXCurrentVersion | $AWK_CMD -F= '{print $2}'`
            Version_Check $BASE_COMMON_VR $PREMISE_COMMON_VR
            RET=$?
            case $RET in
                0)
                    $ECHO_CMD ">     The prerequisite product Performance Management - Base is installed." 1>&2
                    $ECHO_CMD ">     A current version [$BASE_COMMON_VR]" 1>&2
                    $ECHO_CMD ">     A premise version [$PREMISE_COMMON_VR]" 1>&2
                    ;;
                1)
                    $ECHO_CMD ">     The prerequisite product Performance Management - Base [$PREMISE_COMMON_VR] is not installed." 1>&2
                    $ECHO_CMD ">     A current version [$BASE_COMMON_VR]" 1>&2
                    $ECHO_CMD ">     A premise version [$PREMISE_COMMON_VR]" 1>&2
                    return 1
                    ;;
                2)
                    $ECHO_CMD ">     The environment is invalid." 1>&2
                    return 1
                    ;;
            esac
        fi
    else
        if [ -f $JP1PC_HOME/instagtp.ini ] ; then
            BASE_COMMON_VR=`$CAT_CMD $JP1PC_HOME/instagtp.ini | $GREP_CMD EXCurrentVersion | $AWK_CMD -F= '{print $2}'`
            Version_Check $PREMISE_BASE_VR $BASE_COMMON_VR
            RET=$?
            case $RET in
                0)
                    $ECHO_CMD ">     A current version [$BASE_COMMON_VR]" 1>&2
                    $ECHO_CMD ">     A premise version [$PREMISE_BASE_VR]" 1>&2
                    INSTALL_AGTP=1
                    ;;
                1)
                    $ECHO_CMD ">     A current version [$BASE_COMMON_VR]" 1>&2
                    $ECHO_CMD ">     A premise version [$PREMISE_BASE_VR]" 1>&2
                    INSTALL_AGTP=0
                    ;;
                2)
                    $ECHO_CMD ">     The environment is invalid." 1>&2
                    return 1
                    ;;
            esac
        elif [ -f $JP1PC_HOME/insthtmclmgr.ini ] ; then
            BASE_COMMON_VR=`$CAT_CMD $JP1PC_HOME/insthtmclmgr.ini | $GREP_CMD EXCurrentVersion | $AWK_CMD -F= '{print $2}'`
            Version_Check $BASE_COMMON_VR $PREMISE_MGR_VR
            RET=$?
            case $RET in
                0)
                    $ECHO_CMD ">     The prerequisite product $MGR_NAME is installed." 1>&2
                    $ECHO_CMD ">     A current version [$BASE_COMMON_VR]" 1>&2
                    $ECHO_CMD ">     A premise version [$PREMISE_MGR_VR]" 1>&2
                    INSTALL_AGTP=0
                    ;;
                1)
                    $ECHO_CMD ">     The prerequisite product $MGR_NAME [`FormatVersion $PREMISE_MGR_VR`] is not installed." 1>&2
                    $ECHO_CMD ">     A current version [$BASE_COMMON_VR]" 1>&2
                    $ECHO_CMD ">     A premise version [$PREMISE_MGR_VR]" 1>&2
                    return 1
                    ;;
                2)
                    $ECHO_CMD ">     The environment is invalid." 1>&2
                    return 1
                    ;;
            esac
        elif [ ! -f $JP1PC_HOME/insthtmclmgr.ini -a -f $JP1PC_HOME/instmgr.ini ] ; then
            $ECHO_CMD ">     The prerequisite product $MGR_NAME [`FormatVersion $PREMISE_MGR_VR`] is not installed." 1>&2
            return 1
        fi
    fi

    if [ $PPNO_KIND -ne 1 -a "$OPT1" != "-s" ] ; then
        if [ ! -f $JP1PC_HOME/instagtp.ini -a ! -f $JP1PC_HOME/insthtmclmgr.ini ] ; then

            $PRINTF_CMD "\n$MGR_NAME cannot be installed after you install this product. "
            $PRINTF_CMD "If you want to install $MGR_NAME and this product on the same host, you must install $MGR_NAME first.\n"

            $PRINTF_CMD "Note that when you uninstall this product, product versions of all other "
            $PRINTF_CMD "Agents on the same host must be 5.7 or later. To uninstall this product, "
            $PRINTF_CMD "upgrade the Agents to version 5.7 or later, or uninstall the Agents whose "
            $PRINTF_CMD "versions are 5.5 or earlier before uninstalling this product.\n"

            $PRINTF_CMD "\nDo you want to continue the installation? \n"
            while [ 1 ];
            do
                USER_RESPONSE=""
                $PRINTF_CMD "To continue, enter Y. (Default Y) Y/N > "
                read USER_RESPONSE
                checkZero "$USER_RESPONSE"
                ISZERO=$?
                if [ $ISZERO -eq 1 ] ; then
                    break
                fi
                case "${USER_RESPONSE}" in
                    [Yy])
                        break
                        ;;
                    [Nn])
                        if [ -d /var/tmp/htm_install.lck ] ; then
                            $RMDIR_CMD /var/tmp/htm_install.lck
                            RET=$?
                            if [ $RET -ne 0 ] ; then
                                $ECHO_CMD "$RMDIR_CMD failed( exit code : $RET ) : /var/tmp/htm_install.lck" 1>&2
                            fi
                        fi
                        if [ -d "$START_LOCATION" ] ; then
                            cd "$START_LOCATION"
                        fi
                        exit 0
                        ;;
                esac
            done
        fi
    fi

    # Version_Check
    if [ -f $JP1PC_HOME/instagte.ini ] ; then
        BASE_CUR_VR=`$CAT_CMD $JP1PC_HOME/instagte.ini | $GREP_CMD EXCurrentVersion | $AWK_CMD -F= '{print $2}'`
        BASE_CUR_VR_VVRR=`$ECHO_CMD $BASE_CUR_VR | $AWK_CMD '{ print substr( $0, 1, 4 ) }'`
        Version_Check $THIS_VR_VVRR $BASE_CUR_VR_VVRR
        RET=$?
        case $RET in
            0)
                $ECHO_CMD ">     Install version ok [ $BASE_CUR_VR -> $THIS_VR ]" 1>&2
                ;;
            1)
                $ECHO_CMD ">     A newer version of $PRG_NAME is already installed on this system." 1>&2
                $ECHO_CMD ">     You cannot downgrade $PRG_NAME." 1>&2
                $ECHO_CMD "> ##############################################################################" 1>&2
                return 1
                ;;
            2)
                $ECHO_CMD ">     Parameter Error( Version Check )" 1>&2
                ;;
        esac
    else
        $ECHO_CMD ">     Install version ok [ $THIS_VR ]" 1>&2
    fi

    if [ -x $JPCCTRL ] ; then
        if [ -x $GETPINFO ] ; then
            for TMPLHOST in `$GETPINFO H all | $TR_CMD "," "\n"`
            do
                $JPCCTRL confirm "?*" lhost=$TMPLHOST -nolog 1>&2
                RET=$?
                case $RET in
                    0)
                        $ECHO_CMD ">     The service processes are not stopped." 1>&2
                        return 1
                        ;;
                    1)
                        $ECHO_CMD ">     An argument is invalid." 1>&2
                        return 1
                        ;;
                    3)
                        $ECHO_CMD ">     The service processes are not launched." 1>&2
                        ;;
                    100)
                        $ECHO_CMD ">     An attempt to access this directory failed." 1>&2
                        ;;
                    101)
                        $ECHO_CMD ">     The NameServer port number cannot be acquired from the 'services' file." 1>&2
                        return 1
                        ;;
                    222)
                        $ECHO_CMD ">     An IP address cannot be resolved." 1>&2
                        return 1
                        ;;
                    *)
                        $ECHO_CMD ">     An error occurred." 1>&2
                        return 1
                        ;;
                esac
            done
        else
            $ECHO_CMD ">     $GETPINFO: No such file exists or permission was denied." 1>&2
        fi

        # SHUTDOWN ALL HTM PROCESSES
        $ECHO_CMD ">     Shutdown all Tuning Manager processes." 1>&2
        $JPCCTRL confirm "?*" -nolog 1>&2
        RET=$?
        if [ $RET -ne 3 ] ; then
            if [ -x $JPCSTOP ] ; then
                $ECHO_CMD "     The service will now stop." 1>&2
                $JPCSTOP all "kill=180,1" 1>&2
                RET=$?
                case $RET in
                1)
                    $JPCSTOP all 1>&2
                    sleep 30
                    ;;
                10)
                    $ECHO_CMD ">     $JPCSTOP: Failed." 1>&2
                    return 1
                    ;;
                *)
                    sleep 15
                    ;;
                esac
                $ECHO_CMD ">     $JPCSTOP: Completed." 1>&2
            else
                $ECHO_CMD ">     $JPCSTOP: No such file exists or permission was denied." 1>&2
                return 1
            fi
        else
            $ECHO_CMD ">     The service processes are not launched." 1>&2
        fi
    else
        $ECHO_CMD ">     $JPCCTRL: No such file exists or permission was denied." 1>&2
    fi

    # Stop NNM services
    $ECHO_CMD ">     Stop NNM services." 1>&2
    if [ -x $OVSTOP ] ; then
        $ECHO_CMD ">     JPCObjMgr service will now stop." 1>&2
        $OVSTOP JPCObjMgr 1>&2
        RET=$?
        if [ $RET -eq 0 ] ; then
            $ECHO_CMD ">     $OVSTOP JPCObjMgr: Completed." 1>&2
        else
            $ECHO_CMD ">     $OVSTOP JPCObjMgr: Failed." 1>&2
        fi
    else
        $ECHO_CMD ">     $OVSTOP: No such file exists or permission was denied." 1>&2
    fi

    return 0
}

POST_PROC()
{
    if [ $PPNO_KIND -eq 1 ] ; then
        $CP_CMD -pR "$CD_DIRECTORY"/doc $JP1PC_HOME/agte/
        RET=$?
        if [ $RET -eq 0 ] ; then
            $ECHO_CMD ">     Created: $JP1PC_HOME/agte/doc" 1>&2
        else
            $ECHO_CMD ">     Copying failed( exit code : $RET ): $JP1PC_HOME/agte/doc" 1>&2
            return 1
        fi
        $CHOWN_CMD -R root:sys $JP1PC_HOME/agte/doc
        $CHMOD_CMD -R 444 $JP1PC_HOME/agte/doc
        $CHMOD_CMD 755 $JP1PC_HOME/agte/doc
        $CHMOD_CMD 755 $JP1PC_HOME/agte/doc/ja
        $CHMOD_CMD 755 $JP1PC_HOME/agte/doc/ja/pcae
        $CHMOD_CMD 755 $JP1PC_HOME/agte/doc/ja/pcae/figure
    fi
    return 0
}

# -------------------------------------------------------------------
# PUT_PRODUCTTYPE_FILE                                               
# -------------------------------------------------------------------
PUT_PRODUCTTYPE_FILE()
{
    if [ ! -d /opt ] ; then
        $MKDIR_CMD /opt
        RET=$?
        if [ $RET -ne 0 ] ; then
            $ECHO_CMD "$MKDIR_CMD failed( exit code : $RET ) : /opt" 1>&2
            ABORT
        fi
        $ECHO_CMD ">     $MKDIR_CMD /opt: Completed." 1>&2
        $CHOWN_CMD root:sys /opt
        $CHMOD_CMD 755 /opt
    fi

    if [ ! -d /opt/jp1pc ] ; then
        $MKDIR_CMD /opt/jp1pc
        RET=$?
        if [ $RET -ne 0 ] ; then
            $ECHO_CMD "$MKDIR_CMD failed( exit code : $RET ) : /opt/jp1pc" 1>&2
            ABORT
        fi
        $ECHO_CMD ">     $MKDIR_CMD /opt/jp1pc: Completed." 1>&2
        $CHOWN_CMD root:sys /opt/jp1pc
        $CHMOD_CMD 755 /opt/jp1pc
    fi

    if [ $PPNO_KIND -ne 3 ] ; then
        $CP_CMD -p "$CD_DIRECTORY"/TunMgr.dat /opt/jp1pc
        RET=$?
        if [ $RET -ne 0 ] ; then
            $ECHO_CMD "$CP_CMD failed( exit code : $RET ) : TunMgr.dat" 1>&2
            ABORT
        fi
        $CHOWN_CMD root:sys $JP1PC_HOME/TunMgr.dat
        $CHMOD_CMD 444 $JP1PC_HOME/TunMgr.dat
    else
        $CP_CMD -p "$CD_DIRECTORY"/HPTunMgr.dat /opt/jp1pc
        RET=$?
        if [ $RET -ne 0 ] ; then
            $ECHO_CMD "$CP_CMD failed( exit code : $RET ) : HPTunMgr.dat" 1>&2
            ABORT
        fi
        $CHOWN_CMD root:sys $JP1PC_HOME/HPTunMgr.dat
        $CHMOD_CMD 444 $JP1PC_HOME/HPTunMgr.dat
    fi
}

##############
#### MAIN ####
##############
trap ' TRAP ' 1 2 15

EXEC_USER_ID=`$ID_CMD -u`
if [ "$EXEC_USER_ID" != "0" ] ; then
    $ECHO_CMD "You do not have permission to execute install.sh." 1>&2
    ABORT
fi

cd "$CD_DIRECTORY"
$PWD_CMD | $GREP_CMD -v [\^/] 2> /dev/null 1> /dev/null
RET=$?
if [ $RET -eq 0 ] ; then
    $ECHO_CMD "You cannot execute \"/install.sh\"." 1>&2
    ABORT
fi

if [ ! -f dontask ] ; then
    $ECHO_CMD "Not found dontask." 1>&2
    ABORT
fi
if [ ! -d $LIC_PACKAGE ] ; then
    $ECHO_CMD "Not found $LIC_PACKAGE." 1>&2
    ABORT
fi
if [ $PPNO_KIND -ne 1 ] ; then
    if [ ! -x AGTP/SOLARIS/setup ] ; then
        $ECHO_CMD "./AGTP/SOLARIS/setup: No such file exists or permission was denied." 1>&2
        ABORT
    fi
fi

if [ $PPNO_KIND -ne 1 ] ; then
    VV=`$ECHO_CMD $THIS_VR | $AWK_CMD '{ print substr($1,1,2) }' | $AWK_CMD '{printf"%d",$1}'`
    R1=`$ECHO_CMD $THIS_VR | $AWK_CMD '{ print substr($1,3,1) }'`
    R2=`$ECHO_CMD $THIS_VR | $AWK_CMD '{ print substr($1,4,1) }'`
    ZZ=`$ECHO_CMD $THIS_VR | $AWK_CMD '{ print substr($1,5,2) }'`
    INIT_MSG_VERSION_STR="$VV.$R1.$R2($VV.$R1.$R2-$ZZ)"
else
    VV=`$ECHO_CMD $THIS_VR | $AWK_CMD '{ print substr($1,1,2) }'`
    RR=`$ECHO_CMD $THIS_VR | $AWK_CMD '{ print substr($1,3,2) }'`
    ZZ=`$ECHO_CMD $THIS_VR | $AWK_CMD '{ print substr($1,5,2) }'`
    INIT_MSG_VERSION_STR="$VV-$RR-$ZZ"
fi
$ECHO_CMD "Begin installation of $PRG_NAME - $INIT_MSG_VERSION_STR" 1>&2
$DATE_CMD

if [ ! -d /var ] ; then
    $MKDIR_CMD /var
    RET=$?
    if [ $RET -ne 0 ] ; then
        $ECHO_CMD "$MKDIR_CMD failed( exit code : $RET ) : /var" 1>&2
        ABORT
    fi
    $ECHO_CMD ">     $MKDIR_CMD /var: Completed." 1>&2
    $CHOWN_CMD root:sys /var
    $CHMOD_CMD 755 /var
fi
if [ ! -d /var/tmp ] ; then
    $MKDIR_CMD /var/tmp
    RET=$?
    if [ $RET -ne 0 ] ; then
        $ECHO_CMD "$MKDIR_CMD failed( exit code : $RET ) : /var/tmp" 1>&2
        ABORT
    fi
    $ECHO_CMD ">     $MKDIR_CMD /var/tmp: Completed." 1>&2
    $CHOWN_CMD root:sys /var/tmp
    $CHMOD_CMD 1777 /var/tmp
fi
if [ ! -d /var/tmp/htm_install.lck ] ; then
    $MKDIR_CMD /var/tmp/htm_install.lck
    RET=$?    
    if [ $RET -ne 0 ] ; then
        $ECHO_CMD "$MKDIR_CMD failed( exit code : $RET ) : /var/tmp/htm_install.lck" 1>&2
        ABORT
    fi
fi

##################################################################
#### Processing from preprocessing script.(checkinstall etc)
PRE_PROC
RET=$?
if [ $RET -ne 0 ] ; then
    ABORT
fi
##################################################################

if [ $PPNO_KIND -ne 1 ] ; then
    PUT_PRODUCTTYPE_FILE
fi

if [ $PPNO_KIND -ne 1 ] ; then
    if [ $INSTALL_AGTP -eq 1 ] ; then
        cd AGTP/SOLARIS
        $ECHO_CMD "Installing The Common Files." 1>&2
        ./setup -f -c $PGMCODE_AGTP ../ 2> /dev/null 1> /dev/null
        RET=$?
        if [ $RET -ne 0 ] ; then
            $ECHO_CMD "./setup failed( exit code : $RET )." 1>&2
            $ECHO_CMD "The Common Files install failed." 1>&2
            ABORT
        fi
    fi
fi

$PKGADD_CMD -a "$CD_DIRECTORY/dontask" -d "$CD_DIRECTORY" $LIC_PACKAGE
CMD_RET=$?
if [ $CMD_RET -ne 0 ] ; then
    $ECHO_CMD "$PKGADD_CMD failed( exit code : $CMD_RET )." 1>&2
    ABORT
fi

##################################################################
# Processing from postprocessing script.(postinstall etc)
POST_PROC
RET=$?
if [ $RET -ne 0 ] ; then
    ABORT
fi
##################################################################

if [ -d "$START_LOCATION" ] ; then
    cd "$START_LOCATION"
fi

if [ -d /var/tmp/htm_install.lck ] ; then
    $RMDIR_CMD /var/tmp/htm_install.lck
    RET=$?
    if [ $RET -ne 0 ] ; then
        $ECHO_CMD "$RMDIR_CMD failed( exit code : $RET ) : /var/tmp/htm_install.lck" 1>&2
    fi
fi
$DATE_CMD
$ECHO_CMD "Done installing $PRG_NAME." 1>&2
exit 0

