#!/bin/sh
#

BASE=`basename $0`

THE_CMD=$1
DEPLOY_OP=deploy
UNDEPLOY_OP=undeploy

INSTALLDIR=<msg.RootPath>
URIPATH=<da.Uri>
MODULE_NAME=<da.ApplicationModuleName>
DEPLOY_DIR=<da.ApplicationDeployDir>

APP_SERVER_INSTALL_DIR=<da.AppServerInstallDir>
APP_SERVER_DOMAIN_DIR=<da.AppServerDomainDir>
APP_SERVER_DOC_ROOT_DIR=<da.AppServerDocRootDir>
APP_SERVER_INSTANCE_NAME=<da.AppServerTargetName>
APP_SERVER_VS_ID=<da.AppServerVirtualServerID>

APP_SERVER_ADMIN_HOST=<da.AppServerAdminHost>
APP_SERVER_ADMIN_PORT=<da.AppServerAdminPort>
APP_SERVER_ADMIN_USER_ID=<da.AppServerAdminUserID>
APP_SERVER_ADMIN_USER_PSWD=<da.AppServerAdminUserPswd>
APP_SERVER_ADMIN_USER_PASSWORD_FILE=<da.AppServerAdminUserPasswordFile>
APP_SERVER_IS_SECURE_ADMIN_INSTANCE=<da.AppServerIsSecureAdminInstance>

# this is what to add to the classpath
ADDITIONALPATH=$INSTALLDIR/lib/jars/commcli-client.jar:$INSTALLDIR/lib/jars/jdapi.jar

PATH=/bin:/usr/bin:/sbin:/usr/sbin
CP="/bin/cp -p"
AWK=/bin/nawk

#
# configure application server 8.1
#
configure_appserver_81() {
    echo "Configuring application server 8.1 ..."
    echo "Appending classpath-suffix ...."

    #step1: obtain current classpath suffix
    classpath=`${APP_SERVER_INSTALL_DIR}/bin/asadmin get --user "${APP_SERVER_ADMIN_USER_ID}" --passwordfile "${PW_FILE}" ${SECURE_OPTION} --host "${APP_SERVER_ADMIN_HOST}" --port "${APP_SERVER_ADMIN_PORT}" "${APP_SERVER_INSTANCE_NAME}.java-config.classpath-suffix" | cut -f2 -d= | sed 's/^ //'`
    echo "Current classpath-suffix=$classpath"
    matches=`echo $classpath | grep ${ADDITIONALPATH} | wc -l`
    if [ "$matches" -ne 0 ]
    then
        echo "additional path is already in classpath-suffix"
        return
    fi

    #step2: append the classpath-suffix with the additional path
    classpath="$classpath:${ADDITIONALPATH}"

    #step3: update the classpath-suffix in the instance
    ${APP_SERVER_INSTALL_DIR}/bin/asadmin set --user "${APP_SERVER_ADMIN_USER_ID}" --passwordfile "${PW_FILE}" ${SECURE_OPTION} --host "${APP_SERVER_ADMIN_HOST}" --port "${APP_SERVER_ADMIN_PORT}" "${APP_SERVER_INSTANCE_NAME}.java-config.classpath-suffix"=$classpath

}

##############################################################
#
# start main
#

# Check the option
#
if [ "$THE_CMD" = "" ]
then
  echo "$BASE: Usage ERROR: No option specified."
  echo "Usage: $BASE <option>"
  echo "  where valid values for <option> are '$DEPLOY_OP' or '$UNDEPLOY_OP'"
  exit 1
elif [ "$THE_CMD" != "$DEPLOY_OP"  -a  "$THE_CMD" != "$UNDEPLOY_OP" ]
then
  echo "$BASE: Usage ERROR: INVALID option '$THE_CMD' specified."
  echo "Usage: $BASE <option>"
  echo "  where valid values for <option> are '$DEPLOY_OP' or '$UNDEPLOY_OP'"
  exit 1
fi

# Add secure option id enabled
#
if [ "true" = "${APP_SERVER_IS_SECURE_ADMIN_INSTANCE}" ]
then
  SECURE_OPTION=" --secure=true"
else
  SECURE_OPTION=" --secure=false"
fi

