#!/bin/bash
# Licensed Materials - Property of IBM
# 5724-I68
#
# (C) Copyright IBM Corp. 2000, 2006
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

if [ -d ${TOP} ]; then
    . ${TOP}/setup/init_ccd_vars.sh
else
    ${ECHO} "\$TOP not defined!  Cannot continue."
    exit 99
fi

if [ `uname` = "SunOS" ]; then
export USER=$LOGNAME
fi

# fix for 29337
if [ `uname` = "HP-UX" ]; then
export USER=$LOGNAME
fi

if [ "$USER" = "" ]; then
export USER=$LOGNAME
fi
# end fix

# Moved these to the top so that get_params.sh doesn't interfere
MAXMEM=`echo $APPSVR_MEMORY_FLAG | ${AWK} -F " " 'match($1,"[0-9]+") { print substr($1, RSTART, RLENGTH); exit}'`
MINMEM=`echo $APPSVR_MEMORY_FLAG | ${AWK} -F " " 'match($2,"[0-9]+") { print substr($2, RSTART, RLENGTH); exit}'`

. $TOP/bin/get_params.sh

WAS_APPSERVERNAME=${_CCD_APPSERVERNAME=${WAS_APPSERVERNAME}}

${ECHO}
${ECHO}
${ECHO} "Installing ccd  WAR file on Application Server: ${WAS_APPSERVERNAME} on node ${WAS_NODENAME}"

if [ -n "$WAS_SECURITY" ]; then
${ECHO}
${ECHO} "You have indicated that WAS security is enabled.  Please provide the"
${ECHO} "following authentication information:"
${ECHO} "WAS Username:"
read -p "> " WAS_USERNAME
${ECHO} "WAS Password (will not echo):"
read -p "> " -s WAS_PASSWORD
fi

${ECHO}
${ECHO} "Warning!  Running this script will uninstall any previously"
${ECHO} "installed Enterprise Appplication called ccd_${WAS_APPSERVERNAME}!"
${ECHO}
${ECHO} "Are you sure you want to do this? [y/N] "
read ret
case $ret in
  [Yy]*)
      #continue
  ;;
  *)
        ${ECHO} Canceled!
        exit 1
  ;;
esac

case ${CCD_DB} in
    db2)
    DB_JARS=${DB2_HOME}/java/db2jcc.jar
    DB_LICENSE_JAR=${DB2_HOME}/java/db2jcc_license_cu.jar
    ;;
    oracle)
    DB_JAR=${ORACLE_JDBC_DRIVER}
    ;;
esac

# tools.jar is needed for script compilation.                              
WAS_CPATH=${JAVA_HOME}/lib/tools.jar
WAS_CPATH="$WAS_CPATH;$DB_JAR"

if [ ${CCD_DB} = "db2" ]; then
WAS_CPATH="$WAS_CPATH;$DB_JARS"
WAS_CPATH="$WAS_CPATH;$DB_LICENSE_JAR"
fi


for i in `echo $CCD_CLASSPATH | sed -e 's/:/ /g'`; do
    if [ `dirname $i` = "${TOP}/jars" ]; then
        WAS_CPATH="$WAS_CPATH;$i"
    fi
done

for i in `echo $CCD_USER_CLASSPATH | sed -e 's/:/ /g'`; do
        WAS_CPATH="$WAS_CPATH;$i"
done

#Add profiler agent jars to WAS_CPATH
WAS_CPATH="$WAS_CPATH;$TOP/profiler/yourkit/yjp-controller-api-redist.jar;$TOP/profiler/jprofiler/agent.jar"

${ECHO} "*** Installing ccd.war file..."
MAXMEM=`echo $APPSVR_MEMORY_FLAG | ${AWK} -F " " 'match($1,"[0-9]+") { print substr($1, RSTART, RLENGTH); exit}'`
MINMEM=`echo $APPSVR_MEMORY_FLAG | ${AWK} -F " " 'match($2,"[0-9]+") { print substr($2, RSTART, RLENGTH); exit}'`
SVC_NAME=${_CCD_SVC_NAME=appsvr_${SHORT_HOSTNAME}}

case ${CCD_APPSVR} in
    websphereAE)
	echo "*** Error:  WebSphere AE is no longer supported"
	exit
    ;;
    websphere5)
	echo "*** Error:  WebSphere 5 is no longer supported"
	exit
    ;;
    websphere)
	echo "*** Error:  WebSphere 4 is no longer supported"
	exit
    ;;
    websphere6)
	${WAS_PROFILE_HOME}/bin/wsadmin.sh \
		-f ${TOP}/bin/websphere/install_war.jacl \
		${WAS_NODENAME} \
		${WAS_APPSERVERNAME} \
		${TOP}/etc/default/websphere/webapps/ccd.war \
		ccd_${WAS_APPSERVERNAME} \
		${WAS_VHOST} \
		${TOP} \
		${CCD_ETC_DIR} \
		${MINMEM} \
		${MAXMEM} \
		${WAS_CPATH} \
		${SVC_NAME} \
		${_CCD_CONF_APPSVR_PORT} \
		${USER}
	${ECHO} "Checking suppliers directory is empty"
	LSEXPECTED=". .. .blank.txt"
	LSFOUND=`ls -a ${WAS_PROFILE_HOME}/installedApps/${WAS_CELLNAME}/ccd_${WAS_APPSERVERNAME}.ear/ccd.war/suppliers`
	#Echo the variables back to us otherwise the comparison doesn't work
	STRIPA=`echo ${LSEXPECTED}`
	STRIPB=`echo ${LSFOUND}`
	if [ "${STRIPA}" = "${STRIPB}" ]; then
		${ECHO} "Creating suppliers symlink..."
		rm -rf ${WAS_PROFILE_HOME}/installedApps/${WAS_CELLNAME}/ccd_${WAS_APPSERVERNAME}.ear/ccd.war/suppliers
		ln -s ${TOP}/public_html/suppliers ${WAS_PROFILE_HOME}/installedApps/${WAS_CELLNAME}/ccd_${WAS_APPSERVERNAME}.ear/ccd.war/
    	else
		${ECHO} "suppliers directory/symlink already exists with files in, will not overwrite"
    fi
    ;;
esac

${ECHO} "*** Done!"
