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

# Write wcremove command (1 day)
#
#   Write a Unix shell script and Windows BAT script that can
#   delete an existing LH domain.  Leaves sufficient configuration
#   for upgrading the domain (using different web server) next
#   restart.

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

# SMF well-known values
SERVICE_NAME=system/webconsole
ENABLE_PROP=general/enabled
SUN_OS="SunOS"
LINUX_OS="Linux"
HPUX_OS="HP-UX"

# Command line options
INST_FLAG_SHORT="-i"
INST_FLAG_LONG="--instance"
BASE_FLAG_SHORT="-R"
BASE_FLAG_LONG="--Relocation"
ALL_FLAG_SHORT="-a"
ALL_FLAG_LONG="--all"
QUIET_FLAG_SHORT="-q"
QUIET_FLAG_LONG="--quiet"

# Values determined from command line arguments.
INSTANCE=""
BASEDIR=""
ALLOPT=false
QUIETOPT=false
QUIETMODE=""

########################################
# Process command line arguments
########################################

# Process argument options to use short form.
args=`echo $* | sed \
    -e "s/${INST_FLAG_LONG}/${INST_FLAG_SHORT}/" \
    -e "s/${ALL_FLAG_LONG}/${ALL_FLAG_SHORT}/" \
    -e "s/${BASE_FLAG_LONG}/${BASE_FLAG_SHORT}/" \
    -e "s/${QUIET_FLAG_LONG}/${QUIET_FLAG_SHORT}/"`

iflag=0
aflag=0
Rflag=0
qflag=0

ERRTMP=/tmp/wccreate.$$
rm -f $ERRTMP > /dev/null 2>&1
while getopts "i:R:aq" c > ${ERRTMP} 2>&1; do
    case $c in
        "i") iflag=1;
             INSTANCE=${OPTARG}
             ;;
        "a") aflag=1;
             ALLOPT=true
             ;;
        "R") Rflag=1;
             BASEDIR=${OPTARG}
             ;;
        "q") qflag=1;
             QUIETOPT=true
	     QUIETMODE="> /dev/null 2>&1"
             ;;
    esac
done
rm -f $ERRTMP > /dev/null 2>&1

# Must have an instance name.
if [ ${iflag} -eq 0 -o "${INSTANCE}" = "" ]; then
    echo "A console instance name must be specified." 1>&2
    exit 1
fi

# If the base directory is "/", reset to empty string.
if [ "${BASEDIR}" = "/" ]; then
    OBBASEDIR=""
fi

########################################
# Main
########################################

# Must be one of our supported operating environments.
OS=`uname -s`
if [ ! -n "${OS}" ]; then
    echo "Unable to determine operating system." 1>&2
    exit 1
fi

if [ "${OS}" != "${SUN_OS}" -a "${OS}" != "${LINUX_OS}" \
    -a "${OS}" != "${HPUX_OS}" ]; then
    echo "Not supported on detected OS \"${OS}\"." 1>&2
    exit 1
fi

if [ "${OS}" = "${SUN_OS}" ]; then
    CONSOLE_CONFDIR=${SOLARIS_CONF}
else
    CONSOLE_CONFDIR=${UNIX_CONF}
fi

# remove the webconsole service on Linux
if [ "${OS}" = "${LINUX_OS}" ]; 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
    echo "Cannot find console configuration file - domain not removed." 1>&2
    exit 1
fi
. ${PROPFILE}
CONSOLE_CONBASE=$BASEDIR/${console_conf}/${INSTANCE}
CONSOLE_DOMBASE=$BASEDIR/${console_dombase}/${INSTANCE}
CONSOLE_LOGBASE=$BASEDIR/${console_log}/${INSTANCE}

########################################
# Stop the console instance server process.

if [ "${QUITEOPT}" != "true" ]; then
    echo " "
    echo "Stopping console process..."
fi

# Check if this console instance is running.  If so, stop it.
if [ -x ${console_home}/bin/smcwebserver ]; then
    temp=`${console_home}/bin/smcwebserver status -p`
    STAT=`echo "${temp}" | grep "running" | cut -f2 -d"="`
    if [ "${STAT}" != "no" ]; then
	${console_home}/bin/smcwebserver stop ${QUIETMODE}
	if [ $? -ne 0 ]; then
	    echo "Unexpected error stopping console server" 1>&2
	fi
    fi
fi

########################################
# If Solaris 10 up, disable the console SMF service.
# Simply drop the service into maintenance state.
# The service can be restored later by running svcadm clear.

if [ "${OS}" = "${SUN_OS}" ]; then
    VERS=`uname -r`
    if [ "${VERS}" != "5.8" -a "${VERS}" != "5.9" ]; then
	FMRI="${SERVICE_NAME}:${INSTANCE}"
	temp=`/usr/bin/svcs -H ${FMRI}`
	if [ $? -eq 0 ]; then
	    temp=`echo "${temp}" | /usr/bin/cut -f1 -d" "`
	    if [ "${temp}" != "maintenance" -a "${temp}" != "MAINTENANCE" ]; then
		if [ "${QUITEOPT}" != "true" ]; then
		    echo " "
		    echo "Disabling console service..."
		fi
		/usr/sbin/svcadm mark -It maintenance ${FMRI}
		if [ $? -ne 0 ]; then
		    echo "Unexpected error disabling service: ${FMRI}"
		fi
	    fi
	fi
    fi
fi

########################################
# Remove the console instance domain files.

if [ "${QUITEOPT}" != "true" ]; then
    echo " "
    echo "Removing console instance configuration files..."
fi

# Remove the specified console instance domain.
# If its an AppServer domain, must delete it through asadmin.
if [ -d ${CONSOLE_DOMBASE} ]; then
    if [ "${console_type}" = "sjsas" ]; then
	CMD=${container_home}/bin/asadmin
	if [ -x ${CMD} ]; then
	    ${CMD} delete-domain --domaindir `dirname ${CONSOLE_DOMBASE}` \
		${INSTANCE} ${QUIETMODE}
	fi
    fi
fi

# 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
CONSOLE_PREREG_DIR=${CONSOLE_CONBASE}/prereg/console
if [ -d ${CONSOLE_PREREG_DIR} ]; then
    rm -f ${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_CONBASE}/regcache
if [ -d ${CACHEDIR} ]; then
    rm -f ${CACHEDIR}/* ${QUIETMODE}
fi
STATFILE=${CONSOLE_CONBASE}/status.properties
if [ -f ${STATFILE} ]; then
    rm -f ${STATFILE} ${QUIETMODE}
fi
CONS_REGDIR=${CONSOLE_CONBASE}/prereg/console
if [ -d ${CONS_REGDIR} ]; then
    rm -rf ${CONS_REGDIR}/prereg/console ${QUIETMODE}
fi
rm -f ${CONSOLE_CONBASE}/*.bkp > /dev/null 2>&1

# Remove any temporary directory files for this console instance.
# Brute force method - destroy all tmp files!
rm -rf ${CONSOLE_BASE}/tmp/${INSTANCE}_*.tmp > /dev/null 2>&1

# At this point, we have removed all files associated with the
# just in time configuration of a console instance domain.
# If the --all option is specified, remove the configuration directories.
if [ "${ALLOPT}" = "true" ]; then
    if [ "${QUITEOPT}" != "true" ]; then
	echo " "
	echo "Removing all console configuration files..."
    fi

    # Remove configuration subdirectories.
    # Brute force method!
    if [ -d ${CONSOLE_CONBASE} ]; then
	rm -rf ${CONSOLE_CONBASE} ${QUIETMODE}
    fi

fi

exit 0
