#! /bin/bash
. acsstartupLogging.sh
. acsstartupAcsPorts
#*******************************************************************************
# ALMA - Atacama Large Millimiter Array
# (c) European Southern Observatory, 2002
# Copyright by ESO (in the framework of the ALMA collaboration)
# and Cosylab 2002, All rights reserved
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA 02111-1307  USA
#
# "@(#) $Id: cdbjDALClearCache,v 1.13 2012/01/26 15:23:49 hsommer Exp $"
#
# who       when      what
# --------  --------  ----------------------------------------------
# carlita 2006-06-20 using DAL_REFERENCE when the -k option is not used
# dvitas 2003-04-14 changed package name and removed unused argument
# bjeram 2002-11-22 removed -OAport
# bjeram 2002-10-24 created
#

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

CL_DAL_REFERENCE=$DAL_REFERENCE
CL_CURL=
CL_HELP=

if [ "X$1" != "X" ] && [ "X$1" != "XPASS_OPTIONS" ]
then 
	CL_CURL=$1
	shift;
fi

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

#
# Usage info. Be nice and keep this up-to-date!
#
function printUsage {
   echo "Wrapper for the java command: ClearCache"
   echo ""
   echo "Usage: `basename $0` [curl] [OPTIONS] [PASS_OPTIONS [COMMAND_OPTIONS]]"
   echo "Options: "
   echo "   -d DAL_REFERENCE        uses explicity gived DAL_REFERENCE"
   echo "   -h | -help              prints this help and exits"
   echo ""
   echo "  PASS_OPTIONS             the literal string that indicates more options to be passed"
   echo "  COMMAND_OPTIONS          the options to be passed directly to the command"
   echo ""
   echo "Environment Variables:"
   echo "  DAL_REFERENCE     | if the option -d is not set, then uses this variable"
   echo "  MANAGER_REFERENCE | if the option -d is not set and DAL_REFERENCE is not "
   echo "                      set, uses this to construct the dal reference"
   echo "  HOST              | if neither of the above is set, this is used to      "
   echo "                          create the value for the option -ORBInitRef"
}

#
# 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
	-d)                 CL_DAL_REFERENCE=$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
   echo ""
   echo "Calling to ClearCache whit -h option and exit"
   acsStartJava -clientVM com.cosylab.cdb.jdal.ClearCache -h
   exit $EC_OK
fi


############################################################

# Set logger levels to INFO unless the env vars are set already.
if [ "X$ACS_LOG_STDOUT" = "X" ]; then
    export ACS_LOG_STDOUT=4
fi
if [ "X$ACS_LOG_CENTRAL" = "X" ]; then
    export ACS_LOG_CENTRAL=4
fi

# Set JVM_GC_LOG_NAME so that acsStartJava will run the JVM printing garbage collector details to file "cdbjDALClearCache.gclog". 
export JVM_GC_LOG_NAME=cdbjDALClearCache


HOST=`getIP`
ACS_CDB_PORT=`getCDBPort`

if [ "X$CL_DAL_REFERENCE" = "X" ] 
then
  #if not set then use the MANAGER_REFERENCE to construct it
  #if this one is not set neither, then create it using HOST
    if [ "X$MANAGER_REFERENCE" != "X" ]
    then
	extracted_corbaloc=${MANAGER_REFERENCE%:*}
	CL_DAL_REFERENCE="$extracted_corbaloc:$ACS_CDB_PORT/CDB"
    else
	CL_DAL_REFERENCE="corbaloc::$HOST:$ACS_CDB_PORT/CDB"
    fi
fi

if [ "X$1" != "X" ] && [ "X$1" != "XPASS_OPTIONS" ]
then
   printUsage
   exit $EC_OK
fi

shift;
acsStartJava -clientVM com.cosylab.cdb.jdal.ClearCache $CL_CURL -d $CL_DAL_REFERENCE $*

