#!/bin/sh
# ident "@(#)postinstall	1.8 08/11/04 SMI"

SMCDIR=${PKG_INSTALL_ROOT}/usr/sadm/lib/smc
PRELOAD_DIR=${SMCDIR}/lib/preload
EXEC_BASE=/usr
PKGCOND=/usr/bin/pkgcond

is_srngz=99
is_wrngz=99
if [ -x $PKGCOND ]; then
    $PKGCOND -v is_sparse_root_nonglobal_zone > /dev/null 2>&1
    is_srngz=$?
    $PKGCOND -v is_whole_root_nonglobal_zone > /dev/null 2>&1
    is_wrngz=$?
fi

if [ $is_wrngz -eq 0 ]; then
    EXEC_BASE=$BASEDIR
fi

##
## 'pkgcond' does not exist when you do live upgrade from S8/S9
## so in that case, use 'touch' instead for the testing
##
test_writable() {
    if [ $is_srngz -ne 99 ]; then
        $PKGCOND is_path_writable $1 && return $?
    else
        /usr/bin/touch $1/.test.$$ > /dev/null 2>&1
        if [ $? !=  0 ]; then
            return 1
        else
            rm -f  $1/.test.$$ > /dev/null 2>&1
            return 0
        fi
    fi
}


# the preremove script previously contained a typo - incorrectly creating /usr/dt/appconfing
if [ -s ${PKG_INSTALL_ROOT}/usr/dt/appconfing ]; then
    rm -rf ${PKG_INSTALL_ROOT}/usr/dt/appconfing
fi

# test if the executable dtappintegrate is available  
if [ -x ${PKG_INSTALL_ROOT}/usr/dt/bin/dtappintegrate ]; then
    # it is, check for existence of usr/dt/appconfig and usr/sadm/lib/smc
    if [ -s ${PKG_INSTALL_ROOT}/usr/dt/appconfig -a -s ${SMCDIR} ]; then
        # run the cmd to integrate SMC into CDE
        ${PKG_INSTALL_ROOT}/usr/dt/bin/dtappintegrate -s ${SMCDIR} -t ${PKG_INSTALL_ROOT}/usr/dt/appconfig -l C
    fi
fi

# Check JavaHelp jar file, we need to resolve it's full path
# in the face of symbolic links.  The reason for this is because
# the reference to the jar file may be a symbolic link to the
# the default JavaHelp jar file installed on the system, and we
# must have the real full path to the file so we can add it to
# the console's security policy file.
#
jh_jar=${PRELOAD_DIR}/jh.jar
if [ -h "${jh_jar}" ]; then

    # Must cd to where the symbolic link is located as a starting point
    # to follow the link.
    cd `dirname ${jh_jar}`

    # Resolve link to conclusion
    while [ -h "${jh_jar}" ]
    do
	jh_jar=`ls -al ${jh_jar} | awk '{print $NF}'`
    done

    # Resolve to full path, in case it's relative.
    cd `dirname ${jh_jar}`
    jh_jar=`pwd`/`basename ${jh_jar}`

#   Need path to where link target is installed, but relative to
#   where it is located on the client.
    if [ "${PKG_INSTALL_ROOT}" != "/" ]; then
	jh_jar=`echo ${jh_jar} | sed -e "s@^${PKG_INSTALL_ROOT}@@"`
    fi

    # Now add the jar to the console's security policy file.
    #
    if test_writable ${SMCDIR}/policy/; then
        CONSOLE_POLICY=${SMCDIR}/policy/smcconsole.policy
        chmod 644 ${CONSOLE_POLICY}
        echo "" >> ${CONSOLE_POLICY}
        echo "grant codeBase \"file:${jh_jar}\" {" >> ${CONSOLE_POLICY}
        echo "\tpermission java.security.AllPermission;" >> ${CONSOLE_POLICY}
        echo "};" >> ${CONSOLE_POLICY}
        chmod 444 ${CONSOLE_POLICY}
    fi

    # Delete the bundled classlist file (if it exists), and remove the
    # pkg registration for it.  A classlist will be generated automatically
    # by the SMC server when the new jar is registered.
    #
    JH_CLASSLIST=/usr/sadm/lib/smc/lib/preload/jh_classlist.txt
    if [ -f "${PKG_INSTALL_ROOT}${JH_CLASSLIST}" ]; then
	removef ${PKGINST} ${JH_CLASSLIST} > /dev/null
        if test_writable ${PKG_INSTALL_ROOT}/usr/sadm/lib/smc/lib/preload/; then
	    rm -f ${PKG_INSTALL_ROOT}${JH_CLASSLIST}
	fi
	removef -f ${PKGINST}
    fi
fi

exit 0
