#!/bin/ksh
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright (c) 2000-2010 Oracle and/or its affiliates. All rights reserved.
#
# The contents of this file are subject to the terms of either the GNU
# General Public License Version 2 only ("GPL") or the Common Development
# and Distribution License("CDDL") (collectively, the "License").  You
# may not use this file except in compliance with the License.  You can
# obtain a copy of the License at
# https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
# or packager/legal/LICENSE.txt.  See the License for the specific
# language governing permissions and limitations under the License.
#
# When distributing the software, include this License Header Notice in each
# file and include the License file at packager/legal/LICENSE.txt.
#
# GPL Classpath Exception:
# Oracle designates this particular file as subject to the "Classpath"
# exception as provided by Oracle in the GPL Version 2 section of the License
# file that accompanied this code.
#
# Modifications:
# If applicable, add the following below the License Header, with the fields
# enclosed by brackets [] replaced by your own identifying information:
# "Portions Copyright [year] [name of copyright owner]"
#
# Contributor(s):
# If you wish your version of this file to be governed by only the CDDL or
# only the GPL Version 2, indicate your decision by adding "[Contributor]
# elects to include this software in this distribution under the [CDDL or GPL
# Version 2] license."  If you don't indicate a single choice of license, a
# recipient has the option to distribute your version of this file under
# either the CDDL, the GPL Version 2 or to extend the choice of license to
# its licensees as provided above.  However, if you add GPL Version 2 code
# and therefore, elected the GPL Version 2 license, then the option applies
# only if the new code is made subject to such option by the copyright
# holder.
#

PRODUCTNAME="mq"

BASENAME=/bin/basename
DIRNAME=/usr/bin/dirname
_PWD=/bin/pwd
CAT=/bin/cat
ECHO=/bin/echo
NAME=`${BASENAME} $0`
MYDIR=`${DIRNAME} $0`
MYDIR=`cd ${MYDIR}; ${_PWD}`
ENGINE_DIR=/opt/sun/install
META_DATA_DIR=
INIT_CONFIG_DIR=

JAVA_HOME=""
_CMDLINEJAVA_HOME=""

# user options
DRYRUN=
SILENT=
ANSWERFILE=
ALTROOT=
DEBUGLEVEL="INFO"
MEDIALOC=
#LOGDIR=/tmp

#-------------------------------------------------------------------------------
# usage only: define what parameters are available here
# input(s):  exitCode
#-------------------------------------------------------------------------------
usage() {
  ${CAT} <<EOF
Usage: $NAME [OPTION]

Options:
  -a <file>
  Specify an answer file to be used by installer in non interactive mode.

  -h
  Show usage help

  -s
  Silent mode. No output will be displayed. Typically used with an answer file
  specified with -a

  -n <file>
  Dry run mode. Does not uninstall anything. Your uninstall responses are saved to
  <file> and can be used with -a to replicate this uninstall.

  Exit status
  0	Success
  >0	An error occurred

  Usage examples:
   Generate answer file
    uninstaller -n file1

   Using an answer file in silent mode
    uninstaller -a file1 -s

EOF
  exit $1
}

#-------------------------------------------------------------------------------
# perform actual operation for the script: install/uninstall
# input(s):  none
# output(s): instCode
#-------------------------------------------------------------------------------
perform() {

ENGINE_OPS="-p Default-Product-ID=${PRODUCTNAME}"
ENGINE_OPS="${ENGINE_OPS} -p Init-Config-Locations=${PRODUCTNAME}:${INIT_CONFIG_DIR}"
ENGINE_OPS="${ENGINE_OPS} -p UI-Options=internalBrowserOnly"
ENGINE_OPS="${ENGINE_OPS} -m file://${META_DATA_DIR}/"
ENGINE_OPS="${ENGINE_OPS} -s /var/install/config/${PRODUCTNAME}/"

if [ -n "${DRYRUN}" ] ; then
    ENGINE_OPS="${ENGINE_OPS} -n ${DRYRUN}"
fi

if [ -n "${SILENT}" ] ; then
    ENGINE_OPS="${ENGINE_OPS} -p Display-Mode=SILENT"
fi

if [ -n "${ANSWERFILE}" ] ; then
    ENGINE_OPS="${ENGINE_OPS} -a ${ANSWERFILE}"
fi

if [ -n "${ALTROOT}" ] ; then
    ENGINE_OPS="${ENGINE_OPS} -R ${ALTROOT}"
fi

if [ -n "${LOGLEVEL}" ] ; then
    ENGINE_OPS="${ENGINE_OPS} -l ${LOGLEVEL}"
fi

if [ -n "${LOGDIR}" ] ; then
    ENGINE_OPS="${ENGINE_OPS} -p Logs-Location=${LOGDIR}"
fi

if [ -n "${JAVA_HOME}" ] ; then
    ENGINE_OPS="${ENGINE_OPS} -j ${JAVA_HOME}"
fi

if [ -n "${INSTALLPROPS}" ] ; then
    ENGINE_OPS="${ENGINE_OPS} ${INSTALLPROPS}"
fi

#
# Workaround for bug:
# 6567177 - uninstaller doesn't work on Solaris if run from /var/opt/install/contents/mq 
#		directory
#
# Workaround commented out - it causes the "-n" option to not work since the answer file
# will be created under '/'.
#cd /

${ENGINE_DIR}/bin/engine-wrapper -J "${JAVA_OPTIONS}" ${ENGINE_OPS}
instCode=$?

}

