#############################################################################
# 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.
###############################################################################
#
#   File containing yaim's configuration for service gLite
#
#   Description: A single function is provided to manage the creation and
#               configuration of service gLite on the CREAM node
#               The list of services is readed from /etc/gLiteservices.
#		The scripts are stored in /etc/init.d.
#
#
#   Author: Massimo Sgaravatto adapted config_glite_initd provided by yaim-core
#
#
###############################################################################
function config_cream_glite_initd () {

    # Check the existence of the glite services list file
    if [ ! -f /etc/gLiteservices ] ; then
	yaimlog ABORT "The gLite services' list is not defined ! Exiting !"  
    else 
        mv /etc/gLiteservices /etc/gLiteservices.cream  
    fi

    yaimlog DEBUG "Write the gLite service startup function"
    cat > /etc/rc.d/init.d/gLite << EOF
#!/bin/bash
###############################################################################
#
#       Copyright (c) Members of the EGEE Collaboration. 2004
#       See http://eu-egee.org/partners/ for details on the copyright holders
#       For license conditions see the license file or http://eu-egee.org/license.html
#
#   Startup script for POST stage setup
#
#   chkconfig: 345 96 96
#
#   description:  gLite startup script
#
#   processname: gLite
#
#   Author(s): Marian ZUREK <Marian.ZUREK@cern.ch> 
#	       Nuno SILVA <Nuno.Orestes.Vaz.da.Silva@cern.ch>
#
#   Version: V1.1 for Yaim-core-4
#
#   Date: 07/08/2007
###############################################################################

. ${GRID_ENV_LOCATION}/grid-env.sh

 
GLITE_STARTUP_FILE=/etc/gLiteservices.cream

if [ ! -r \${GLITE_STARTUP_FILE} ]
then
        echo "File \${GLITE_STARTUP_FILE} not created (or not readable) ... exiting!"
        exit 1
fi

case "\$1" in 
    	start) 	SERVICE_LIST=\`cat \$GLITE_STARTUP_FILE\`
		for s in \`echo \${SERVICE_LIST}\`
                do
		  echo "*** \`basename \${s}\`:"; 
		  \${s} start
		  echo""
		done;;
 
        stop)	SERVICE_LIST=\`tac \$GLITE_STARTUP_FILE\`
                for s in \`echo \${SERVICE_LIST}\`
                do
		  echo "*** \`basename \${s}\`:";
		  \${s} stop
		  echo""
		done;;
 
        restart) echo "STOPPING SERVICES"; \$0 stop; 
                 echo "STARTING SERVICES"; \$0 start;;
 
        status)	SERVICE_LIST=\`cat \$GLITE_STARTUP_FILE\`
                for s in \`echo \${SERVICE_LIST}\`
                do
		  echo "*** \`basename \${s}\`:";
		  \${s} status
		  echo""
		done;;

        version) echo -n "emi version: "; rpm -q --qf %{V} emi-version; echo
                 echo -n "yaim version:  "; rpm -q --qf %{V} glite-yaim-core; echo ;;

        *) echo "====================================================================="
           echo "===== Usage: gLite { start | stop | restart | status | version} ====="
           echo "====================================================================="
           exit 1;;
    esac
 
exit 0
EOF

    yaimlog DEBUG "Make the gLite init script executable"
    chmod +x /etc/rc.d/init.d/gLite

    # Add the script to the service list    
    /sbin/chkconfig --add gLite

    return 0
}
