#! /bin/bash
. acsstartupAcsPorts
. acsstartupLogging.sh
. acsstartupConstants
. acsstartupAcsInstance
#*******************************************************************************
# E.S.O. - VLT project
#
# who       when      what
# --------  --------  ----------------------------------------------
#

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

###
### ----------- 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 -------------
export HOST=`getIP`


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

########################################################################
#SANITY CHECKS----------------------------------------------------------
ACS_INSTANCE_DIR=`getInstanceDirName $ACS_INSTANCE`

#Cannot shutdown everything if this user does not own the processes.
if [ ! -w $ACS_INSTANCE_DIR ]
then
    ACS_LOG_ERROR "acsRestartIrd" "Unable to shutdown because the lock directory '$ACS_INSTANCE_DIR' is not owned by '$USER'!"
    exit $EC_CANNOTUSE
fi

#go to the directory where all process IDs are stored
cd $ACS_INSTANCE_DIR &> /dev/null || (ACS_LOG_ERROR "acsRestartIrd" "Cannot cd to $ACS_INSTANCE_DIR"; exit $EC_FAILURE)

ACS_LOG_INFO "acsRestartIrd" "Stopping the CORBA Interface Repository"
if [ -e ACS_IR_PID ]
then
    acsKillProc ACS_IR_PID 2> /dev/null || (ACS_LOG_ERROR "acsRestartIrd" "Cannot kill the interface repository")
    rm -f ACS_IR_PID
else
    ACS_LOG_ERROR "acsRestartIrd" "The file containing the interface repository's process ID, 'ACS_IR_PID', does not exist"
fi

ACS_IR_PORT=`getIRPort`
IR_IOR=$ACS_INSTANCE_DIR/InterfaceRepositoryIOR

ACS_LOG_INFO "acsRestartIrd" "Starting the CORBA Interface Repository"
LD_LIBRARY_PATH=$MICO_HOME/lib:$LD_LIBRARY_PATH
$MICO_HOME/bin/ird -ORBNoResolve -ORBIIOPAddr inet:$HOST:$ACS_IR_PORT --ior $IR_IOR  &

ACS_IR_PID=$!
echo $ACS_IR_PID > $ACS_INSTANCE_DIR/ACS_IR_PID

acsIrfeed  -IRcorbaloc corbaloc::$HOST:$ACS_IR_PORT/InterfaceRepository

#
# ___oOo___