# Perform the command
#
if [ -f "${APP_SERVER_INSTALL_DIR}/bin/asadmin" ]
then
  if [ "$THE_CMD" = "$UNDEPLOY_OP" ]
  then
      echo "ERROR: Undeploying is not supported...."
      exit 1
  elif [ "$THE_CMD" = "$DEPLOY_OP" ]
  then
     AS_INST_DIR="${APP_SERVER_DOMAIN_DIR}"
     if [ -d "${AS_INST_DIR}" ]
     then
       AS_CONFIG_DIR="${AS_INST_DIR}/config"
       if [ ! -d "${AS_CONFIG_DIR}" ]
       then
         echo "ERROR: Application Server Instance Config Directory does not exits: ${AS_CONFIG_DIR}: No such directory...."
         exit 1
       fi

       AS_CONFIG_BACK_UP_DIR="${AS_CONFIG_DIR}/.DA_`date +%Y%m%d%H%M%S`/"

       echo /bin/mkdir -p "${AS_CONFIG_BACK_UP_DIR}"
       /bin/mkdir -p "${AS_CONFIG_BACK_UP_DIR}"

       if [ $? != 0 ]
       then
         echo "ERROR: Cannot create config backup directory ${AS_CONFIG_BACK_UP_DIR} ...."
         exit 1
       fi

       echo ""
       echo "Taking config backup into directory: ${AS_CONFIG_BACK_UP_DIR} ...."

       echo ""
       echo /bin/chmod 700 "${AS_CONFIG_BACK_UP_DIR}" 
       /bin/chmod 700 "${AS_CONFIG_BACK_UP_DIR}" 

       echo ""
       echo /bin/cp -r "${AS_CONFIG_DIR}/domain.xml ${AS_CONFIG_BACK_UP_DIR}"
       /bin/cp -r ${AS_CONFIG_DIR}/domain.xml ${AS_CONFIG_BACK_UP_DIR}

       if [ $? != 0 ]
       then
         echo "ERROR: Could not backup ${AS_CONFIG_DIR}/domain.xml ..."
       fi

       echo ""
       echo /bin/cp -r "${AS_CONFIG_DIR}/server.policy ${AS_CONFIG_BACK_UP_DIR}"
       /bin/cp -r ${AS_CONFIG_DIR}/server.policy ${AS_CONFIG_BACK_UP_DIR}

       if [ $? != 0 ]
       then
         echo "ERROR: Could not backup ${AS_CONFIG_DIR}/server.policy ..."
       fi
     else
       echo "ERROR: Application Server Instance Directory does not exits: ${AS_INST_DIR}: No such directory...."
       exit 1
     fi

     echo ""
     echo ""
     
     PW_FILE=$AS_CONFIG_DIR/.as_pass_uwc
     
     if [ -f $PW_FILE ]; then
       /bin/rm -f  $PW_FILE
     fi

     /bin/touch $PW_FILE

     echo "AS_ADMIN_PASSWORD=${APP_SERVER_ADMIN_USER_PSWD}" > $PW_FILE

     echo ${APP_SERVER_INSTALL_DIR}/bin/asadmin deploydir --user "${APP_SERVER_ADMIN_USER_ID}" --passwordfile xxxxxxx ${SECURE_OPTION} --port "${APP_SERVER_ADMIN_PORT}" --target "${APP_SERVER_INSTANCE_NAME}" --virtualservers "${APP_SERVER_VS_ID}" --contextroot ${URIPATH} --force=true --name "${MODULE_NAME}" ${DEPLOY_DIR}
     ${APP_SERVER_INSTALL_DIR}/bin/asadmin deploydir --user "${APP_SERVER_ADMIN_USER_ID}" --passwordfile "${PW_FILE}" ${SECURE_OPTION} --port "${APP_SERVER_ADMIN_PORT}" --target "${APP_SERVER_INSTANCE_NAME}" --virtualservers "${APP_SERVER_VS_ID}" --contextroot ${URIPATH} --force=true --name "${MODULE_NAME}" ${DEPLOY_DIR}

     if [ $? = 0 ]
     then
       echo "Deployed the application successfully...."
     else
       echo "ERROR: Failed deploying the application...."
       /bin/rm -f $PW_FILE
       exit 1
     fi

     # Add classpath to commcli-client.jar and jdapi.jar
     #
     configure_appserver_81

     echo ""
     echo ""

     if [ -d "${AS_INST_DIR}" ]
     then
        AS_CONFIG_DIR="${AS_INST_DIR}/config"
        SERVER_POLICY_FILE="${AS_CONFIG_DIR}/server.policy"
        OS=`/bin/uname`
        if [ -f "${SERVER_POLICY_FILE}" ]
        then
        if [ $OS = "SunOS" ]; then
