#! /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

# PDP 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 "$PDP_HOME" ] && PDP_HOME=`cd "$PRGDIR/.." ; pwd`    

# config, log and lib directories
PDP_CONFDIR=${PDP_CONFDIR:-"$PDP_HOME/conf"}
PDP_LOGDIR=${PDP_LOGDIR:-"$PDP_HOME/logs"}
PDP_LIBDIR=${PDP_LIBDIR:-"$PDP_HOME/lib"}
PDP_ENDORSEDDIR=${PDP_ENDORSEDDIR:-"$PDP_LIBDIR/endorsed"}
PDP_PROVIDEDDIR=${PDP_PROVIDEDDIR:-"$PDP_LIBDIR/provided"}

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

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

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

# uses pdp-pep-common, canl and bouncycastle from
# the install RPM packages
LOCALCLASSPATH="/usr/share/java/argus-pdp-pep-common.jar"
if [ "$PDP_USE_OS_CANL" = "false" ] ; then
    for jar in $PDP_PROVIDEDDIR/canl-*.jar
    do
        if [ -f "$jar" ] ; then
            LOCALCLASSPATH="$LOCALCLASSPATH:$jar"
        fi
    done
else
    LOCALCLASSPATH="$LOCALCLASSPATH:/usr/share/java/canl.jar"
fi
if [ "$PDP_USE_OS_BCPROV" = "false" ] ; then
    for jar in $PDP_PROVIDEDDIR/bcprov-*.jar
    do
        if [ -f "$jar" ] ; then
            LOCALCLASSPATH="$LOCALCLASSPATH:$jar"
        fi
    done
else
    LOCALCLASSPATH="$LOCALCLASSPATH:/usr/share/java/bcprov-1.46.jar"
fi


# include all embedded jar
jarfiles="$PDP_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
# remove first :
LOCALCLASSPATH="$(echo $LOCALCLASSPATH | sed 's/^://g')"

# check classpath
if [ -z "$LOCALCLASSPATH" ]; then
    echo "$0: ERROR: LOCALCLASSPATH empty, check the $PDP_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 "$PDP_ENDORSEDDIR" ]; then
    echo "$0: ERROR: $PDP_ENDORSEDDIR does not exist"
    exit 1
fi
# set default max memory
if [ -z "$PDP_JOPTS" ]; then
    PDP_JOPTS="-Xmx256M"
fi
# set the mandatory endorsed dir and the classpath
PDP_JOPTS="-Djava.endorsed.dirs=$PDP_ENDORSEDDIR -classpath $LOCALCLASSPATH $PDP_JOPTS"

# set the mandatory PEP server directory properties
PDP_JOPTS="-Dorg.glite.authz.pdp.home=$PDP_HOME -Dorg.glite.authz.pdp.confdir=$PDP_CONFDIR -Dorg.glite.authz.pdp.logdir=$PDP_LOGDIR $PDP_JOPTS"

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

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

function start {
    # Run the PDP
    $JAVACMD $PDP_JOPTS 'org.glite.authz.pdp.server.PDPDaemon' $PDP_CONF &
    if [ -n "$PDP_PID" ]; then
      echo $! > $PDP_PID
    fi
}

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


function print_help {
   echo "PDP control script"
   echo "Usage:"
   echo "  $0 start   - start the PDP service"
   echo "  $0 stop    - stop the PDP service" 
   echo "  $0 status  - print PDP status"
   echo "  $0 version - print PDP version"
   echo "  $0 reloadPolicy - reload the PDP policies from the PAP"

}

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