#! /bin/bash
#
# Copyright (c) Members of the EGEE Collaboration. 2006-2010.
# See http://www.eu-egee.org/partners/ for details on the copyright holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#set -x

# PEP server home
PEPD_HOME=${PEPD_HOME-"/usr/share/argus/pepd"}

# config, log and lib directories
PEPD_CONFDIR=${PEPD_CONFDIR-"$PEPD_HOME/conf"}
PEPD_LOGDIR=${PEPD_LOGDIR-"$PEPD_HOME/logs"}
PEPD_LIBDIR=${PEPD_LIBDIR-"$PEPD_HOME/lib"}
PEPD_ENDORSEDDIR=${PEPD_ENDORSEDDIR-"$PEPD_LIBDIR/endorsed"}

# configuration file and check
PEPD_CONF=${PEPD_CONF-"$PEPD_CONFDIR/pepd.ini"}
if [ ! -r "$PEPD_CONF" ]; then
    echo "$0: ERROR: config file $PEPD_CONF not readable"
    exit 1
fi

# java command and check
JAVACMD=${JAVACMD-`which java`}
if [ ! -x "$JAVACMD" ] ; then
    echo "$0: ERROR: 'java' not available in PATH"
    exit 1
fi


# add in the dependency .jar files from the lib directory
jarfiles="$PEPD_LIBDIR/*.jar"
for jar in $jarfiles
do
    # if the directory is empty, then it will return the input string
    # this is stupid, so case for it
    if [ -f "$jar" ] ; then
        LOCALCLASSPATH="$LOCALCLASSPATH":"$jar"
    fi
done
# check classpath
if [ -z "$LOCALCLASSPATH" ]; then
    echo "$0: ERROR: LOCALCLASSPATH empty, check the $PEPD_LIBDIR directory"
    exit 1
fi
# check endorsed directory
if [ ! -d "$PEPD_ENDORSEDDIR" ]; then
    echo "$0: ERROR: $PEPD_ENDORSEDDIR does not exist"
    exit 1
fi
# set the mandatory endorsed dir and the classpath
PEPD_JOPTS="-Djava.endorsed.dirs=$PEPD_ENDORSEDDIR -classpath $LOCALCLASSPATH $PEPD_JOPTS"

# set the mandatory PEP server directory properties
PEPD_JOPTS="-Dorg.glite.authz.pep.home=$PEPD_HOME -Dorg.glite.authz.pep.confdir=$PEPD_CONFDIR -Dorg.glite.authz.pep.logdir=$PEPD_LOGDIR $PEPD_JOPTS"

function executeAdminCommand {
    ADMIN_HOST=`sed 's/ //g' $PEPD_CONF | grep "^adminHost" | awk 'BEGIN {FS="="}{print $2}'`
    if [ -z "$ADMIN_HOST" ] ; then
       ADMIN_HOST="127.0.0.1"
    fi

    ADMIN_PORT=`sed 's/ //g' $PEPD_CONF | grep "^adminPort" | awk 'BEGIN {FS="="}{print $2}'`
    if [ -z "$ADMIN_PORT" ] ; then
       ADMIN_PORT="8155"
    fi
    
    ADMIN_PASS=`sed 's/ //g' $PEPD_CONF | grep "^adminPassword" | awk 'BEGIN {FS="="}{print $2}'`
    
    $JAVACMD $PEPD_JOPTS 'org.glite.authz.common.http.JettyAdminServiceCLI' $ADMIN_HOST $ADMIN_PORT $1 $ADMIN_PASS
}

function start {
    # Run the PDP
    $JAVACMD $PEPD_JOPTS 'org.glite.authz.pep.server.PEPDaemon' $PEPD_CONF &
}


function print_help {
   echo "PEP Daemon control script"
   echo "Usage:"
   echo "  $0 start   - to start the service"
   echo "  $0 stop    - to stop the service" 
   echo "  $0 status  - print PEP daemon status"
   echo "  $0 clearResponseCache - clears the PEP daemon PDP response cache"
}

case "$1" in
    start)
        start
        ;;
    stop)
        executeAdminCommand 'shutdown' 
        ;;
    status) 
        executeAdminCommand 'status'
        ;;
    clearResponseCache) 
        executeAdminCommand 'clearResponseCache'
        ;;
    *) 
        print_help 
        exit 1
        ;;
esac
exit $? 

