#!/bin/sh
#############################################################################
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright 2000-2007 Sun Microsystems, Inc. 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.html
# or mq/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 mq/legal/LICENSE.txt.  Sun designates
# this particular file as subject to the "Classpath" exception as provided by
# Sun in the GPL Version 2 section of the License file that accompanied this
# code.  If applicable, add the following below the License Header, with the
# fields enclosed by brackets [] replaced by your own identifying information:
# "Portions Copyrighted [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. 
#
#
# @(#)init-config	1.12 07/11/07
# 
#############################################################################

#
# Post install configurator script for file based install of Message Queue.
# This script does:
#  - writes the JDK to use for MQ into the imqenv.conf file
#  - writes the JDK to use for MQ into the uninstaller.properties file
#
MYDIR=`dirname $0`
cd ${MYDIR}
MYDIR=`pwd`
UNAME=uname

#
# This is the VM/CLASSPATH that was used to run the installer
#
INSTALLER_JAVA_HOME=${INIT_CONFIG_JAVA_HOME}
INSTALLER_JAVA_CLASSPATH=${CLASSPATH}:${INIT_CONFIG_JAVA_CLASSPATH}:${MYDIR}

#
# This is the VM that we want MQ to use. Currently hardcoded. This value/path
# will eventually be obtained from the PH framework.
#
IMQ_DEFAULT_JAVAHOME=/usr/jdk/instances/jdk1.5.0

#
# This is the MQ install home that will be obtained from OI framework
#
IMQ_INSTALL_HOME=/

RESET=
SILENT=
CONFIGSCHEMA=
CONFIGDATA=
VALIDATE=
CONFIG_STATUS=SUCCESS
PLATFORM_ERROR=None
PRODUCT_ERROR=None
DOC_REF=None
NEXT_STEPS=None

CHECK_PERM_FILES='
var/install/contents/mq/uninstaller
mq/bin/imqadmin
mq/bin/imqbrokerd
mq/bin/imqcmd
mq/bin/imqdbmgr
mq/bin/imqkeytool
mq/bin/imqobjmgr
mq/bin/imqusermgr
mq/bin/mquninstall
mq/bin/certutil
mq/bin/ssltap
mq/lib/imq.jar
mq/lib/imqadmin.jar
mq/lib/imqbroker.jar
mq/lib/imqjmx.jar
mq/lib/imqservlet.jar
mq/lib/imqutil.jar
mq/lib/imqxm.jar
mq/lib/jaxm-api.jar
mq/lib/jms.jar
mq/lib/fscontext.jar
mq/lib/jhall.jar
mq/lib/install/mqconfigmf
mq/lib/install/init-config
mq/lib/libmqcrt.so.1
mq/lib/libfreebl3.so
mq/lib/libimqutil.so.1
mq/lib/libnspr4.so
mq/lib/libnss3.so
mq/lib/libnssckbi.so
mq/lib/libplc4.so
mq/lib/libplds4.so
mq/lib/libsmime3.so
mq/lib/libsoftokn3.so
mq/lib/libssl3.so
install/bin/engine-wrapper
install/bin/uninstaller
'

printResult() {
    echo '<resultReport xmlns="http://openinstaller.org/config/resultreport/V1">' >&2
    echo '<configStatus>'$1'</configStatus>' >&2
    echo '<platformError>'$2'</platformError>' >&2
    echo '<productError>'$3'</productError>' >&2
    echo '<docReference>'$4'</docReference>' >&2
    echo '<nextSteps>'$5'</nextSteps>' >&2
    echo '</resultReport>' >&2
}

usage()  {
    echo "### TBD: usage help ###"
}

# Set error codes returned by mqconfigmf script
setErrorCodes() {
    INVALID_OS_ARCH=101
    MFWKSETUP_CMD_NOT_FOUND=102
    MQ_INSTALL_DIR_NOT_FOUND=103
    COM_SUN_CMM_MQ_XML_NOT_FOUND=104
    MFWK_CMD_FAILED=105
    TOKEN_REPLACEMENT_FAILED=106
    CACAOADM_CMD_NOT_FOUND=107
    CACAO_NOT_RUNNING=108
    USAGE_ERROR=109
}

#
# Check (and correct) file permissions
# Some OS's may not have unzip installed by default which causes the OI
# framework to use java.util.zip to extract files. This may leave some
# files with incorrect permissions, in particular files that should be
# executeable.
#
checkFilePermissions() {
    for f in $CHECK_PERM_FILES
    do
        if [ -s ${IMQ_INSTALL_HOME}/$f -a ! -x ${IMQ_INSTALL_HOME}/$f ]
        then
            echo "Fixing permissions for file: ${IMQ_INSTALL_HOME}/$f"
            chmod 755 ${IMQ_INSTALL_HOME}/$f
        fi
    done
}

#
# Re-do sym links on AIX since
# the unzip classes on AIX do not preserve sym links.
#
checkSymLinks() {

    if [ `${UNAME} -s` = "AIX" ]; then
	if [ -d ${IMQ_INSTALL_HOME}/mq/lib ]; then
            cd ${IMQ_INSTALL_HOME}/mq/lib; 
            if [ -s libmqcrt.so.1 ]; then
	        rm libmqcrt.so; ln -s libmqcrt.so.1 libmqcrt.so;
            fi;
            if [ -s libimqutil.so.1 ]; then
	        rm libimqutil.so; ln -s libimqutil.so.1 libimqutil.so;
            fi;
	fi;
    fi
}


