#! /usr/bin/ksh
# Name:         postpatch
# Title:
# Version:
# Revision: 
# Update Date: 
# Programmer:   
# Documents:
#
#Description Post patch installation script
#

if [ $ROOTDIR = "/" ]; then
  BASEDIR=`pkgparam SUNWsmgr BASEDIR`
else
  BASEDIR=`pkgparam -R ${ROOTDIR} SUNWsmgr BASEDIR`
fi

#remove the leading path if ROOTDIR not other than /
if [ $ROOTDIR = "/" ]; then
  ROOTDIR=""
fi

RUN_LEV=`who -r | awk '{print $3}'`
 
# If RUN_LEV is null set it to unknown - 
RUN_LEV=${RUN_LEV:=unknown}

if [ $RUN_LEV != "2" ] ; then
    #This is a non-jumpstart install
    echo "#! /usr/bin/ksh" > /tmp/inst_SUNWsmgr
    echo "" >> /tmp/inst_SUNWsmgr
    echo "JAVAPATH=${ROOTDIR}/usr/opt/SUNWsmgr/jre/bin/java" >> /tmp/inst_SUNWsmgr
    echo "" >> /tmp/inst_SUNWsmgr
    echo "PKG_INSTALL_ROOT=${ROOTDIR}" >> /tmp/inst_SUNWsmgr
    cat ${BASEDIR}/usr/opt/SUNWsmgr/bin/esm_smgr >> /tmp/inst_SUNWsmgr
    cp /tmp/inst_SUNWsmgr ${BASEDIR}/usr/opt/SUNWsmgr/bin/esm_smgr
    rm /tmp/inst_SUNWsmgr

    removef SUNWsmgr ${ROOTDIR}/usr/opt/SUNWsmgr/bin/esm_smgr
    installf SUNWsmgr ${ROOTDIR}/usr/opt/SUNWsmgr/bin/esm_smgr e 0755 root bin

    /usr/bin/diff ${ROOTDIR}/usr/opt/SUNWsmgr/app.dat ${ROOTDIR}/usr/opt/SUNWsmgr/apptmp.dat >/dev/null 2>&1
    status=$?
    if [ "$status" -ne 0 ] ; then
        #app.dat file is different, so clean up the temp files.  Leave app.dat alone. 
        if [ ! -e ${ROOTDIR}/usr/opt/SUNWsmgr/app.dat ]; then
            mv ${ROOTDIR}/usr/opt/SUNWsmgr/temp/app.dat ${ROOTDIR}/usr/opt/SUNWsmgr/app.dat
            installf SUNWsmgr ${ROOTDIR}/usr/opt/SUNWsmgr/app.dat v 0644 root bin
        fi
        rm ${ROOTDIR}/usr/opt/SUNWsmgr/apptmp.dat
        rm ${ROOTDIR}/usr/opt/SUNWsmgr/temp/app.dat
        rmdir ${ROOTDIR}/usr/opt/SUNWsmgr/temp
    else
        #app.dat files are the same, so we must replace it with the new one.
        rm ${ROOTDIR}/usr/opt/SUNWsmgr/app.dat
        rm ${ROOTDIR}/usr/opt/SUNWsmgr/apptmp.dat
        mv ${ROOTDIR}/usr/opt/SUNWsmgr/temp/app.dat ${ROOTDIR}/usr/opt/SUNWsmgr/app.dat
        rmdir ${ROOTDIR}/usr/opt/SUNWsmgr/temp
        installf SUNWsmgr ${ROOTDIR}/usr/opt/SUNWsmgr/app.dat v 0644 root bin
    fi 
    removef SUNWsmgr ${ROOTDIR}/usr/opt/SUNWsmgr/apptmp.dat
    removef SUNWsmgr ${ROOTDIR}/usr/opt/SUNWsmgr/temp/app.dat
    removef SUNWsmgr ${ROOTDIR}/usr/opt/SUNWsmgr/temp

    chmod 0755 ${ROOTDIR}/usr/opt/SUNWsmgr/bin/esm_smgr

    echo "Now you can start the GUI by typing :
    ${ROOTDIR}/usr/opt/SUNWsmgr/bin/esm_smgr"
else
    #This appears to be a jumpstart install
    echo "#! /usr/bin/ksh" > /tmp/inst_SUNWsmgr
    echo "" >> /tmp/inst_SUNWsmgr
    echo "JAVAPATH=/usr/opt/SUNWsmgr/jre/bin/java" >> /tmp/inst_SUNWsmgr
    echo "" >> /tmp/inst_SUNWsmgr
    echo "PKG_INSTALL_ROOT=" >> /tmp/inst_SUNWsmgr
    cat $BASEDIR/usr/opt/SUNWsmgr/bin/esm_smgr >> /tmp/inst_SUNWsmgr
    cp /tmp/inst_SUNWsmgr $BASEDIR/usr/opt/SUNWsmgr/bin/esm_smgr
    rm /tmp/inst_SUNWsmgr

    removef SUNWsmgr $BASEDIR/usr/opt/SUNWsmgr/bin/esm_smgr
    installf SUNWsmgr $BASEDIR/usr/opt/SUNWsmgr/bin/esm_smgr e 0755 root bin

    /usr/bin/diff $BASEDIR/usr/opt/SUNWsmgr/app.dat $BASEDIR/usr/opt/SUNWsmgr/apptmp.dat >/dev/null 2>&1
    status=$?
    if [ "$status" -ne 0 ] ; then
        #app.dat file is different, so clean up the temp files.  Leave app.dat alone. 
        if [ ! -e $BASEDIR/usr/opt/SUNWsmgr/app.dat ]; then
            mv $BASEDIR/usr/opt/SUNWsmgr/temp/app.dat $BASEDIR/usr/opt/SUNWsmgr/app.dat
            installf SUNWsmgr $BASEDIR/usr/opt/SUNWsmgr/app.dat v 0644 root bin
        fi
        rm $BASEDIR/usr/opt/SUNWsmgr/apptmp.dat
        rm $BASEDIR/usr/opt/SUNWsmgr/temp/app.dat
        rmdir $BASEDIR/usr/opt/SUNWsmgr/temp
    else
        #app.dat files are the same, so we must replace it with the new one.
        rm $BASEDIR/usr/opt/SUNWsmgr/app.dat
        rm $BASEDIR/usr/opt/SUNWsmgr/apptmp.dat
        mv $BASEDIR/usr/opt/SUNWsmgr/temp/app.dat $BASEDIR/usr/opt/SUNWsmgr/app.dat
        rmdir $BASEDIR/usr/opt/SUNWsmgr/temp
        installf SUNWsmgr $BASEDIR/usr/opt/SUNWsmgr/app.dat v 0644 root bin

    fi 
    removef SUNWsmgr $BASEDIR/usr/opt/SUNWsmgr/apptmp.dat
    removef SUNWsmgr $BASEDIR/usr/opt/SUNWsmgr/temp/app.dat
    removef SUNWsmgr $BASEDIR/usr/opt/SUNWsmgr/temp

    chmod 0755 $BASEDIR/usr/opt/SUNWsmgr/bin/esm_smgr

    echo "It appears that you may have done a jumpstart install.
    Please inspect the /usr/opt/SUNWsmgr/bin/esm_smgr script before running."
fi
