#!/bin/sh
#
##############################
# ctdb:                        Starts the clustered tdb daemon
#
# chkconfig:           - 90 01
#
# description:                 Starts and stops the clustered tdb daemon
# pidfile:             /var/run/ctdbd/ctdbd.pid
#

### BEGIN INIT INFO
# Provides:            ctdb
# Required-Start:      $network
# Required-Stop:       $network
# Default-Stop:
# Default-Start:
# Short-Description:   start and stop ctdb service
# Description:         initscript for the ctdb service
### END INIT INFO

# Source function library.
. /etc/init.d/functions

# Avoid using root's TMPDIR
unset TMPDIR

[ -z "$CTDB_BASE" ] && {
    export CTDB_BASE="/etc/ctdb"
}

[ -z "$CTDB_VARDIR" ] && {
    export CTDB_VARDIR="/var/ctdb"
}

. $CTDB_BASE/functions
. $CTDB_BASE/init_functions
loadconfig network
loadconfig ctdb

# check networking is up (for redhat)
[ "$NETWORKING" = "no" ] && exit 0

detect_init_style
export CTDB_INIT_STYLE

ctdbd=${CTDBD:-/usr/sbin/ctdbd}

if [ "$CTDB_VALGRIND" = "yes" ]; then
    init_style="valgrind"
else
    init_style="$CTDB_INIT_STYLE"
fi

set_retval() {
    return $1
}

ctdbd=${CTDBD:-/usr/sbin/ctdbd}

start() {
    ctdb ping >/dev/null 2>&1 && {
	echo $"CTDB is already running"
	return 0
    }

    build_ctdb_options

    # make sure we drop any ips that might still be held if previous
    # instance of ctdb got killed with -9 or similar
    drop_all_public_ips

    check_persistent_databases || return $?

    if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
	ulimit -c 0
    else
	ulimit -c unlimited
    fi

    case $init_style in
	valgrind)
	    eval valgrind -q --log-file=/var/log/ctdb_valgrind \
		$ctdbd --valgrinding "$CTDB_OPTIONS"
	    RETVAL=$?
	    echo
	    ;;
	alt)
		start_daemon --lockfile /var/lock/subsys/ctdb --expect-user root -- $ctdbd "$CTDB_OPTIONS"
		RETVAL=$?
	    ;;
    esac

    if [ $RETVAL -eq 0 ] ; then
	if wait_until_ready ; then
	    set_ctdb_variables
	else
	    RETVAL=1
	    pkill -9 -f $ctdbd >/dev/null 2>&1
	fi
    fi

    case $init_style in
	suse)
	    set_retval $RETVAL
	    rc_status -v
	    ;;
	redhat)
	    [ $RETVAL -eq 0 ] && success || failure
	    echo
	    ;;
    esac

    return $RETVAL
}

stop() {
    echo -n $"Shutting down ctdbd service: "
    pkill -0 -f $ctdbd || {
	echo -n "  Warning: ctdbd not running ! "
	case $init_style in
	    suse)
		rc_status -v
		;;
	    redhat)
		echo ""
		;;
	esac
	return 0
    }
    ctdb shutdown >/dev/null 2>&1
    RETVAL=$?
    count=0
    while pkill -0 -f $ctdbd ; do
	sleep 1
	count=$(($count + 1))
	[ $count -gt 10 ] && {
	    echo -n $"killing ctdbd "
	    pkill -9 -f $ctdbd
	    pkill -9 -f $CTDB_BASE/events.d/
	}
    done
    [ $RETVAL -eq 0 ] && echo_success || echo_failure
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
    echo ""
    return $RETVAL
}

restart() {
    stop
    start
}

status() {
    echo -n $"Checking for ctdbd service: "
    ctdb ping >/dev/null 2>&1 || {
	RETVAL=$?
	echo -n "  ctdbd not running. "
	if [ -f /var/lock/subsys/ctdb ]; then
		echo $"ctdb dead but subsys locked"
		RETVAL=2
	else
		echo $"ctdb is stopped"
		RETVAL=3
	fi
	return $RETVAL
    }
    echo ""
    ctdb status
}


case "$1" in
    start)
  	start
	;;
    stop)
  	stop
	;;
    restart|reload|force-reload)
  	restart
	;;
    status)
  	status
	;;
    condrestart|try-restart)
  	ctdb status > /dev/null && restart || :
	;;
    condstop)
  	ctdb status > /dev/null && stop || :
	;;
    cron)
	# used from cron to auto-restart ctdb
  	ctdb status > /dev/null || restart
	;;
    *)
	echo $"Usage: $0 {start|stop|restart|reload|force-reload|status|cron|condrestart|try-restart|condstop}"
	exit 1
esac

exit $?
