#! /bin/bash
#
# Author: Peter Friesen
#
# /etc/init.d/webservice
#
### BEGIN INIT INFO
# Provides: webservice
# Required-Start: $syslog $local_fs $network $named $time
# Required-Stop:  $syslog $local_fs $network $named $time
# Default-Start:  3 4 5
# Default-Stop:   0 1 2 6
# Short-Description: webservice
# Description: webservice including tomcat and liferay portal
### END INIT INFO

export PATH=/usr/bin:/bin:$PATH
export RPM_INSTALL_PREFIX=/opt

# be sure that DISPLAY is not set, when JRE is started to be sure that libawt.so is not loaded
unset DISPLAY

WEBSERVICE='/opt/webservice'
#TOMCAT_START="${WEBSERVICE}/tomcat/bin/startup.sh"
#TOMCAT_STOP="${WEBSERVICE}/tomcat/bin/shutdown.sh"
#TOMCAT_PID_FILE="/var${WEBSERVICE}/catalina.pid"
APACHE2_INIT="${WEBSERVICE}/install/init_apache2.sh -p false"
SYSBACKUP_TRIGGER='/var/opt/Assistant/data/sysbackup_running';
WEBSERVICE_START_INIT="/opt/webservice/scripts/checkWebserviceStartUp.sh"
WEBSERV_RPM_INSTALLED="/var/opt/soco2/tmp/webservice_rpm.installed"
INTENDED_STOP_TRIGGER="/var/opt/soco2/tmp/intended_webservice.stop"
#NUC_ACTIVATION_DONE="/var/opt/webservice/NUC_activation_done"
NUC_ACTIVATION_DONE="/var/opt/webservice/nuc_update.success"
NUC_ACTIVATION_FAILED="/var/opt/webservice/nuc_update.failure"
FI_IMPORT_NEEDED="/var/opt/webservice/FI_import.needed"
ASSI_INSTALLATION_INTERRUPTED='/var/opt/webservice/Assi-install.wait'
AMO_INSTALL_TRIGGER="/opt/dscxl/share/AMO_install_requested"
WS_LOGDIR="/var/log/webservice"
STARTUP_LOGFILE="${WS_LOGDIR}/startup.log"
MEMORYTUNING_SCRIPT="/opt/yaps/MemoryTuning.sh"
CHECK_ASSI_INSTALL_STATUS_SCRIPT="/opt/webservice/scripts/os4k_init_AMO.sh 0 0 5"


JRE_HOME=`${WEBSERVICE}/scripts/getJavaPath.sh liferay`

export JRE_HOME
export JAVA_HOME=${JRE_HOME}


#test -x ${TOMCAT_START} || exit 5
#test -x ${TOMCAT_STOP}  || exit 5

. /etc/rc.status

rc_reset

