#!/bin/sh
#ident "@(#)preremove	1.16	04/24/06 SMI"

# The default console instance name, configuration directory, and
# configuration file name.  These are hard-coded well-known values!
INSTANCE=console
CONSOLE_CONFDIR=$BASEDIR/etc/webconsole
CONFIG_FILE=config.properties

OS=`uname -s`
if [ ! -n "${OS}" ]; then
    echo "Unable to determine operating system."
    exit 1
fi

if [ "${OS}" != "SunOS" -a "${OS}" != "Linux" -a "${OS}" != "HP-UX" -a "${OS}" != "AIX" ]; then
    echo "Not supported on detected OS \"${OS}\"."
    exit 1
fi

if [ "${OS}" != "SunOS" ]; then
    CONSOLE_CONFDIR=$BASEDIR/etc/opt/webconsole
fi

if [ "${OS}" = "Linux" ]; then
    /sbin/chkconfig --del webconsole > /dev/null
fi

# Get path to our console instance configuration properties file.
# If it exists, source it to get remaining paths
PROPFILE=${CONSOLE_CONFDIR}/${INSTANCE}/${CONFIG_FILE}
if [ ! -f "${PROPFILE}" ]; then
    # Warn, but exit gracefully so pkg can be removed.
    echo "Cannot find console configuration file - no cleanup."
    exit 0
fi
. ${PROPFILE}
CONSOLE_DOMBASE=$BASEDIR/${console_dombase}/${INSTANCE}
CONSOLE_LOGBASE=$BASEDIR/${console_log}/${INSTANCE}
CONSOLE_BASE=$BASEDIR/${console_base}
CONSOLE_PREREG_DIR=$BASEDIR/${console_conf}/${console_instance}/prereg/console

# Note that we do not remove the APPBASE directory as it
# may contain copied legacy web applications.  These must be
# left in place when upgrading from 3.0 as the legacy regnot
# files are pointing to this directory.  These directories
# are cleaned up when the legacy web app calls smreg remove.

# Remove the default console instance domain
# Brute force method - destroy the domain subdirectory!
if [ -d ${CONSOLE_DOMBASE} ]; then
    rm -rf ${CONSOLE_DOMBASE}
fi

# Remove the default console instance log files
# Brute force method - destroy the log subdirectory!
if [ -d ${CONSOLE_LOGBASE} ]; then
    rm -rf ${CONSOLE_LOGBASE}
fi

# Remove the default console registration notification
# files, only remove console registration notification
# app regnots should remain for future upgrades
if [ -d ${CONSOLE_PREREG_DIR} ]; then
    rm -rf ${CONSOLE_PREREG_DIR}
fi

# Remove misc files and subdirs for the default console instance.
# Leave the registration notification files - needed for upgrade.
# Remove console registration notification files, however.
CACHEDIR=${CONSOLE_CONFDIR}/${INSTANCE}/regcache
if [ -d ${CACHEDIR} ]; then
    rm -rf ${CACHEDIR}
fi
STATFILE=${CONSOLE_CONFDIR}/${INSTANCE}/status.properties
if [ -f ${STATFILE} ]; then
    rm -f ${STATFILE}
fi
CONS_REGDIR=${CONSOLE_CONFDIR}/${INSTANCE}/prereg/console
if [ -d ${CONS_REGDIR} ]; then
    rm -rf ${CONS_REGDIR}/prereg/console
fi
rm -f ${CONSOLE_CONFDIR}/${INSTANCE}/*.bkp

# Remove any temporary directory files
# Brute force method - destroy the tmp subdirectory!
if [ -d ${CONSOLE_BASE}/tmp ]; then
    rm -rf ${CONSOLE_BASE}/tmp
fi

# Clean up legacy web application registration datastores.
# They can be removed if empty.
PLUGINS=${CONSOLE_BASE}/registered_plugins
JARS=${CONSOLE_BASE}/registered_jars
MODULES=${CONSOLE_BASE}/registered_modules

# Remove the list of registered plugins
# if there are no apps registered in it
if [ -f ${PLUGINS} ]; then
    TMP=`cat ${PLUGINS} \
        | sed -e 's/    / /g' -e 's/^ //g' -e '/^#/d' -e 's/ //g'`

    if [ "$TMP" = "" ]; then
        rm -f ${PLUGINS}
    fi
fi

# Remove the list of registered jars
# if there are no jars registered in it
if [ -f ${JARS} ]; then
    TMP=`cat ${JARS} \
        | sed -e 's/    / /g' -e 's/^ //g' -e '/^#/d' -e 's/ //g'`

    if [ "$TMP" = "" ]; then
        rm -f ${JARS}
    fi
fi

# Remove the list of registered modules
# if there are no modules registered in it
if [ -f ${MODULES} ]; then
    TMP=`cat ${MODULES} \
        | sed -e 's/    / /g' -e 's/^ //g' -e '/^#/d' -e 's/ //g'`

    if [ "$TMP" = "" ]; then
        rm -f ${MODULES}
    fi
fi

exit 0
