#!/bin/bash
# Copyright (c) Members of the EGEE Collaboration. 2004.
# 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.
#
# Common init.d script for the gLite File Transfer Agents
#
# chkconfig: 2345 80 20
#
# Authors: 
#      Akos Frohner <Akos.Frohner@cern.ch>
#      Gavin McCance <Gavin.McCance@cern.ch>
#
# description: File Transfer Agents 

USAGE="$0 (start|stop|status|restart|reconfig|debug|kill) [--instance full_name]* [--slow]"
   
TEMP=$(getopt -o i:Z --long instance:,slow -- "$@")
eval set -- "$TEMP"

PACKAGE='glite-data-transfer-agents'
INSTANCES=""
SU='/bin/su'
SLOW=""

while true; do
    case "$1" in
        -i|--instance)
            shift
            INSTANCES="$INSTANCES $1"
            shift
            ;;
        -Z|--slow)
            SLOW="True"
            shift
            ;;
        --)
            # end of options
            shift
            break
            ;;
    esac
done

#################################################################
# default configuration
#
# Yaim gLite defaults
if [ -r /etc/profile.d/gliteenv.sh ]; then
   source /etc/profile.d/gliteenv.sh
else
   # Legacy gLite defaults
   [ -r /etc/glite/profile.d/glite_setenv.sh ] && source /etc/glite/profile.d/glite_setenv.sh
fi

if [ -f "/usr/bin/glite-transfer-channel-addmanager" ]; then
	GLITE_LOCATION='/usr'
	GLITE_LOCATION_ETC='/etc'
else
	GLITE_LOCATION='/opt/glite'
	GLITE_LOCATION_ETC=${GLITE_LOCATION}/etc
fi 


if [ ! -d "$GLITE_LOCATION" ]; then
    echo "Error: '$GLITE_LOCATION' does not exsits." >&2
    echo "Error: Please specify GLITE_LOCATION!" >&2
    echo $USAGE >&2
    exit 1
fi

# service defaults
# Run yaim sysconfig script
if [ -r /etc/sysconfig/${PACKAGE} ]; then
   source /etc/sysconfig/${PACKAGE}
else
   # Legacy support for gLite <=1.5
   [ -r $GLITE_LOCATION_ETC/$PACKAGE.conf ] && source $GLITE_LOCATION_ETC/$PACKAGE.conf
fi

#################################################################
# configuration directory
#
# Default now to the standard LCG edguser
GLITE_SERVICE_USER=${GLITE_SERVICE_USER:-edguser}

# checking if the user exists
id -u $GLITE_SERVICE_USER >/dev/null 2>/dev/null
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
    echo "ERROR: '$GLITE_SERVICE_USER' user does not exists!"
    exit 1
fi

#################################################################
# service certificate
#
if [ -d "/etc/grid-security" ]; then
    if [ -r "/etc/grid-security/hostcert.pem" ]; then
        cp -up /etc/grid-security/hostcert.pem /etc/grid-security/$PACKAGE-cert.pem
        chown $GLITE_SERVICE_USER /etc/grid-security/$PACKAGE-cert.pem
    fi
    if [ -r "/etc/grid-security/hostkey.pem" ]; then
        cp -up /etc/grid-security/hostkey.pem /etc/grid-security/$PACKAGE-key.pem
        chown $GLITE_SERVICE_USER /etc/grid-security/$PACKAGE-key.pem
    fi
fi

#################################################################
# configuration directory
#
CONFIGDIR="$GLITE_LOCATION_ETC"
if [ ! -d "$CONFIGDIR" ]; then
    echo "ERROR: '$CONFIGDIR' configuration directory does not exists!" >&2
    exit 1
fi

#################################################################
# all, if no instances were specified
#
if [ -z "$INSTANCES" ]; then
    if [ -d "$CONFIGDIR/$PACKAGE.d/" ]; then
        INSTANCES=$(ls $CONFIGDIR/$PACKAGE.d/glite-transfer-*.properties.xml | \
                    xargs -i basename {} .properties.xml) 2>/dev/null
    fi
    if [ -z "$INSTANCES" ]; then # ONLY do legacy support if there are no proper instances found
       # legacy support
       INSTANCES="$INSTANCES "$(ls $CONFIGDIR/glite-transfer-*.properties.xml | \
                   xargs -i basename {} .properties.xml) 2>/dev/null
    fi
fi

# error, if there is still nothing
if [ -z "$INSTANCES" ]; then
    echo "ERROR: no service instances were found in '$CONFIGDIR'" >&2 
    exit 1
fi

#################################################################
# main 
#

case $1 in 
    start)
        command='start'
        ;;
    stop)
        command='stop'
        ;;
    debug)
        command='debug'
        ;;
    kill)
        command='kill'
        ;;
    restart)
        command='restart'
        ;;
    status)
        command='status'
        ;;
    reconfig)
        command='reconfig'
        ;;
    *)
        echo $USAGE
        exit 2
        ;;
esac

if [ "x$SLOW" != "x" ]; then
   # Sleep for a while
   echo "Slow down $command"
fi

# the actual work is done by the config-service
for instance in $INSTANCES; do
    # however we check if the service is disabled
    if [ "$command" = "start" -o "$command" = "restart" ]; then
       grep -s -q $instance /etc/${PACKAGE}.disabled $CONFIGDIR/${PACKAGE}.disabled
       if [ $? = 0 ]; then
          echo "Ignoring disabled instance: $instance"
          continue
       fi
    fi
    if [ "x$SLOW" != "x" ]; then
       # Sleep for a while
       sleep `echo $[$RANDOM % 120 + 1]`
    fi
    # Doing the real work, by calling a wrapper script, which
    # set all the necessary environmental variables for the binary.
    sleep 5
    $SU - $GLITE_SERVICE_USER -s /bin/bash -c "$GLITE_LOCATION/sbin/glite-data-config-service-wrapper $PACKAGE $instance $command"
done