cat << END_OF_FILE >> ${SERVER_POLICY_FILE}

// Start: Delegated Administrator related additions
grant {
        permission java.util.PropertyPermission "user.language", "write";
};
grant {
        permission java.util.logging.LoggingPermission "control";
};
// end: Delegated Administrator related additions

END_OF_FILE
        elif [ $OS = "Linux" ]; then
cat << END_OF_FILE >> ${SERVER_POLICY_FILE}

// Start: Delegated Administrator related additions
grant {
        permission java.util.PropertyPermission "user.language", "write";
};
grant {
        permission java.util.logging.LoggingPermission "control";
};
// end: Delegated Administrator related additions

END_OF_FILE
       fi
      
           echo "INFO: Successfully configured server.policy file: ${SERVER_POLICY_FILE}...."  
        else
           echo ""
           echo "ERROR: Could not configure server.policy."
           echo "  Reason: Could not locate ${SERVER_POLICY_FILE}: No such file exists...."
           echo "Please do it manually...."
        fi
     else
       echo ""
       echo "ERROR: Could not configure server.policy."
       echo "  Reason: Application Server Instance Directory does not exits: ${AS_INST_DIR}: No such directory...."
       echo "Please do it manually...."
     fi

     echo ""
     echo ""

     echo ${APP_SERVER_INSTALL_DIR}/bin/asadmin reconfig --user "${APP_SERVER_ADMIN_USER_ID}" --passwordfile xxxxxxx ${SECURE_OPTION} --host "${APP_SERVER_ADMIN_HOST}" --port "${APP_SERVER_ADMIN_PORT}" --keepmanualchanges=true "${APP_SERVER_INSTANCE_NAME}"
     ${APP_SERVER_INSTALL_DIR}/bin/asadmin reconfig --user "${APP_SERVER_ADMIN_USER_ID}" --passwordfile "${PW_FILE}" ${SECURE_OPTION} --host "${APP_SERVER_ADMIN_HOST}" --port "${APP_SERVER_ADMIN_PORT}" --keepmanualchanges=true "${APP_SERVER_INSTANCE_NAME}"

     if [ $? = 0 ]
     then
       echo "Reconfigured the application server instance successfully...."
     else
       echo "ERROR: Failed Reconfiguring the application server...."
       /bin/rm -f $PW_FILE       
       exit 1
     fi

     echo ""
     echo ""

     echo ${APP_SERVER_INSTALL_DIR}/bin/asadmin get --user "${APP_SERVER_ADMIN_USER_ID}" --passwordfile xxxxxxx ${SECURE_OPTION} --host "${APP_SERVER_ADMIN_HOST}" --port "${APP_SERVER_ADMIN_PORT}" "${APP_SERVER_INSTANCE_NAME}.applications.web-module.${MODULE_NAME}.*"
     ${APP_SERVER_INSTALL_DIR}/bin/asadmin get --user "${APP_SERVER_ADMIN_USER_ID}" --passwordfile "${PW_FILE}" ${SECURE_OPTION} --host "${APP_SERVER_ADMIN_HOST}" --port "${APP_SERVER_ADMIN_PORT}" "${APP_SERVER_INSTANCE_NAME}.applications.web-module.${MODULE_NAME}.*"

     if [ $? != 0 ]
     then
       echo "ERROR: Failed Getting the module attributes...."
     fi
  fi
else
  echo "ERROR: Unable to locate the asadmin command: ${APP_SERVER_INSTALL_DIR}/bin/asadmin"
  echo "$BASE: ERROR performing command '$THE_CMD'"
  exit 1
fi
/bin/rm -f $PW_FILE
exit 0
