# corosync - Corosync Cluster Engine
#
# Starts corosync

expect fork

env prog=corosync
env initconf=@INITCONFIGDIR@/corosync
env rpm_lockfile=@LOCALSTATEDIR@/lock/subsys/corosync
env deb_lockfile=@LOCALSTATEDIR@/lock/corosync

script
    [ -f "$initconf" ] && . $initconf
    exec $prog $COROSYNC_OPTIONS
end script

pre-start script
    mkdir -p @LOCALSTATEDIR@/run
    # rewrite according to environment.
    #[ -c /dev/watchdog ] || modprobe softdog
end script

post-start script
wait_for_ipc()
{
    [ -f "$initconf" ] && . $initconf
    try=0
    max_try=$((COROSYNC_INIT_TIMEOUT*2-1))
    [ "$max_try" -le "0" ] && max_try=120

    while [ "$try" -le "$max_try" ]; do
        if corosync-cfgtool -s > /dev/null 2>&1; then
            return 0
        fi
        sleep 0.5
        try=$((try + 1))
    done
    logger -i -t "$UPSTART_JOB" "ERROR: Any interfaces are faulty!"
    return 1
}
    wait_for_ipc || { stop; exit 1; }

    [ -f "$initconf" ] && . $initconf
    [ -z "$LOCK_FILE" -a -d @SYSCONFDIR@/sysconfig ] && LOCK_FILE="$rpm_lockfile"
    [ -z "$LOCK_FILE" -a -d @SYSCONFDIR@/default ] && LOCK_FILE="$deb_lockfile"
    touch $LOCK_FILE
end script

post-stop script
    [ -f "$initconf" ] && . $initconf
    [ -z "$LOCK_FILE" -a -d @SYSCONFDIR@/sysconfig ] && LOCK_FILE="$rpm_lockfile"
    [ -z "$LOCK_FILE" -a -d @SYSCONFDIR@/default ] && LOCK_FILE="$deb_lockfile"
    rm -f $LOCK_FILE
end script
