#!/bin/sh
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL)". You may
# only use this file in accordance with the terms of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2016 Adam Stevko. All rights reserved.
#

ulimit -n 65536

. /lib/svc/share/smf_include.sh

HAPROXY="/usr/sbin/haproxy"
CONF_FILE="/etc/haproxy/haproxy.cfg"
PID_FILE="/var/run/haproxy.pid"
MASTER_SOCKET="/var/run/haproxy-master.sock,uid,root,gid,root,mode,600"

haproxy_verify_config() {

    echo "Verifying HAProxy configuration: \c"
    if ! ${HAPROXY} -c -f ${CONF_FILE} >/dev/null 2>&1; then
        echo "FAILED"
        exit ${SMF_EXIT_ERR_CONFIG}
    else
        echo "OK"
    fi
}



[ ! -f ${CONF_FILE} ] && exit ${SMF_EXIT_ERR_CONFIG}

case "$1" in
    start)
        haproxy_verify_config

        exec ${HAPROXY} -D -f ${CONF_FILE} -W -S ${MASTER_SOCKET} -p ${PID_FILE} -sf $(<${PID_FILE})
    ;;

    refresh)
        haproxy_verify_config

        exec /usr/bin/kill -USR2 $(<${PID_FILE})
    ;;

    stop)
        exec /usr/bin/kill -TERM $(<${PID_FILE})
    ;;

    *)
        echo "Usage: $0 {start|stop|refresh}"
        exit 1
    ;;

esac