setJavaHome() {
    if [ "$_CMDLINEJAVA_HOME" = "" ]; then
	#
	# Read uninstall.properties file to determine JDK to use
	#
	if [ -f ${UNINSTALL_PROP} -a -r ${UNINSTALL_PROP} ] ; then
            {
                while read oneLine; do
	            if [ "$oneLine"X != "X" ]; then
	                JAVA_HOME=$oneLine
	                break;
	            fi
                done
            } < ${UNINSTALL_PROP}
	fi
    else
        JAVA_HOME=${_CMDLINEJAVA_HOME}
        ${ECHO} "Using the user defined JAVA_HOME : ${JAVA_HOME}"
    fi


    #
    # No JVM set - revert to hardcoded defaults
    #
    if [ "$JAVA_HOME" = "" ] ; then
        if [ "`uname -s`" = Linux ] ; then
            JAVA_HOME=${INSTALL_HOME}/usr/java/jdk1.5.0_15
        else
            JAVA_HOME=${INSTALL_HOME}/usr/jdk/instances/jdk1.5.0
        fi
    fi

    if [ ! -d ${JAVA_HOME} -o ! -r ${JAVA_HOME} ] ; then
        ${ECHO} "${JAVA_HOME} must be the root directory of a valid JVM installation"
        exit 1
    fi
}

#
# MAIN
#

OPTSTRING="hl:n:svta:j:J:p:"

# check arguments
while getopts "${OPTSTRING}" opt ; do
    case "${opt}" in

	a) ANSWERFILE=${OPTARG}
	;;

        R) ALTROOT=${OPTARG}

            if [ ! -d ${ALTROOT} -o ! -r ${ALTROOT} ] ; then
                ${ECHO} "${ALTROOT} is not a valid alternate root"
                exit 1
            fi
        ;;

	l) LOGDIR=${OPTARG}

	    if [ ! -d ${LOGDIR} -o ! -w ${LOGDIR} ] ; then
		${ECHO} "${LOGDIR} is not a directory or is not writable"
		exit 1
	    fi
	;;

	s) SILENT=true
	;;
	v) LOGLEVEL=FINEST
	;;  
        t) INSTALLPROPS="${INSTALLPROPS} -p Display-Mode=CUI"
        ;;
        n) DRYRUN=${OPTARG}
        ;;
	j) _CMDLINEJAVA_HOME=${OPTARG}

	    if [ ! -d ${_CMDLINEJAVA_HOME} -o ! -r ${_CMDLINEJAVA_HOME} ] ; then
		${ECHO} "${_CMDLINEJAVA_HOME} must be the root directory of a valid JVM installation"
		exit 1
	    fi
	;;

	J) JAVA_OPTIONS=${OPTARG}
	;;
	p) INSTALLPROPS="${INSTALLPROPS} -p ${OPTARG}"
	;;
	?|h) usage
	;;
    esac
done

#
# Solaris or Linux case only.
# Check if user is root before continuing on.
#
uninstall_mq=1
if [ "`uname -s`" = SunOS ] ; then
    if [ `/usr/xpg4/bin/id -u` != 0 ]; then
        ${ECHO} "$0: You must be \"root\" to install Message Queue."
        ${ECHO} "Exiting."
        exit 1
    fi
    if [ -x /bin/zonename ]; then
        if [ "`/bin/zonename`" != "global" ] ; then
            if [ ! -n "${SILENT}" ] ; then
                ${ECHO} "$0: You are attempting to uninstall Message Queue from"
	        ${ECHO} "    a non-global zone.  Message Queue is only supported"
                ${ECHO} "    when installed into a whole root non-global zone so"
                ${ECHO} "    the uninstall of Message Queue is also only supported"
                ${ECHO} "    when uninstalled from a whole root non-global zone."
                ${ECHO} "    Do you wish to continue? [y/n]: \c"
	        read ans
	    else
	        ans=y
	    fi;
            ${ECHO} " "
	    if [ "$ans" = "y" -o "$ans" = "Y" -o "$ans" = "yes" -o  "$ans" = "Yes" -o "$ans" = "YES" ]; then
    	        uninstall_mq=1
	    else
    	        echo "Exiting."
    	        uninstall_mq=0
    	        exit 1
	    fi;
        fi;
    fi 
    ENGINE_DIR=/opt/install
    META_DATA_DIR=/var/opt/install/contents/${PRODUCTNAME}
    INIT_CONFIG_DIR=/usr/share/lib/imq/install
    UNINSTALL_PROP=/var/opt/install/contents/mq/uninstaller.properties
else
    if [ `/usr/bin/id -u` != 0 ]; then
        ${ECHO} "$0: You must be \"root\" to install Message Queue."
        ${ECHO} "Exiting."
        exit 1
    fi
    ENGINE_DIR=/opt/sun/install
    META_DATA_DIR=/var/opt/sun/install/contents/${PRODUCTNAME}
    INIT_CONFIG_DIR=/opt/sun/mq/private/share/lib/install
    UNINSTALL_PROP=/var/opt/sun/install/contents/mq/uninstaller.properties
fi

trap 'exit' 1 2 13 15

setJavaHome

perform
exit $instCode
