#! /bin/bash
#
# Author: Gregor Giel
#
# /etc/init.d/plt_upgrade
#
### BEGIN INIT INFO
# Provides: plt_upgrade
# 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: soco2_plt_upgrade 
# Description: soco2 platform upgrade continuation after reboot
### END INIT INFO

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

WEBSERVICE='/opt/webservice'
STMIX_TRIGGERFILE="/var/opt/soco/stmix"


. /etc/rc.status

rc_reset

case "$1" in
    start)
        logger -t plt_upgrade "received service command: $1"

        mkdir -p /var/log/webservice
            
        # check if a platform upgrade was running during the shutdown/power-off and continue it
        logger -t plt_upgrade "checking for an incomplete platform upgrade"
        
        if [ -e /var/opt/webservice/plt_upgrade_running ]; then
          logger -t plt_upgrade "incomplete platform upgrade detected, continuing"
          if [ -e /var/opt/webservice/plt_upgrade_running.status ]; then
            logger -t plt_upgrade "incomplete platform upgrade and Wiederaufsetzstatus plt_upgrade_running.status detected, continuing"
            call=`cat /var/opt/webservice/plt_upgrade_running`

            # we have to check if the perl script swupdated is really at the correct place to call it ( I44027 )
            # for this I have to parse the original call written in the /var/opt/webservice/plt_upgrade_running
 
            set -- $call
            executable=$1
            shift
            args=$*

            if [ ! -x ${executable} ]; then
              logger -t plt_upgrade "Executable ${executable} not found for Wiederaufsetzstatus, checking alternatives to call"
              if [ -x "${executable}.old" ]; then
                executable="${executable}.old"
                logger -t plt_upgrade "Executable ${executable} found for Wiederaufsetzstatus, using it"
              elif [ -x "/opt/webservice/scripts/swupdated" ]; then
                executable="/opt/webservice/scripts/swupdated"
                logger -t plt_upgrade "Executable ${executable} found for Wiederaufsetzstatus, using it"
              elif [ -x "/opt/webservice/scripts/swupdated.new" ]; then
                executable="/opt/webservice/scripts/swupdated.new"
                logger -t plt_upgrade "Executable ${executable} found for Wiederaufsetzstatus, using it"
              elif [ -x "/instmedia/opt/yaps/script/swupdated" ]; then
                executable="/instmedia/opt/yaps/script/swupdated"
                logger -t plt_upgrade "Executable ${executable} found for Wiederaufsetzstatus, using it"
              else
                logger -t plt_upgrade "Error: No Executable found for Wiederaufsetzstatus"
              fi
            fi

            if [ -x ${executable} ]; then
              logger -t plt_upgrade "Executable ${executable} can be called. Calling it with argument: ${args}"
              ${executable} ${args}
            else
              logger -t plt_upgrade "Executable ${executable} can not be called. Calling the original Wiederaufsetzstatus string in hope that it somehow may succeed: $call"
              $call
            fi
          else
            logger -t plt_upgrade "No Wiederaufsetzstatus plt_upgrade_running.status detected, switching to normal mode"
            rm -f /var/opt/webservice/plt_upgrade_running
            
            if [ -f ${STMIX_TRIGGERFILE} ]; then
              /opt/soco/native/os/linux/insserv_SG.sh -s
            else
              $WEBSERVICE/scripts/insserv_HA.sh
              /sbin/service openais start
            fi
          fi		  
        else
          if [ -f ${STMIX_TRIGGERFILE} ]; then
            logger -t plt_upgrade "no incomplete platform upgrade detected, re-enabling socod "
            /opt/soco/native/os/linux/insserv_SG.sh -s
          else
            logger -t plt_upgrade "no incomplete platform upgrade detected, re-enabling openais startup and starting openais"		  
            $WEBSERVICE/scripts/insserv_HA.sh
            /sbin/service openais start
          fi
        fi
        rc_status -v
        logger -t plt_upgrade "exiting service command: start (rc=$_rc_status)"
        ;;
    stop)
        logger -t plt_upgrade "received service command: $1"
        echo -n "Stopping plt_upgrade not supported"
        rc_failed 0
        rc_status -v
        logger -t plt_upgrade "exiting service command: $1 (rc=$_rc_status)"
        ;;
    restart|force-reload)
        logger -t plt_upgrade "received service command: $1"
        echo -n "restarting plt_upgrade not supported"
        rc_failed 0
        rc_status -v
        logger -t plt_upgrade "exiting service command: $1 (rc=$_rc_status)"
        ;;
    try-restart|reload)
        logger -t plt_upgrade "received service command: $1"
        echo -n "try-restarting plt_upgrade not supported"
        rc_failed 0
        rc_status -v
        logger -t plt_upgrade "exiting service command: $1 (rc=$_rc_status)"
        ;;
    status)
        echo -n "Checking the status of plt_upgrade not supported "
        rc_failed 0
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|reload|force-reload}"
        logger -t plt_upgrade "wrong usage of service (rc=1)"
        exit 1
        ;;
esac

# Set exit status
rc_exit
