#!/sbin/openrc-run
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-proxy/haproxy/files/haproxy.initd-r1,v 1.2 2011/12/04 10:32:32 swegener Exp $

extra_commands="checkconfig"
extra_started_commands="reload"
command=/usr/sbin/haproxy

CONF=${HAPROXY_CONF:-/etc/haproxy/${SVCNAME}.cfg}
PIDFILE=/var/run/${SVCNAME}.pid
MASTER_SOCK=/etc/haproxy/pxc/haproxy-main.sock

depend() {
        need net
        after firewall
        use dns logger
}

checkconfig() {
        if [ ! -f "${CONF}" ] && [ ! -d "${CONF}" ]; then
                eerror "${CONF} does not exist!"
                return 1
        fi

        ebegin "Checking ${CONF}"
        $command -q -c -f "${CONF}"
        eend $?
}

start() {
        ebegin "Starting ${SVCNAME}"
        start-stop-daemon --pidfile "${PIDFILE}" --exec $command \
                --start -- -W -p "${PIDFILE}" -f "${CONF}"
        eend $?
}

stop() {
        ebegin "Stopping ${SVCNAME}"

        if [ "${RC_CMD}" = "restart" ]; then
                checkconfig || return 1
        fi

        start-stop-daemon --stop --pidfile "${PIDFILE}"
        eend $?
}

reload() {
        ebegin "Reloading ${SVCNAME}"
        checkconfig || { eerror "Reloading failed, please fix your ${CONF} first"; return 1; }
        if [ -S "${MASTER_SOCK}" ]; then
                echo "reload" | socat "${MASTER_SOCK}" - >/dev/null
        else
                $command -W -p "${PIDFILE}" -f "${CONF}" -sf $(cat "${PIDFILE}")
        fi
        eend $?
}
