#!/bin/bash
# IBM Confidential
# OCO Source Materials
# 5724-V51
#
# (c) Copyright IBM Corp. 2000, 2008 All Rights Reserved.
#
# The source code for this program is not published or otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.
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 appservers other than websphere6, add all jars under $TOP/jars to classpath
# for websphere6, we will skip emf jars since the versions of WAS emf jars and latest IES emf jars differ

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

if [ ${CCD_APPSVR} = "websphere6" ]; then
  for i in `echo $CCD_CLASSPATH | sed -e 's/:/ /g'`; do
    is_emf=`echo $i | ${GREP} 'org\.eclipse\.emf'`
    if [ `dirname $i` = "${TOP}/jars" ]; then
        #add non-emf jars only
    if [ $i != "$is_emf" ]; then
          WAS_CPATH="$WAS_CPATH;$i"
    fi
    fi
  done

#add WAS emf jars
emf_common=`find ${WAS_HOME}/plugins -name 'org\.eclipse\.emf\.common_*\.jar'`
emf_ecore=`find ${WAS_HOME}/plugins -name 'org\.eclipse\.emf\.ecore_*\.jar'`
emf_ecore_xmi=`find ${WAS_HOME}/plugins -name 'org\.eclipse\.emf\.ecore\.xmi_*\.jar'`

#added WAS jars for enabling Fedarated security in WAS
emf_ecore_sdo=`find ${WAS_HOME}/plugins -name 'org\.eclipse\.emf\.ecore\.sdo_*\.jar'`
emf_commonj_sdo=`find ${WAS_HOME}/plugins -name 'org\.eclipse\.emf\.commonj\.sdo_*\.jar'`
emf_ecore_change=`find ${WAS_HOME}/plugins -name 'org\.eclipse\.emf\.ecore\.change_*\.jar'`

WAS_CPATH="$WAS_CPATH;$emf_common;$emf_ecore;$emf_ecore_xmi;$emf_ecore_sdo;$emf_commonj_sdo;$emf_ecore_change"
fi


#add user classpath
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
    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
    ;;
    
    *)
        ${ECHO} "$0: Error!: CCD_APPSVR set to \"${CCD_APPSVR}\". Unknown value"
        exit
    ;;
esac

${ECHO} "*** Done!"
