# ----------------------------------------------------------------------
# File: eosha
# Author: Andreas-Joachim Peters - CERN
# ----------------------------------------------------------------------

# ************************************************************************
# * EOS - the CERN Disk Storage System                                   *
# * Copyright (C) 2011 CERN/Switzerland                                  *
# *                                                                      *
# * This program is free software: you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License as published by *
# * the Free Software Foundation, either version 3 of the License, or    *
# * (at your option) any later version.                                  *
# *                                                                      *
# * This program is distributed in the hope that it will be useful,      *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
# * GNU General Public License for more details.                         *
# *                                                                      *
# * You should have received a copy of the GNU General Public License    *
# * along with this program.  If not, see <http://www.gnu.org/licenses/>.*
# ************************************************************************

#!/bin/bash
#
# chkconfig: 35 95 12
# description: eosha - eos HA daemon
#

# Get function from functions library
. /etc/init.d/functions
. /etc/sysconfig/eos

# Start the eosha service
start() {
        [ -e /var/lock/subsys/eosha ] && failure && echo "eosha is already running!" && exit -1;
        echo -n "Starting eosha: "
        [ -z "${EOS_MGM_MASTER1}" ] && failure && echo "Env EOS_MGM_MASTER1 undefined!" && exit -1;
        [ -z "${EOS_MGM_MASTER2}" ] && failure && echo "Env EOS_MGM_MASTER2 undefined!" && exit -1;
        [ -z "${EOS_MGM_ALIAS}"   ] && failure && echo "Env EOS_MGM_ALIAS undefined!"  && exit -1;
        mkdir -p ${EOS_LOGDIR-/var/log/eos/}/eosha        	
	chown daemon:daemon ${EOS_LOGDIR-/var/log/eos/}/eosha        	
        echo eosha ${EOS_LOGDIR-/var/log/eos/eosha}/eosha/eosha.log ${EOS_MGM_MASTER1} ${EOS_MGM_MASTER2} ${EOS_MGM_ALIAS} ${EOS_MGM_FAILOVERTIME-120}
        eosha ${EOS_LOGDIR-/var/log/eos/}/eosha//eosha.log ${EOS_MGM_MASTER1} ${EOS_MGM_MASTER2} ${EOS_MGM_ALIAS} ${EOS_MGM_FAILOVERTIME-120}
        ### Create the lock file ###
        touch /var/lock/subsys/eosha
        success $"eosha has been started."
        echo
}

# Stop the eosha service
stop() {
        echo -n "Stopping eosha: "
        killproc eosha
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/eosha
        rm -f /var/run/eosha.pid
        echo
        /etc/init.d/eossync stop
}

### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status eosha
        ;;
  condrestart)	
	status eosha 2>&1 | grep dead 
	if [ $? -eq 0 ]; then
          stop
	  start
	  exit 0
	fi
	status eosha 2>&1 | grep running
        if [ $? -eq 0 ]; then
	  stop
	  start
	  exit 0
	fi
	;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0