#!/bin/bash -p
#
# $Id: ha-dovecot,v 1.2 2008/12/19 09:32:27 mkretchn Exp $

#---+ Notes

## Cf: http://linux-ha.org/OCFResourceAgent
##     http://www.opencf.org/cgi-bin/viewcvs.cgi/specs/ra/resource-agent-api.txt?rev=HEAD
##     /usr/share/heartbeat/crm.dtd

#---+ Contexte OCF

# Pour execution (status monitor) hors controle de heartbeat
if [ -z "$OCF_ROOT" ]; then
    OCF_ROOT=/usr/lib/ocf
fi

source ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

#---+ Actions

usage () {
  echo "Usage: $0 {start|stop|monitor|meta-data|validate-all}"
}

meta_data () {
    local rev="$Revision: 1.2 $"
    rev=${rev#* }; rev=${rev% *}
    cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="ha-dovecot">
    <version>$rev</version>
    <longdesc lang="en">
	dovecot
    </longdesc>
    <shortdesc lang="en">dovecot RA</shortdesc>
    <actions>
	<action name="start" timeout="1m"/>
	<action name="stop" timeout="1m"/>
	<action name="monitor" interval="30s" timeout="20s" start-delay="1m" />
	<action name="validate-all"  timeout="5s" />
	<action name="meta-data"  timeout="5s" />
    </actions>
</resource-agent>
EOF
}

exec="/usr/sbin/dovecot"
bin=$(basename $exec)
config="/etc/dovecot.conf"

start () {

	[ -x $exec ] || {
		echo >&2 "** $exec not executable"
		return $OCF_ERR_GENERIC
	}
        [ -f $config ] || {
                echo >&2 "** $config doesn't exist"
                return $OCF_ERR_GENERIC
        }
	
	local pid=$(dovecot_pid)
	if [ "$pid" ]; then
		echo >&2 "dovecot already running"
    		return $OCF_SUCCESS
	elif $exec; then
	local pid=$(dovecot_pid)
	  if [ "$pid" ]; then
		    return $OCF_SUCCESS
		else
		    echo >&2 "** no dovecot pid"
		    return $OCF_ERR_GENERIC
		fi
	  else
		echo >&2 "** $bin fails to start"
		return $OCF_ERR_GENERIC
	  fi
}


stop () {
	local pid=$(dovecot_pid)
	echo $pid
        if [ "$pid" ]; then
		if killpid $pid; then
		    return $OCF_SUCCESS
		else
		    echo >&2 "** killpid $pid: fails"
		    return $OCF_ERR_GENERIC
		fi
	 else
		return $OCF_SUCCESS
	 fi
}

status () {
    # Seems to not be used by heartbeat
    return
}

monitor () {
	local pid=$(dovecot_pid)
        if [ "$pid" ]; then 
		MYSTATUS=`echo ". logout" | nc localhost 143 | grep "ready" | wc -l`
		if [ "$MYSTATUS" -eq "1" ]; then
			return $OCF_SUCCESS 
		else
			return $OCF_ERR_GENERIC
		fi
	else
		return $OCF_NOT_RUNNING
        fi
}

validate_all () {
    return
}

#---+ Utilitaires

dovecot_pid () {
	pidof $bin
}


# Check if $pid (could be plural) are running
checkpid() {
    local i

    for i in $* ; do
        [ -d "/proc/$i" ] && return 0
    done
    return 1
}

killpid () {
    # Repris depuis /etc/init.d/functions:killproc
    # killpid <pid> [<delai en secondes>]
    # FIXME: faire faire ca par lps -k ou autre option?

    local pid=$1
    local delay=${2:-3}

    # Succes si pas de process
    checkpid $pid || return 0

    # TERM first, then KILL if not dead
    kill -TERM $pid
    usleep 100000
    if checkpid $pid && sleep 1 &&
        checkpid $pid && sleep $delay &&
        checkpid $pid ; then
        kill -KILL $pid
        usleep 100000
    fi
    if checkpid $pid; then
        return 1
    else
        return 0
    fi
}

#---+ Debug
[[ $0 == *bash ]] && return

#---+ Main

if [ $# -eq 0 ]
then
    usage
    exit $OCF_ERR_ARGS
fi

# PATH

source /usr/local/bashutil/autoload_lib
autoload_lib /usr/local/bashutil/lib/batch

case $1 in
  start)	start ;;
  stop)		stop ;;
  status)	status ;;
  monitor)	monitor ;;
  meta-data)	meta_data;;
  validate-all ) validate_all;;
  *)		usage
 		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
