#!/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.
#
# Startup script for PEP daemon server
#
# chkconfig: 345 97 97
#
# description:  PEP daemon server startup script
#
# processname: pepd
#
# Author(s): Maria Alandes Pradillo <yaim-contact@cern.ch>
#            Valery Tschopp <argus-support@cern.ch>
#
# Version: V1.3
#
# Date: 11/02/2011
###############################################################################
# set -x

# PEP server control script
PEPDCTL=/usr/sbin/pepdctl

if [ `id -u` -ne 0 ]; then
    echo "You need root privileges to run this script"
    exit 1
fi 

# source optional system configuration
if [ -r /etc/sysconfig/argus-pepd ]; then
   . /etc/sysconfig/argus-pepd
fi

# export the variable for the $PEPDCTL script
[ -n "$JAVACMD" ] && export JAVACMD="$JAVACMD"
[ -n "$PEPD_JOPTS" ] && export PEPD_JOPTS="$PEPD_JOPTS"
[ -n "$PEPD_HOME" ] && export PEPD_HOME="$PEPD_HOME"
[ -n "$PEPD_CONF" ] && export PEPD_CONF="$PEPD_CONF"
[ -n "$PEPD_CONFDIR" ] && export PEPD_CONFDIR="$PEPD_CONFDIR"
[ -n "$PEPD_LOGDIR" ] && export PEPD_LOGDIR="$PEPD_LOGDIR"
[ -n "$PEPD_LIBDIR" ] && export PEPD_LIBDIR="$PEPD_LIBDIR"
[ -n "$PEPD_ENDORSEDDIR" ] && export PEPD_ENDORSEDDIR="$PEPD_ENDORSEDDIR"

case "$1" in
    start)
        $PEPDCTL start
        ;; 
    stop)
        $PEPDCTL stop
        ;;
    restart)
        $PEPDCTL stop
        sleep 5
        $PEPDCTL start
        ;;
    status)
        $PEPDCTL status
        ;;
    clearcache)
        $PEPDCTL clearResponseCache
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status|clearcache}"
        exit 1
        ;;
esac

exit $?