#
# Perform actual configuration/unconfiguration
#
doWork() {
if [ -n "${RESET}" ] ; then
    echo "Reset - nothing done"
else
    #
    # Determine selected JDK_HOME
    #
    ${INSTALLER_JAVA_HOME}/bin/java \
      -cp "${INSTALLER_JAVA_CLASSPATH}" \
      ${INIT_CONFIG_HELPER_CLASS} \
      -s ${CONFIGSCHEMA} \
      -d ${CONFIGDATA} \
      -o DECODE \
      -f /tmp/mq.config.$$ 

     IMQ_DEFAULT_JAVAHOME=`awk '/IMQ_SELECTED_JDK/ {getline; print}' /tmp/mq.config.$$`
     IMQ_INSTALL_HOME=`awk '/IMQ_INSTALL_HOME/ {getline; print}' /tmp/mq.config.$$`

     /bin/rm -f /tmp/mq.config.$$

     echo "User chose JDK: $IMQ_DEFAULT_JAVAHOME"
     echo "User chose IMQ_INSTALL_HOME: $IMQ_INSTALL_HOME"

     IMQENV_CONF=${IMQ_INSTALL_HOME}/etc/mq/imqenv.conf
     echo "imqenv.conf file: $IMQENV_CONF"

     UNINSTALL_PROP=${IMQ_INSTALL_HOME}/${UNINSTALL_PROP_REL_LOCATION}

    ${INSTALLER_JAVA_HOME}/bin/java -cp ${INSTALLER_JAVA_CLASSPATH} SetupJDKHome -i ${IMQENV_CONF} -j "IMQ_DEFAULT_JAVAHOME=${IMQ_DEFAULT_JAVAHOME}"
    RETVAL=$?
    echo "RETVAL: ${RETVAL}"

    if [ ! ${RETVAL} = 0 ]
    then
	CONFIG_STATUS=FAIL

	if [ ${NEXT_STEPS} = ""] 
	then
	    NEXT_STEPS="Setting JDK in ${IMQENV_CONF} failed. Please edit this file manually."
	else
	    NEXT_STEPS=${NEXT_STEPS}" Setting JDK in ${IMQENV_CONF} failed. Please edit this file manually."
	fi
    fi

    ${INSTALLER_JAVA_HOME}/bin/java -cp ${INSTALLER_JAVA_CLASSPATH} SetupJDKHome -i ${UNINSTALL_PROP} -j "${IMQ_DEFAULT_JAVAHOME}"
    RETVAL=$?
    echo "RETVAL: ${RETVAL}"

    if [ ! ${RETVAL} = 0 ]
    then
	CONFIG_STATUS=FAIL

	if [ ${NEXT_STEPS} = ""] 
	then
	    NEXT_STEPS="Setting JDK in ${UNINSTALL_PROP} failed. Please edit this file manually."
	else
	    NEXT_STEPS=${NEXT_STEPS}" Setting JDK in ${UNINSTALL_PROP} failed. Please edit this file manually."
	fi

    fi

    #
    # Check/correct file permissions
    #
    checkFilePermissions

    #
    # Re-do sym links on some unix's.
    # 
    checkSymLinks
fi
    

}

#
# Perform validation - just set DOC_REF
#
doValidate() {
if [ -n "${RESET}" ] ; then
    DOC_REF="Validation of MQ unconfigurator performed. No actual unconfiguration done."
else
    DOC_REF="Validation of MQ configurator performed. No actual configuration done."
fi
}


########
# MAIN #
########
echo "init-config script for MQ"
echo "init-config: options passed: " $*

setErrorCodes

OPTSTRING="f:d:rshv"

while getopts "${OPTSTRING}" opt; do
    case ${opt} in
        f) echo "-f specified with: " ${OPTARG}
        CONFIGSCHEMA=${OPTARG}
	;;

        d) echo "-d specified with: " ${OPTARG}
        CONFIGDATA=${OPTARG}
	;;

        h) usage 
        ;;

        r) RESET=true 
        ;;

        s) SILENT=true 
        ;;

        v) VALIDATE=true 
        ;;

    esac

done

#
# Set relative location of uninstaller.properties
#
UNINSTALL_PROP_REL_LOCATION=var/install/contents/mq/uninstaller.properties

if [ -n "${VALIDATE}" ] ; then
    doValidate
else
    doWork
fi

echo "CONFIG_STATUS: ${CONFIG_STATUS}"
echo "PLATFORM_ERROR: ${PLATFORM_ERROR}"
echo "PRODUCT_ERROR: ${PRODUCT_ERROR}"
echo "DOC_REF: ${DOC_REF}"
echo "NEXT_STEPS: ${NEXT_STEPS}"

printResult ${CONFIG_STATUS} "${PLATFORM_ERROR}" "${PRODUCT_ERROR}" "${DOC_REF}" "${NEXT_STEPS}"
