#! /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
PRG="$0"
while [ -h "$PRG" ]; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
    PRG=`dirname "$PRG"`/"$link"
    fi
done
PRGDIR=`dirname "$PRG"`
[ -z "$PEPD_HOME" ] && PEPD_HOME=`cd "$PRGDIR/.." ; pwd`    

# 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"}
PEPD_PROVIDEDDIR=${PEPD_PROVIDEDDIR:-"$PEPD_LIBDIR/provided"}

# PID file location
PEPD_PID=${PEPD_PID:-"/var/run/argus-pepd.pid"}

# 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

# uses pep-common and pdp-pep-common from the installed packages
# uses canl, voms-api-java and bouncycastle from the install RPM packages or provided
LOCALCLASSPATH="/usr/share/java/argus-pdp-pep-common.jar:/usr/share/java/argus-pep-common.jar"
if [ "$PEPD_USE_OS_CANL" = "false" ] ; then
    for jar in $PEPD_PROVIDEDDIR/canl-*.jar
    do
        if [ -f "$jar" ] ; then
            LOCALCLASSPATH="$LOCALCLASSPATH:$jar"
        fi
    done
else
    LOCALCLASSPATH="$LOCALCLASSPATH:/usr/share/java/canl.jar"
fi
if [ "$PEPD_USE_OS_BCPROV" = "false" ] ; then
    for jar in $PEPD_PROVIDEDDIR/bcprov-*.jar
    do
        if [ -f "$jar" ] ; then
            LOCALCLASSPATH="$LOCALCLASSPATH:$jar"
        fi
    done
else
    LOCALCLASSPATH="$LOCALCLASSPATH:/usr/share/java/bcprov-1.46.jar"
fi
if [ "$PEPD_USE_OS_VOMS" = "false" ] ; then
    for jar in $PEPD_PROVIDEDDIR/voms-api-java-*.jar
    do
        if [ -f "$jar" ] ; then
            LOCALCLASSPATH="$LOCALCLASSPATH:$jar"
        fi
    done
else
    LOCALCLASSPATH="$LOCALCLASSPATH:/usr/share/java/voms-api-java3.jar"
fi
if [ "$PEPD_USE_OS_BCMAIL" = "false" ] ; then
    for jar in $PEPD_PROVIDEDDIR/bcmail-*.jar
    do
        if [ -f "$jar" ] ; then
            LOCALCLASSPATH="$LOCALCLASSPATH:$jar"
        fi
    done
else
    LOCALCLASSPATH="$LOCALCLASSPATH:/usr/share/java/bcmail-1.46.jar"
fi

# include all embedded jar
jarfiles="$PEPD_LIBDIR/*.jar"
for jar in $jarfiles
do
    # if the directory is empty, then it will return the input string
    if [ -f "$jar" ] ; then
        LOCALCLASSPATH="$LOCALCLASSPATH:$jar"
    fi
done

# remove first :
LOCALCLASSPATH="$(echo $LOCALCLASSPATH | sed 's/^://g')"

# check classpath
if [ -z "$LOCALCLASSPATH" ]; then
    echo "$0: ERROR: LOCALCLASSPATH empty, check the $PEPD_LIBDIR directory"
    exit 1
fi

# check that all jars in classpath exist
check_jarfiles="$(echo $LOCALCLASSPATH | sed 's/:/ /g')"
for jar in $check_jarfiles
do
    if [ ! -f "$jar" ] ; then
        echo "$0: ERROR: $jar in classpath does not exist"
        exit 1
    fi
done

# check endorsed directory
if [ ! -d "$PEPD_ENDORSEDDIR" ]; then
    echo "$0: ERROR: $PEPD_ENDORSEDDIR does not exist"
    exit 1
fi

# set default max memory
if [ -z "$PEPD_JOPTS" ]; then
    PEPD_JOPTS="-Xmx256M"
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="localhost"
    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 PEP Server (background)
    $JAVACMD $PEPD_JOPTS 'org.glite.authz.pep.server.PEPDaemon' $PEPD_CONF &
    if [ -n "$PEPD_PID" ]; then
      echo $! > $PEPD_PID
    fi
}

function version {
    # Print currently used Version of the PEPd
    $JAVACMD $PEPD_JOPTS 'org.glite.authz.pep.server.Version'
}
    


function print_help {
   echo "PEP Server 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 version - print PEP daemon version"
   echo "  $0 clearResponseCache - clears the PEP Server PDP response cache"
}

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