#!/bin/sh
#

BASE=`basename $0`

THE_CMD=$1
DEPLOY_OP=deploy
UNDEPLOY_OP=undeploy

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

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

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

PATH=/bin:/usr/bin:/sbin:/usr/sbin

##############################################################
#
# 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
#
SECURE_OPTION=" "
if [ "true" = "${APP_SERVER_IS_SECURE_ADMIN_INSTANCE}" ]
then
  SECURE_OPTION=" --secure"
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}/.CommsExpress_`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

     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: Communications Express related additions
grant codeBase "file:${DEPLOY_DIR}/-" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/jato/jato.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/activation.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/mail.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/jcapi.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/ldapjdk.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/jaxp-api.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/dom.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/sax.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/xercesImpl.jar" {
        permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/lib/xalan.jar" {
        permission java.security.AllPermission;
};
// end: Communications Exporess related additions

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

// Start: Communications Express related additions
grant codeBase "file:${DEPLOY_DIR}/-" {
        permission java.security.AllPermission;
};

grant codeBase "file:/opt/sun/private/share/lib/jato.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/opt/sun/private/share/lib/jcapi.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/opt/sun/share/lib/ldapjdk.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/opt/sun/share/lib/jaxp-api.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/opt/sun/share/lib/dom.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/opt/sun/share/lib/sax.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/opt/sun/share/lib/xercesImpl.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/opt/sun/share/lib/xalan.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/usr/share/lib/activation.jar" {
        permission java.security.AllPermission;
};

grant codeBase "file:/usr/share/lib/mail.jar" {
        permission java.security.AllPermission;
};

// end: Communications Exporess 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
