#!/bin/sh
#
# $Header: emll/install/clean_cron_proc.sh /main/3 2009/11/26 23:05:13 aghanti Exp $
#
# clean_cron_proc.sh
#
# Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      clean_cron_proc.sh - <one-line expansion of the name>
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    aghanti     10/08/09 - Removal of dependency on xpg4 utilities
#    kthiruma    06/03/09 - XbranchMerge kthiruma_xbranchmerge_all_103110_txns
#                           from st_emll_10.3.1
#    kthiruma    03/20/09 - Bug8347470
#    kthiruma    03/20/09 - Creation
#

# 
# Reset the locale to ENGLISH for command line operations.
LC_ALL=C
export LC_ALL

#Variable declations
_binDir=/bin
_usrBinDir=/usr/bin
_usrLocalBinDir=/usr/local/bin

#Assuming system temp directory is /temp
CCR_TEMP=/tmp

# Constant declarations for exit values.
SUCCESS=0
ERR_INVALID_USAGE=1

#Initialization 
if [ -f ${_binDir}/cut ]
then
    CUT=${_binDir}/cut
elif [ -f ${_usrBinDir}/cut ]
then
    CUT=${_usrBinDir}/cut
fi

if [ -f ${_binDir}/uname ]
then
    UNAME=${_binDir}/uname
elif [ -f ${_usrBinDir}/uname ]
then
    UNAME=${_usrBinDir}/uname
fi

PLATFORM=`$UNAME | $CUT -f1`

if [ "$PLATFORM" = "Linux" ]
then

ECHO=echo
KILL=kill
RM=${_binDir}/rm
PS=${_binDir}/ps
AWK=${_binDir}/awk

else

ECHO=${_usrBinDir}/echo
KILL=${_usrBinDir}/kill
RM=${_usrBinDir}/rm
PS=${_usrBinDir}/ps
AWK=${_usrBinDir}/awk

fi

if [ -f ${_binDir}/egrep ]
then
    EGREP=${_binDir}/egrep
elif [ -f ${_usrBinDir}/egrep ]
then
    EGREP=${_usrBinDir}/egrep
fi

#Variable Initialization
CRONTAB=${_usrBinDir}/crontab
RMF="$RM -f"
RMRF="$RM -rf"

# Bug 5931631 - getopts issue with /bin/sh on SunOS
# If SunOS, run this script with /usr/xpg4/bin/sh (if available)
if [ $PLATFORM = "SunOS" ]
then
    if [ -f /usr/xpg4/bin/sh ]
    then
        if [ -z "${_xpg4ShAvbl_clean_cron_proc}" ]
        then
            _xpg4ShAvbl_clean_cron_proc=1
            export _xpg4ShAvbl_clean_cron_proc
            /usr/xpg4/bin/sh $0 "$@"
            exit $?
        fi
    fi
fi

if [ $PLATFORM = "OSF1" ]
then
    if [ -z "${BIN_SH}" ]
    then
        BIN_SH=xpg4
        export BIN_SH
        $0 "$@"
        exit $?
    fi
fi


#-----------------------------Sub routines -------------------------
#To remove crontab entry
remove_crontab_entry()
{
# Default the temporary directory to /tmp for where temporary files are written.
#    export CCR_TEMP
# Save the variable for future reference
_tmpCrontab=${CCR_TEMP}/crontab.$$

  $CRONTAB -l > /dev/null 2>&1
  # Proceed only if crontab command succeeded (eg. crontab is not disabled)
  if [ $? -eq 0 ]
  then
    $CRONTAB -l > ${_tmpCrontab}

    $EGREP -v "JAVA_HOME=[^ ]* ${CCR_HOME}/bin/emCCR" ${_tmpCrontab} > ${_tmpCrontab}.1
    $CRONTAB ${_tmpCrontab}.1
    $RMF ${_tmpCrontab}
    $RMF ${_tmpCrontab}.1
  fi
_returnData=$?
}

#Kill the ccr(nmz) process
remove_ccr_process()
{
_tmpProc=${CCR_TEMP}/proc.$$
line=""

$PS -ef | $EGREP "${CCR_HOME}/bin/nmz"| $EGREP -v "$EGREP ${CCR_HOME}/bin/nmz" | $EGREP -v  `basename $0` | $AWK -F" " '{print $2}' > ${_tmpProc}

while read line
do

$KILL -9 $line  > /dev/null  2>&1

done < ${_tmpProc}

$RMF ${_tmpProc}

_returnData=$?
}

#usage help
usage()
{
$ECHO " ";
$ECHO "Usage: `basename $0` "
$ECHO "-o ORACLE_HOME"
$ECHO "-l Temporary Directory : Default /tmp directory"
$ECHO "-h <Usage Help>";
$ECHO " ";
}

#-------------------------Main Code----------------------------
if [ $# -lt 1 ] 
then
    usage
    exit $SUCCESS
fi

#Read commandLine arguements
while getopts :o:l:h _option
do
    if [ $? -eq 0 ]
    then
        case ${_option}
        in
            h)  usage
                exit $SUCCESS;;

            o)  ORACLE_HOME=${OPTARG};;

            l)  CCR_TEMP=${OPTARG};;

            :)
                $ECHO "Required value for option \"-${OPTARG}\" is missing."
                $ECHO ""
                usage
                exit  $ERR_INVALID_USAGE;;

            *)
                $ECHO "Invalid command qualifier specified \"-${OPTARG}\""
                $ECHO ""
                usage
                exit $ERR_INVALID_USAGE;;

            \?)
                $ECHO "Invalid command qualifier specified \"-${OPTARG}\""
                $ECHO ""
                usage
                exit $ERR_INVALID_USAGE;;

        esac
    fi
done

$ECHO "PLATFORM : $PLATFORM"

#Define CCR home
CCR_HOME=${ORACLE_HOME}/ccr

#Remove Crontab entry for the ccr home
remove_crontab_entry
$ECHO "CRONTAB : ${_returnData}"

#kill the ccr process
remove_ccr_process
$ECHO "NMZ : ${_returnData}"

#return with exit value
exit ${_returnData}

