#! /bin/bash
. acsstartupPids
#---------------------------------------------------------
#Simple script which just invalidates the IFR persistence file
#if possible and reports if there is any problem. It also 
#checks the status of the IFR Service to report if a restart
#is required
#---------------------------

### store current ACS_INSTANCE
ACE_INSTANCE_OLD=$ACS_INSTANCE
COMMAND=`basename $0`

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

#
# These will contain the parsing results (CL_XXX, CL = command line)
#
CL_BASEPORT=$ACS_INSTANCE
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 "Retruns status of an Acs instance"
   echo ""
	echo "Usage: `basename $0` [OPTIONS]  "
	echo "Options: "
	echo "   -b | -baseport INSTANCE        the acs instance (0-9) you want to invalidate cache for"
        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 ACS_INSTANCE=$CL_BASEPORT

PERSISTENCE_FILE=$ACS_TMP/ifr_cache.$ACS_INSTANCE
if [ ! -e $PERSISTENCE_FILE ]; then
  ACS_LOG_INFO "$COMMAND" "There is no IFR persistence file available"
else
  if [ ! -w $PERSISTENCE_FILE ]; then
    ACS_LOG_ERROR "$COMMAND" "No write access to IFR persistence file"
  else
    rm -f $PERSISTENCE_FILE
    if PID=`getIRPid 2> /dev/null`; then
      ACS_LOG_INFO "$COMMAND" "IFR Service is running. Since IFR persistence file has been invalidated you should restart the IFR Service"
    fi
  fi
fi

if [ $ACS_INSTANCE_OLD ]; then
    export ACS_INSTANCE=$ACS_INSTANCE_OLD
fi

