#!/bin/bash
#
#       /etc/rc.d/init.d/mdrepdaemon
#
# Starts the mdserver daemon
#
# chkconfig: 345 95 5
# description: Starts the metadata service replication daemon mdrepdaemon
# processname: mdrepdaemon

#Get OS information
if [ `uname -m` = "x86_64" ]
then OS_BIT=64
else OS_BIT=32
fi

OS_PLAT=`cat /etc/issue | grep Debian | head -c 3`

if [ -z $OS_PLAT ]; then
OS_PLAT=SL
fi

# Source function library.
if [ "$OS_PLAT" = "SL" ]; then
	. /etc/init.d/functions
else
	. /lib/lsb/init-functions
fi

if test x$GLITE_LOCATION = x ; then
  echo GLITE_LOCATION not set, assuming /usr
  GLITE_LOCATION=/usr
fi

if test x$GLITE_LOCATION_VAR = x ; then 
  echo GLITE_LOCATION_VAR not set, assuming /var
  GLITE_LOCATION_VAR=/var
fi

if test x$GLITE_LOCATION_ETC = x ; then 
  echo GLITE_LOCATION_ETC not set, assuming /etc
  GLITE_LOCATION_ETC=/etc
fi

if test ! -d ${GLITE_LOCATION_VAR}/log ; then
  mkdir -p ${GLITE_LOCATION_VAR}/log
fi

if test ! -x ${GLITE_LOCATION}/bin/mdrepdaemon ; then
  echo Error: ${GLITE_LOCATION}/bin/mdrepdaemon does not exist
  exit 0
fi

OPTIONS="-c${GLITE_LOCATION_ETC}/amgad.config -D -l${GLITE_LOCATION_VAR}/log/mdrepdaemon.log"
[ -e /etc/sysconfig/mdrepdaemon ] && . /etc/sysconfig/mdrepdaemon

RETVAL=0

#
#       See how we were called.
#

prog="mdrepdaemon"

start() {
	if [ "$OS_PLAT" = "SL" ]; then
        # Check if amgad is already running
        if [ ! -f /var/lock/subsys/mdrepdaemon ]; then
            echo -n "Starting $prog: "
						echo -n "$OPTIONS"
            daemon ${GLITE_LOCATION}/bin/mdrepdaemon $OPTIONS
            RETVAL=$?
            [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mdrepdaemon
            echo
				else 
            echo mdrepdaemon already running.  
            echo If you are sure that this is not the case, delete /var/lock/subsys/mdrepdaemon
            echo and try again.
            RETVAL=1
        fi
        return $RETVAL
	else
		AMGA_PROCESS=`ps ax | grep mdrepdaemon | grep amgad.conf |grep -v grep | wc -l`
		if [ $AMGA_PROCESS -eq 0 ]; then	
			log_daemon_msg "Starting AMGA replication daemon" "mdrepdaemon"
			start-stop-daemon --start --quiet -p /var/run/mdrepdaemon.pid --make-pidfile --exec ${GLITE_LOCATION}/bin/mdrepdaemon -- $OPTIONS
			log_end_msg $?
		else
			log_warning_msg "WARNING: AMGA replication daemon is already running"
  		return 0
		fi
	fi
	
}

stop() {
        
if [ "$OS_PLAT" = "SL" ]; then
        echo -n "Stopping $prog: "
        killproc /${GLITE_LOCATION}/bin/mdrepdaemon
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mdrepdaemon
        echo
        return $RETVAL
else
	log_daemon_msg "Stopping AMGA replication daemon" "mdrepdaemon"
  start-stop-daemon --stop --quiet --signal 15 --retry 5 --exec ${GLITE_LOCATION}/bin/mdrepdaemon
  log_end_msg $?
fi      
}


restart() {
        stop
        start
}       

reload() {
        restart
}       

status_at() {
  if [ "$OS_PLAT" = "SL" ]; then
  	status ${GLITE_LOCATION}/bin/mdrepdaemon        
	else
		AMGA_PROCESS=`ps ax | grep mdrepdaemon | grep amgad.conf |grep -v grep | wc -l`
		if [ $AMGA_PROCESS -eq 0 ]; then	
			log_success_msg " AMGA replication daemon is stopped"
		else
			log_success_msg " AMGA replication daemon is running"
		fi
	fi
}

case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload|restart)
        restart
        ;;
condrestart)
        if [ -f /var/lock/subsys/mdrepdaemon ]; then
            restart
        fi
        ;;
status)
        status_at
        ;;
*)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        exit 1
esac

exit $?
exit $RETVAL