case "$1" in
    start)
        echo "" >> $STARTUP_LOGFILE
        echo "----------------------------------------------------"  >> $STARTUP_LOGFILE
        echo `date` "--- Called" $0 $1 "---" >> $STARTUP_LOGFILE

        logger -t webservice "received service command: $1 (from: $PPID)"
        logger -t webservice "using JRE_HOME=$JRE_HOME for webservice startup"

        echo `date` "setting working dir to: ${WS_LOGDIR}"  >> $STARTUP_LOGFILE
        mkdir -p ${WS_LOGDIR}
        cd ${WS_LOGDIR}

        # log rotate
        echo `date` "checking logfile size 100k for ${STARTUP_LOGFILE}"  >> $STARTUP_LOGFILE
        if [ -f "${STARTUP_LOGFILE}" ]; then
          size=`du -b ${STARTUP_LOGFILE} 2>&1 | awk '{print $1}'`
          is_big=`expr $size \> 100000`
          if [ "${is_big}" -eq 1 ]; then
            # log rotate
            rm -fv ${STARTUP_LOGFILE}.old
            mv -vf ${STARTUP_LOGFILE} ${STARTUP_LOGFILE}.old
            echo  `date` "executed log rotate for ${STARTUP_LOGFILE}" >> "${STARTUP_LOGFILE}"
          fi
        fi
        
        #unpack yapp and install it if this is the fisrt start after RPM install/upgrade
        #for now keep this code only as backup - we will try to perform the installation directly in the RPM install/update scriptlets
        if [ -e /opt/.yapp_deploy_on_ws_start ]; then
            rm -fv /opt/.yapp_deploy_on_ws_start
            echo  `date` 'First ws start after a RPM install/upgrade detected => will deploy YAPP and install it' >> "${STARTUP_LOGFILE}"
            rm -rf /opt/yapp
            ( cd /opt ; tar -xzf /opt/webservice/yapp.tgz )
            bash /opt/yapp/yapp-install.sh ws_rc_start
            echo  `date` "YAPP deploy and install finished with rc $?" >> "${STARTUP_LOGFILE}"
        fi
        
        trap 'bash /opt/yapp/yapp-install.sh _ws_start_' EXIT

        rm -rf ${INTENDED_STOP_TRIGGER}
        rm -rf ${SYSBACKUP_TRIGGER}
        current_hostname=`hostname`
        getent hosts ${current_hostname} >/dev/null 2>&1
        if [ "$?" -ne "0" ]
        then
            echo -n "No IP address found for given hostname ${current_hostname}, liferay won't start!"
            echo  `date` "No IP address found for given hostname ${current_hostname}, liferay won't start!" >> $STARTUP_LOGFILE
            echo  `date` "Current content of /etc/hosts:"  >> $STARTUP_LOGFILE            
            echo  `date` "Begin"  >> $STARTUP_LOGFILE
            cat /etc/hosts >> $STARTUP_LOGFILE
            echo  `date` "End"  >> $STARTUP_LOGFILE
            rc_failed 3
        else
            echo `date` "Starting postgresql" >> $STARTUP_LOGFILE
            service postgresql status >/dev/null 2>&1 || service postgresql start >> $STARTUP_LOGFILE
            echo `date` "Starting shellinabox" >> $STARTUP_LOGFILE
            service shellinabox status >/dev/null 2>&1 || service shellinabox start  >> $STARTUP_LOGFILE
            echo `date` "Starting apache2" >> $STARTUP_LOGFILE
            service apache2 status >/dev/null 2>&1 || service apache2 start >> $STARTUP_LOGFILE

            # check against yourself
            # but on SLES15 we can not just use service status command since this is unreliable (OSFOURK-22028)
            # we call the webservice init script directly via a symlink to avoid systemd interfering
            # final solution shall be migration to systemd unit files for webservice
            
            #service webservice status >/dev/null 2>&1
            ln -sfv /etc/init.d/webservice /bin/legacy-webservice >> $STARTUP_LOGFILE
            /bin/legacy-webservice status >> $STARTUP_LOGFILE 2>&1
            
            if [ $? -eq "0" ]; then
                echo "Starting webservice rejected - already running "
                echo  `date` "Starting webservice rejected - already running " >> $STARTUP_LOGFILE
                rc_failed 0
            else

              if [ -e /var/opt/webservice/ngsdb-backup.zip ]; then
                # give the pgsql a little time for startup
                echo `date` "file /var/opt/webservice/ngsdb-backup.zip exists -> importing it" >> $STARTUP_LOGFILE
                sleep 5
                echo `date` "Calling ngs-db-clean.sh" >> $STARTUP_LOGFILE
                $WEBSERVICE/install/ngs-db-clean.sh
                echo `date` "Calling ngs-db-init.sh" >> $STARTUP_LOGFILE
                $WEBSERVICE/install/ngs-db-init.sh
                echo `date` "Calling ngs-db-restore.sh" >> $STARTUP_LOGFILE
                $WEBSERVICE/install/ngs-db-restore.sh
              else
                echo `date` "file /var/opt/webservice/ngsdb-backup.zip does not exist -> continue with webservice startup" >> $STARTUP_LOGFILE
              fi

              # delete the ngsdb backup on the remote node of duplex
              # The backup is imported only on Primary node, when webservice is started.
              # So it should not be left existing on the Secondary node.
              # It might accidentally be imported by webservice.
              /opt/webservice/scripts/is_duplex
              if [ $? == "1" ]; then
                text="Trying to remove the db dump file on the standby duplex node to avoid inconsistent import after the next HA switch "
                echo "$text" >> $STARTUP_LOGFILE
                /opt/webservice/scripts/sshx -p "rm -f /var/opt/webservice/ngsdb-backup.zip" >> $STARTUP_LOGFILE
              fi

              if [ -e ${NUC_ACTIVATION_DONE} ] || [ -e ${FI_IMPORT_NEEDED} ]; then
                if [ -e ${FI_IMPORT_NEEDED} ]; then
                  echo `date` "Info: import of xml data from remote node requested" >> $STARTUP_LOGFILE
                else
                  echo `date` "Info: previous NUC activation was successful" >> $STARTUP_LOGFILE
                fi
                echo `date` "Calling export_all_node_fi_xml.sh" >> $STARTUP_LOGFILE
                $WEBSERVICE/install/export_all_node_fi_xml.sh
                echo `date` "Calling node-first-install-from-xml.sh -n" >> $STARTUP_LOGFILE
                $WEBSERVICE/install/node-first-install-from-xml.sh -n
                if [ -e ${NUC_ACTIVATION_DONE} ]; then
                  rm -rf ${NUC_ACTIVATION_DONE}
                  rm -rf ${AMO_INSTALL_TRIGGER}
                fi
              fi
              if [ -e ${NUC_ACTIVATION_FAILED} ]; then
                echo `date` "Info: previous NUC activation failed" >> $STARTUP_LOGFILE
                rm -rf ${NUC_ACTIVATION_FAILED}
              fi

              if [ ! -e ${WEBSERV_RPM_INSTALLED} ] && [ -e ${ASSI_INSTALLATION_INTERRUPTED} ]; then
                echo `date` "Assistant Installation was interrupted, check for continuation! " >> $STARTUP_LOGFILE
                ${CHECK_ASSI_INSTALL_STATUS_SCRIPT}  2>&1 &
              fi

              # check the pg_hba.conf file
              if [ -e  /var/lib/pgsql/data/pg_hba.conf ]; then
                 cnt_matching_lines_liferay=`fgrep -cxf /opt/webservice/install/pg_hba-liferay.conf /var/lib/pgsql/data/pg_hba.conf`
                 cnt_matching_lines_ngs=`fgrep -cxf /opt/webservice/install/pg_hba-ngs.conf /var/lib/pgsql/data/pg_hba.conf`
                 if [ $cnt_matching_lines_liferay != 3 -o $cnt_matching_lines_ngs != 3 ]; then
                    echo `date` "postgres config file (pg_hba.conf) not consistent - probably corrupted !!" >> $STARTUP_LOGFILE
                    /bin/cp /var/lib/pgsql/data/pg_hba.conf ${WS_LOGDIR}/faulty_pg_hba.conf
                 fi
              else
                echo `date` "postgres config file (pg_hba.conf) not found !!" >> $STARTUP_LOGFILE
              fi

              $JRE_HOME/bin/java -Dcom.ibm.tools.attach.enable=no -version 2>&1 | grep -i IBM
              if [ "$?" -eq "0" ]
              then
                  echo  "IBM JRE installed ($JRE_HOME) "
                  $WEBSERVICE/scripts/checkIf32Bit.sh $JRE_HOME/bin/java
                  is32=$?
                  if [ "${is32}" -eq "0" ]; then
                      echo `date` "using IBM JRE 32-bit " >> $STARTUP_LOGFILE
                      #export JAVA_OPTS="-Xms96m -Xmx192m -XX:PermSize=32m -XX:MaxPermSize=128m";
                      export JAVA_OPTS="-Xmx340m -XX:MaxPermSize=210m";
                  else
                      # only the 64 bit JRE allows compressedrefs option, so we have to be sure it is really a 64 bit
                      echo `date` "using IBM JRE 64-bit with 32-bit pointers " >> $STARTUP_LOGFILE
                      #export JAVA_OPTS="-Xms96m -Xmx224m -XX:PermSize=32m -XX:MaxPermSize=128m -Xcompressedrefs";
                      export JAVA_OPTS="-Xmx340m -XX:MaxPermSize=210m -Xcompressedrefs";
                  fi
                  # set the IBM option for the java heap dump files -> using one singleton file (with rotate/alternate) to prevent rotzing the /var partition
                  JAVACORE_FILE="${WS_LOGDIR}/javacore.webservice.txt"
                  JAVAHEAP_FILE="${WS_LOGDIR}/heapdump.webservice.phd"
                  JAVAHEAPUSER_FILE="${WS_LOGDIR}/heapdump.webservice.user.phd"
                  JAVASYSTEM_FILE="${WS_LOGDIR}/core.webservice.dmp"
                  if [ -f "${JAVACORE_FILE}" ]; then
                    mv -vf "${JAVACORE_FILE}" "${JAVACORE_FILE}".old >> $STARTUP_LOGFILE 2>&1
                  fi
                  if [ -f "${JAVAHEAP_FILE}" ]; then
                    mv -vf "${JAVAHEAP_FILE}" "${JAVAHEAP_FILE}".old >> $STARTUP_LOGFILE 2>&1
                  fi
                  if [ -f "${JAVAHEAPUSER_FILE}" ]; then
                    mv -vf "${JAVAHEAPUSER_FILE}" "${JAVAHEAPUSER_FILE}".old >> $STARTUP_LOGFILE 2>&1
                  fi
                  if [ -f "${JAVASYSTEM_FILE}" ]; then
                    mv -vf "${JAVASYSTEM_FILE}" "${JAVASYSTEM_FILE}".old >> $STARTUP_LOGFILE 2>&1
                  fi
                  export JAVA_OPTS="${JAVA_OPTS} -Dcom.ibm.tools.attach.enable=no -Dcom.ibm.tools.attach.logging=no"
                  export JAVA_OPTS="${JAVA_OPTS} -Djavax.xml.stream.supportDTD=false -Dcom.ibm.xml.xlxp.support.dtd.compat.mode=false"
                  export JAVA_OPTS="${JAVA_OPTS} -Xdump:java:defaults:file=${JAVACORE_FILE} -Xdump:heap:defaults:file=${JAVAHEAP_FILE} -Xdump:heap:events=user,file=${JAVAHEAPUSER_FILE} -Xdump:system:defaults:file=${JAVASYSTEM_FILE}"
                  export JAVA_OPTS="${JAVA_OPTS} -Xdump:tool:events=throw,filter=*MemoryError,exec=/opt/webservice/scripts/stopWebservice.sh,range=1..0"
                  echo `date` "using IBM JRE for webservice startup: JRE_HOME=${JRE_HOME} JAVA_OPTS=${JAVA_OPTS}" >> $STARTUP_LOGFILE

                  # ensure that the security jce jar files exists and are appropriate
                  if [ -x /opt/soco/ibm_jce_policy-install.sh ]; then
                    /opt/webservice/scripts/has_softgate >> $STARTUP_LOGFILE
                    there_is_active_sg=$?
                    if [ "${there_is_active_sg}" == 0 ]; then
                      echo "ibm_jce_policy-install.sh has to be called since there no active SG doing it" >> $STARTUP_LOGFILE
                      /opt/soco/ibm_jce_policy-install.sh $JRE_HOME
                    else
                      echo "ibm_jce_policy-install.sh can be skipped since there is an active SG doing it" >> $STARTUP_LOGFILE
                    fi
                  else
                    echo "Error: inconsistent installation (ibm_jce_policy-install.sh missing), Softgate not installed" >> $STARTUP_LOGFILE
                  fi
              else
                  echo  "SUN/Oracle JRE installed "
                  export JAVA_OPTS="-Xmx250m -XX:MaxPermSize=150m"
                  echo `date` "using SUN/Oracle JRE for webservice startup: JRE_HOME=${JRE_HOME} JAVA_OPTS=${JAVA_OPTS}" >> $STARTUP_LOGFILE
              fi

              if [ -x ${MEMORYTUNING_SCRIPT} ]; then
                echo `date` "Starting test for necessity of automatic memory tuning for Surv. and Standalone SGs on 4GB HW with script ${MEMORYTUNING_SCRIPT}"  >> ${STARTUP_LOGFILE}
                ${MEMORYTUNING_SCRIPT} enable_deferred >> ${STARTUP_LOGFILE} 2>&1 &
              else
                echo `date` "Test for necessity of automatic memory tuning for Surv. and Standalone SGs on 4GB HW can not be started because script ${MEMORYTUNING_SCRIPT} not found"  >> ${STARTUP_LOGFILE}
              fi

              echo `date` "Calling" $APACHE2_INIT >> $STARTUP_LOGFILE
              $APACHE2_INIT >> $STARTUP_LOGFILE 2>&1

              if [ "$?" -eq "0" ]
              then
                  echo -n "Starting webservice "
                  chrt -o -p 0 $$
                  echo "" >> $STARTUP_LOGFILE
                  echo `date` "Calling" $WEBSERVICE_START_INIT >> $STARTUP_LOGFILE
                  $WEBSERVICE_START_INIT >> $STARTUP_LOGFILE 2>&1
                  echo `date` "Starting tomcat ..." >> $STARTUP_LOGFILE
                  service tomcat start
                  #USE_NOHUP=false $TOMCAT_START >> $STARTUP_LOGFILE 2>&1
              else
                  rc_failed 3
              fi
            fi
        fi
        rc_status -v
        logger -t webservice "exiting service command: $1 (rc=$_rc_status)"
        ;;
    stop)
        logger -t webservice "received service command: $1 (from: $PPID)"
        echo -n "Stopping webservice "
        echo `date` "Stopping webservice" >> $STARTUP_LOGFILE

        chrt -o -p 0 $$
        # give the tomcat a little time before stopping
        sleep 5
        if [ -e ${WEBSERV_RPM_INSTALLED} ]; then
          echo `date` "Wait additional 10sec to be sure that the deploying finished." >> $STARTUP_LOGFILE
          sleep 10
        fi

        export JAVA_OPTS="${JAVA_OPTS} -Dcom.ibm.tools.attach.enable=no -Dcom.ibm.tools.attach.logging=no"

        #$TOMCAT_STOP -force > /dev/null 2>&1
        echo `date` "Stopping tomcat ..." >> $STARTUP_LOGFILE
        service tomcat stop
        echo `date` "Tomcat stop finished with rc $?" >> $STARTUP_LOGFILE
        echo `date` "Cleanup the Axis2 .lck files" >> $STARTUP_LOGFILE
        rm -f /opt/webservice/tomcat/temp/axis2-tmp*.lck

        rc_failed 0
        rc_status -v
        logger -t webservice "exiting service command: $1 (rc=$_rc_status)"

        #OSFOURK-10218 I33061 : V6R3 replacement service must be stoped in case of switchover
        #stop Replacement Server in case Portal is stopped for any reason
        service replacement stop
        true

        ;;
    restart|force-reload)
        $0 stop
        $0 start
        ;;
    try-restart|reload)
        if ($0 status) >/dev/null 2>&1; then
            $0 start
        else
            rc_reset
        fi
        ;;
    status)
        echo -n "Checking the status of webservice "
        if [ -e "$INTENDED_STOP_TRIGGER" ]; then
            rm -rf ${INTENDED_STOP_TRIGGER}
            rc_failed 0
        else
            systemctl status --no-pager tomcat
            rc_failed $?
#           if [ -e "$TOMCAT_PID_FILE" ]
#           then
#             # PID=`cat /var/opt/webservice/catalina.pid`
#             # PROCS=`ps -eo pid | grep $PID | grep -v grep | wc -l`
#             /sbin/checkproc -p "$TOMCAT_PID_FILE" /bin/sh
#             if [ "$?" -eq "0" ]
#             then
#               rc_failed 0
#             else
#               rc_failed 3
#             fi
#           else
#             rc_failed 3
#           fi
        fi
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|reload|force-reload}"
        logger -t webservice "wrong usage of service (rc=1)"
        exit 1
        ;;
esac

# Set exit status
rc_exit
