#! /bin/bash
. acsstartupLogging.sh
. acsstartupConstants
. acsstartupAcsInstance
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: acsStopORBSRVC,v 1.43 2012/05/31 14:56:22 acaproni Exp $"
#
# who       when      what
# --------  --------  ----------------------------------------------
# azagar   2008-08-08 split on seperate service shutdown scripts
# mschilli 2004-04-16 added proper command line parsing
# david 2003-05-14 changed to bash for tat tests
# gchiozzi 2002-12-12 acsLogSvc was missing for SunOS
# gchiozzi 2002-11-22 Added stopping CDB
# gchiozzi 2002-09-30 Removed CDB Server. Not needed any more.
# bjeram 2002-04-10 added kill acslogsvc
# psivera  2002-03-18 ported to SUN Solaris
# gchiozzi 2001-09-19 created
#

#************************************************************************
#   NAME
# 
#   SYNOPSIS
# 
#   DESCRIPTION
#
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#
#   BUGS     
#
#------------------------------------------------------------------------
#

#ACS_LOG_COMMAND $@
COMMAND=`basename $0`
PARAMS=$*

###
### ----------- Command Line Parsing ---------------------

#
# These will contain the parsing results (CL_XXX, CL = command line)
#
CL_BASEPORT=
CL_HELP=

#
# These options can be recognized (longopts comma-separated. colon means argument is required)
#
LONGOPTS=help,baseport:
SHORTOPTS=hb:

#
# Usage info. Be nice and keep this up-to-date!
#
function printUsage {
   echo "Stops the Orb services for Acs; note that acsStop is an alternative for this"
   echo ""
   echo "Usage: `basename $0` [OPTIONS]  "
   echo "Options: "
   echo "   -b | -baseport INSTANCE        the acs instance (0-9) you want to use"
   echo "   -h | -help                     prints this help and exits"
}

#
# Run getopt (posixly_correct needed). We run twice:
# First run is simply to check the commandline for correctness
# Second run is the real deal which replaces the command line args with getopt's output
export POSIXLY_CORRECT=1

getopt -n `basename $0` -Q -u -a -l $LONGOPTS $SHORTOPTS "$@" || {
   printUsage
	exit $EC_BADARGS;
}

set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`

#
# Iterate over getopt's output and set CL_XXX variables accordingly
#
while : 
do
	case "$1" in
	--baseport)         CL_BASEPORT=$2 ; shift ;;
	-b)                 CL_BASEPORT=$2 ; shift ;;
	--help)             CL_HELP=true ;; 
	-h)                 CL_HELP=true ;; 
	--) break ;;
	esac
	shift
done
shift

# restore 
export POSIXLY_CORRECT=
unset POSIXLY_CORRECT

if [ "$CL_HELP" ] ; then
   printUsage
   exit $EC_OK
fi

#
# (Note: Rest of command line now in $@ )
#
### ---------- End of Command Line Parsing -------------

#Check command-line args for baseport option
if [ "$CL_BASEPORT" ]
then
  ACS_INSTANCE="$CL_BASEPORT"
fi

INSTANCE_DIR=`getInstanceDirName $ACS_INSTANCE`

ACS_LOG_COMMAND $@


########################################################################
#SANITY CHECKS-------------------------------------------------------

if [ ! -d $INSTANCE_DIR ]
then
    ACS_LOG_DEBUG "$COMMAND" "Lock directory '$INSTANCE_DIR' does not exist!"

elif [ ! -w $INSTANCE_DIR ]
then
    ACS_LOG_ERROR "$COMMAND" "Lock directory '$INSTANCE_DIR' is not owned by '$USER'!"
    exit $EC_CANNOTUSE
fi

########################################################################
#SHUTDOWN SERVICES------------------------------------------------------

ACS_LOG_INFO "$COMMAND" "Stopping the ORB Services and CDB using 'ACS_INSTANCE=$ACS_INSTANCE'"

acsAlarmService -k $PARAMS
acsConfigurationDatabase -k $PARAMS
acsNamingService -k $PARAMS
acsNotifyService -k $PARAMS
acsNotifyService -k -n Logging $PARAMS
acsNotifyService -k -n Archive $PARAMS
acsNotifyService -k -n Alarm $PARAMS
acsLoggingService -k $PARAMS
acsInterfaceRepository -k $PARAMS
acsACSLogService -k $PARAMS

########################################################################
#FINAL CLEANUP----------------------------------------------------------
PIDSFILE=$INSTANCE_DIR/$ACS_PIDSFILE
if [ -e $PIDSFILE ]
then
    file=`cat $PIDSFILE`
    
    ACS_LOG_INFO "$COMMAND" "Killing any rogue ACS processes..."
    for line in $file
    do
      acsKillProc $line &> /dev/null
    done
fi

ACS_LOG_INFO "$COMMAND" "Freeing $INSTANCE_DIR"
if [ -d $INSTANCE_DIR ]; then
    mv $INSTANCE_DIR $INSTANCE_DIR.`date +%s` &> /dev/null || (ACS_LOG_ERROR "$COMMAND" "Cannot move $INSTANCE_DIR")
fi


#
# ___oOo___
