#!/bin/sh
#
# Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved. 
#
#   NAME
#     S92RB
#
#   DESCRIPTION
#     Oracle Secure Backup daemon startup and shutdown logic.
#
#   MODIFIED   (MM/DD/YY) 
#   asathyam   10/09/09 - Merge changes done in HP 10.3.0.1 after 10.3.0.1 of
#                         Linux was frozen
#                         Bug 8713171, 8713133 Export PATH for hpux
#   asathyam   12/24/08 - Fix code related to stopping obhttpd when observiced
#                         is stopped
#   jwadleig   12/18/08 - remove the code from the previous change
#   asathyam   12/02/08 - Kill obhttpd when observiced gets stopped
#   tdziedzi   05/18/05 - Product name change 
#   tdziedzi   05/18/04 - Executables name change 
#   tdziedzi   05/05/04 - Update vendor/product name, copyright info 
#
# ******************************************************************************
#  Old changes (pre ade migration):
#     06/15/02 jrw  update copyright/IP notice.
#     12/03/01 gms  v3 mods.
#     07/13/99 gms  s/QUIT/TERM/.
#     08/13/97 jrw  define dynamic QR dir.
#     12/01/96 jrw  port to alpha osf1.
#     01/12/96 jrw  changes for HP-UX 10.
#     11/28/94 jrw  setup to be modified by installation tools to point to real QR dir.
#     07/21/94 jrw  original coding.
#
#   Usage:
#     The Oracle Secure Backup installation tools place this file into the
#     appropriate system directory, one of /etc/rc2.d, /sbin/init.d,
#     /sbin/rc3.d, etc.  The system bootstrap and code notices the existence
#     of this file and invokes it at the appropriate time.

# In HP-UX During startup/shutdown PATH variable gets reset to /sbin
# We need /usr/bin path to STOP observiced during shutdown/reboot.

PATH=$PATH:/usr/bin
export PATH

case "$1" in
    'start_msg')
        echo "Start Oracle Secure Backup services daemon"
        ;;

    'start')
        # start the observiced

        if [ -f QSD_TO_BOOT ]
        then
            QSD=QSD_TO_BOOT
        else
            if [ -f /etc/observiced ]
            then
                QSD=/etc/observiced
            else
                echo "Can't find Oracle Secure Backup services daemon executable."
                exit 1
                fi
            fi

        echo "Starting Oracle Secure Backup services daemon."
        $QSD -b
        exit 0
        ;;

    'stop_msg')
        echo "Stop Oracle Secure Backup services daemon"
        ;;

    'stop')
        QSDPID="`/bin/ps -e | grep observi | egrep -v '(sh |grep )' | sed -e 's,^ *\([0-9][0-9]*\).*$,\1,'`"
        if [ "x$QSDPID" = "x" ]
        then
            echo "Oracle Secure Backup services daemon is not running."
            exit 1
            fi
        if [ `echo $QSDPID | grep " " >/dev/null` ]
        then
            S="s"
        else
            S=""
            fi
        echo "Stopping observiced (it's running in pid$S $QSDPID)"
        kill -TERM $QSDPID

        # When observiced gets killed, kill all the HTTPD processes also. They get started when admin observiced 
        # is started
        HTTPPID="`/bin/ps -ef | grep obhttpd | egrep -v '(sh |grep|nobody|www )' | sed -n 's/[ ]*[a-z]* *\([0-9]*\).*/\1/p'`"

        if [ "x$HTTPPID" != "x" ]
        then
            echo "Stopping  Httpd (it's running in pid $HTTPPID)"
            kill -TERM $HTTPPID
        fi
        ;;

    esac

