#!/bin/csh -f
# Fast shutdown usage:
# aleph_shutdown <-kill> <signal>
# -kill  means that after running all regular shutdown jobs, all running
#        processes in $aleph_exe are killed.
# use signal to add option to kill (like -9)
# Description:
# Fast shutdown kills running jobd and servers, it then signals batch queue
# and all ue processes to shut down, by creating stop files for each library.
# when the -kill option is used, all remaining processes running in $aleph_exe
# are killed using <signal>

        source $alephe_dev/alephe/aleph_start
        source $aleph_proc/def_local_env

        if (-f $alephe_root/aleph_startup.private) then
          source $alephe_root/aleph_startup.private
        endif

        set old_alephver_alias = `alias alephver`
        alias alephver 'echo "xxxx" > /dev/null'


        setenv l_aleph_app_version a${ALEPH_VERSION}_${ALEPH_COPY}

#*******************************************
#       jobd
#*******************************************
        echo "*** Shutdown jobd"
        aleph_ps kill "$aleph_exe/jobd $ALEPH_APP_VERSION" "$$|log"
        echo " "

#*******************************************
#       servers
#*******************************************
        echo "*** Shutdown servers"
        $aleph_exe/server_monitor -ks

#********************************************
#       SRU/SRW server
#********************************************
        set current_dir = `pwd`
        cd $LOGDIR
        set pidfiles = `ls -1| grep "sru_server_[0-9]*.pid"`
        foreach f ($pidfiles)
            /bin/kill `cat $f` >& /dev/null
            rm $f
        end
        cd $current_dir
        echo " "

#********************************************
#       JBOSS shutdown
#********************************************
        if ($JBOS_SERVER_STARTUP == Y) then
            echo "*** Shutdown Jboss_server"
            aleph_ps exist "/exlibris/aleph/$l_aleph_app_version/product/local/java/bin/java"
        set isJboss = `get_symbol aleph_ps`
        if ($isJboss != 0) then
        kill $isJboss
        endif
        endif
        echo " "

#********************************************
#       Stopping ILL Automatic Receipt
#********************************************
        if ($?RECEIPT_STARTUP_LIBS) then
            echo "*** Stopping ILL Automatic Receipt"

            foreach lib ($RECEIPT_STARTUP_LIBS)
                start_proc00 $lib
                echo "  - $active_library "
                source $aleph_proc/elib_util_e_42
            end
            echo " "
        endif

#********************************************
#       Stop library batch queues and daemons
#********************************************
        echo "*** Stopping library batch queues and daemons"
        foreach lib ( $ALEPH_LIBS )
            set prof_lib=`eval echo \$${lib}_dev/$lib/prof_library`
            if (! -f $prof_lib) then
                goto next
            endif
            eval `grep setenv $prof_lib | sed 's/\#.*//' | sed 's/$/;/'`

            echo "  - $lib"

            set_symbol submit_param "stop_que//00/"
            submit

            touch $data_scratch/util_e_01_stop
            touch $data_scratch/util_e_03_stop
            touch $data_scratch/util_e_06_stop
            touch $data_scratch/util_e_08_stop
            touch $data_scratch/util_e_11_stop
            touch $data_scratch/util_e_13_stop
            touch $data_scratch/util_e_17_stop
            touch $data_scratch/util_e_19_stop
            touch $data_scratch/util_e_21_stop
            touch $data_scratch/util_e_23_stop
next:
        end
        echo " "

        sleep 30
# Number of loops to wait for everything to shut down. 
# After that, finish the shutdown script anyway. 
        @ number_of_wait = 6

#********************************************
#       Check shutdown of library batch queues and daemons
#********************************************
        echo "*** Check shutdown of library batch queues and daemons"

        foreach lib ($ALEPH_LIBS)
            if (-f `$aleph_exe/dev_aleph $lib`/$lib/prof_library) then
                echo "  - $lib "
                start_proc00 $lib

                set a = `$aleph_exe/lib_batch_running $active_library`
                @ wait_loop = 0
                while("$a" == "Y")
                    echo "$lib batch is active. Waiting 30 seconds"
                    sleep 30
                    @ wait_loop++
                    if  ($wait_loop != $number_of_wait) then
                       set a = `$aleph_exe/lib_batch_running $lib`
                    else
                       set a = "N"
                    endif
                end

                foreach deamon (ue_01_a ue_03_a ue_06_a ue_08_a ue_11_a ue_13_a ue_17_a ue_19_a ue_21_a ue_23_a ue_41_a)
                    aleph_ps exist "$deamon $active_library.$l_aleph_app_version"
                    set pid = `gs aleph_ps`
                    @ wait_loop = 0
                    while ($pid != 0)
                        echo "$lib $deamon is active. Waiting 30 seconds"
                        sleep 30
                        @ wait_loop++
                        if  ($wait_loop != $number_of_wait) then
                           aleph_ps exist "$deamon $active_library.$l_aleph_app_version"
                           set pid = `gs aleph_ps`
                        else
                           set pid = 0
                        endif
                    end
                end
            endif
        end
        echo " "

#********************************************
#       Check shutdown of servers
#********************************************
        echo "*** Check shutdown of servers"
        elib_server_monitor -s
        set server_status = $status
        @ wait_loop = 0
        while ($server_status != 0)
            echo "Servers are still active"
            server_monitor
            echo ""
            echo "Waiting 30 seconds"
            echo ""
            sleep 30
            @ wait_loop++
            if  ($wait_loop != $number_of_wait) then
               elib_server_monitor -s
               set server_status = $status
            else
               set server_status = 0
            endif
        end
        echo " "

#********************************************
#       Check shutdown of jbos_server
#********************************************
        if ($JBOS_SERVER_STARTUP == Y) then
            echo "*** Check shutdown of Jboss_server"
            ps -ef|grep jbos|grep $l_aleph_app_version > /dev/null
            set jbos_status = $status
            @ wait_loop = 0
            while ($jbos_status == 0)
                echo "Jbos_server is still active"
                echo ""
                echo "Waiting 30 seconds"
                echo ""
                sleep 30
                @ wait_loop++
                if  ($wait_loop != $number_of_wait) then
                   ps -ef|grep jbos|grep $l_aleph_app_version > /dev/null
                   set jbos_status = $status
                else
                   set jbos_status = 1
                endif
            end
            echo " "
        endif

        echo "*** Shutdown complete"

exit:
        alias alephver "$old_alephver_alias"

        exit

