#!/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
#


# Source function library.
if [ -f /etc/init.d/functions ] ; then
    . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
fi


# Avoid using root's TMPDIR
unset TMPDIR

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

. $CTDB_BASE/functions
SourceIfNotEmpty /etc/sysconfig/network
SourceIfNotEmpty /etc/sysconfig/ctdb


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


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


build_ctdb_options () {

    maybe_set () {
	# If the 2nd arg is null then return - don't set anything.
	# Else if the 3rd arg is set and it doesn't match the 2nd arg
	# then return
	[ -z "$2" -o \( -n "$3" -a "$3" != "$2" \) ] && return

	val="$2"
	case "$1" in
	    --*) sep="=" ;;
	    -*)  sep=" " ;;
	esac
	# For these options we're only passing a value-less flag.
	[ -n "$3" ] && {
	    val=""
	    sep=""
	}

	CTDB_OPTIONS="${CTDB_OPTIONS}${CTDB_OPTIONS:+ }${1}${sep}${val}"
    }

    [ -z "$CTDB_RECOVERY_LOCK" ] && {
        echo "No recovery lock specified. Starting CTDB without split brain prevention"
    }
    maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"

    # build up CTDB_OPTIONS variable from optional parameters
    maybe_set "--logfile"                "$CTDB_LOGFILE"
    maybe_set "--nlist"                  "$CTDB_NODES"
    maybe_set "--socket"                 "$CTDB_SOCKET"
    maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
    maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
    maybe_set "--dbdir"                  "$CTDB_DBDIR"
    maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
    maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
    maybe_set "--transport"              "$CTDB_TRANSPORT"
    maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
    maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
    maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
    maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
    maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
    maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
    maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
    maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
    maybe_set "--log-ringbuf-size"       "$CTDB_LOG_RINGBUF_SIZE"
    maybe_set "--syslog"                 "$CTDB_SYSLOG"               "yes"
    maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
}

check_tdb () {
	local PDBASE=$1

	local TDBTOOL_HAS_CHECK=`echo "help" | /usr/bin/tdbtool | grep check | wc -l`

	test x"$TDBTOOL_HAS_CHECK" = x"1" && {
		#
		# Note tdbtool always exits with 0
		#
		local OK=`/usr/bin/tdbtool $PDBASE check | grep "Database integrity is OK" | wc -l`
		test x"$OK" = x"1" || {
			return 1;
		}

		return 0;
	}

	/usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
		return $?;
	}

	return 0;
}

check_persistent_databases () {
    PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/ctdb}/persistent"
    mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
    local ERRCOUNT=$CTDB_MAX_PERSISTENT_CHECK_ERRORS

    test -z "$ERRCOUNT" && {
	ERRCOUNT="0"
    }
    test x"$ERRCOUNT" != x"0" && {
	return 0;
    }
    for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
	check_tdb $PDBASE || {
	    echo "Persistent database $PDBASE is corrupted! CTDB will not start."
	    return 1
	}
    done
}

set_ctdb_variables () {
    # set any tunables from the config file
    set | grep ^CTDB_SET_ | cut -d_ -f3- | 
    while read v; do
	varname=`echo $v | cut -d= -f1`
	value=`echo $v | cut -d= -f2`
	ctdb setvar $varname $value || RETVAL=1
    done || exit 1
}

set_retval() {
    return $1
}

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

start() {
    echo -n $"Starting ctdbd service: "

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

    build_ctdb_options

    check_persistent_databases || return $?

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

    start_daemon --pidfile=/var/run/ctdbd/ctdbd.pid  $ctdbd $CTDB_OPTIONS
    RETVAL=$?

    sleep 1

    set_ctdb_variables

    return $RETVAL
}	

stop() {
    stop_daemon --pidfile /var/run/ctdbd/ctdbd.pid --lockfile "$LOCKFILE" $PROCESSNAME
    RETVAL=$?
    return $RETVAL
}

restart() {
    stop
    start
}	

status() {
    echo -n $"Checking for ctdbd service: "
    ctdb ping >/dev/null 2>&1 || {
	RETVAL=$?
	echo -n "  ctdbd not running. "
	return $RETVAL
    }
    echo ""
    ctdb status
}


case "$1" in
    start)
  	start
	;;
    stop)
  	stop
	;;
    restart|reload)
  	restart
	;;
    status)
  	status
	;;
    condrestart)
  	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|status|cron|condrestart|condstop}"
	exit 1
esac

exit $?
