#!/bin/csh -f
#
# Copyright (c) 2005, Oracle. All rights reserved.  
#
#   NAME
#     stoprb
#
#   DESCRIPTION
#     stop Reliaty Backup processes on some host
#
#   MODIFIED   (MM/DD/YY) 
#   rvullika    11/07/05 - fix script for solaris limitaion
#                          ( ps -e truncates process name to 8 chars)
#   rvullika    08/15/05 - rvullika_rbtoosb_cnv
#   rvullika    08/02/05 - Creation 
#
#  Usage:
#     stoprb                     -  To stop RB processes locally
#     stoprb <host1> <host2> ... -  To stop RB daemons other hosts

#  Common initialization
setenv OBTRACE 0

if (! $?installobinit) then
    if (-f initinstall.sh) then
        source initinstall.sh
        set st = $status
    else if (-f install/initinstall.sh) then
        source install/initinstall.sh
        set st = $status
    else
        echo "No initinstall.sh program exists in either '.' or './install'.  Be sure"
        echo "to run the Oracle Secure Backup 'installhere' program with your current"
        echo "working directory set to the Oracle Secure Backup directory for this host."
        set st = 1
        endif
    if ($st) goto done
    endif

set tmpf = /tmp/stoprb.$$
unset multi

# See if the script should be run locally or on remote host
if ($#argv == 0) then
    set host
else
    set host = $1
    set multi
    endif

set noisy_sd

again:

set no_mach_name
source $idir/machinfo.sh
set st = $status
if ($st != 0) exit ($st)


if (! $?os_name) then
    echo ""
    echo -n "Error: can't access ${host}: "
    $rsh $host echo
    goto trynext
    endif

set pscmd = '/bin/ps'
set psopt = '-e'

switch ($os_name)
    case SunOS:
        set psopt = '-ax'
        breaksw
    case solaris:
        set psopt = '-aef'
        breaksw

    default:
        breaksw
    endsw

if ("$host" == "") unset host

echo ""
if ($?host) then
    echo "Stopping Reliaty Backup daemons on $host"
else
    echo "Stopping Reliaty Backup daemons"
    endif

set daemons_sd = 0
set i_cmd = ""
if ($?cmd) then 
    set i_cmd = "$cmd"
    endif
set i_blk = ""
if ($?blk) then
    set i_blk = "$blk"
    endif

set daemons = (rbtar rbrobotd rbndmpd rbixd rbscheduled rbserviced rbhttpd rbnotify rbexec rbtool)

#  Send each running daemon the TERM signal:
#
set k_any = 0
if ($?host) then
    $rsh $host $pscmd $psopt >$tmpf
else
    $pscmd $psopt >$tmpf
    endif

foreach daemon ($daemons)
    if ($os_name == solaris) then
        set pids = ( `cat $tmpf | grep -v grep | grep "$daemon" | awk '{ print $2 }'` )
    else
        set pids = ( `cat $tmpf | grep -v grep | grep "$daemon" | awk '{ print $1 }'` )
        endif
    foreach pid ($pids)
        @ k_any += 1
        echo "    Stopping $daemon in process $pid."
        if ($?host) then
            $rsh $host $i_cmd $i_blk kill -TERM $pid
        else
            $i_cmd $i_blk kill -TERM $pid
            endif
        end
    end


#  If we found any running daemons, give things a little time to quiesce, then hit each
#  with the big hammer:
#
if ($k_any > 0) then
    if ($k_any > 1) then
        echo "    Waiting for $k_any daemons to stop..."
    else
        echo "    Waiting for the daemon to stop..."
        endif

    if (! $?so) sleep 4

    if ($?host) then
        $rsh $host $pscmd $psopt >$tmpf
    else
        $pscmd $psopt >$tmpf
        endif

    foreach daemon ($daemons)
        set pids = ( `cat $tmpf | grep -v grep | grep "$daemon" | awk '{ print $1 }'` )
        foreach pid ($pids)
            if ($?noisy_sd) echo "    Forcing $daemon in pid $pid to stop."
            if ($?host) then
                $rsh $host $i_cmd $i_blk kill -KILL $pid
            else
                $i_cmd $i_blk kill -KILL $pid
                endif

            @ errsum += $status
    
            #  Wait just a bit for her to go away...
            set i = 1
            while ($i < 10)
                if ($?host) then
                    $rsh $host $pscmd $psopt >$tmpf
                else
                    $pscmd $psopt >$tmpf
                    endif

                set pid2 = ( `cat $tmpf | grep -v grep | grep "$daemon" | grep "$pid " | awk '{ print $1 }'` )

                if ($pid2 != $pid || $?so) break
                sleep 1
                @ i += 1
                end

            if ($i >= 10 && $?noisy_sd) then
                echo "    It appears that $daemon in pid $pid never stopped."
                @ daemons_sd += 1
                endif
            end
        end
    else
        if ($?host) then
            echo "    No Reliaty Backup daemons are running on $host."
        else
            echo "    No Reliaty Backup daemons are running."
            endif
        endif

    unset k_any pid pids daemon daemons i pid2 i_cmd i_blk
    rm -f $tmpf >&/dev/null

trynext:
    if ($?multi) then
        shift
        if ($#argv > 0) then
            set host = "$1"
            goto again
            endif
        endif

exit (0)
