#!/bin/bash -e
source $OPENSHIFT_CARTRIDGE_SDK_BASH

export STOPTIMEOUT=20

HAPROXY_CART="haproxy-1.4"
export HAPROXY_PID="${OPENSHIFT_HAPROXY_DIR}/run/haproxy.pid"

if ! [ $# -eq 1 ]
then
    echo "Usage: \$0 [start|restart|stop|status|deploy]"
    exit 1
fi

function isrunning() {
    if [ -f "${HAPROXY_PID}" ]; then
        haproxy_pid=`cat $HAPROXY_PID 2> /dev/null`
        [ -z "$haproxy_pid" ]  &&  return 1
        current_user=`id -u`
        if `ps --pid $haproxy_pid > /dev/null 2>&1` ||     \
           `pgrep -x haproxy -u $current_user > /dev/null 2>&1`; then
            return 0
        fi
    fi
    return 1
}

function ping_server_gears() {
    #  Ping the server gears and wake 'em up on startup.
    gear_registry=$OPENSHIFT_HAPROXY_DIR/conf/gear-registry.db
    for geardns in $(cut -f 2 -d ';' "$gear_registry"); do
        [ -z "$geardns" ]  ||  curl "http://$geardns/" > /dev/null 2>&1  ||  :
    done
}

function wait_to_start() {
   ep=$(grep "listen stats" $OPENSHIFT_HAPROXY_DIR/conf/haproxy.cfg | sed 's#listen\s*stats\s*\(.*\)#\1#')
   i=0
   while ( ! curl "http://$ep/haproxy-status/;csv" &> /dev/null )  && [ $i -lt 10 ]; do
       sleep 1
       i=$(($i + 1))
   done

   if [ $i -ge 10 ]; then
      echo "`date`: HAProxy status check - max retries ($i) exceeded" 1>&2
   fi
}

function _stop_haproxy_ctld_daemon() {
    haproxy_ctld_daemon stop 2>&1
}

function _start_haproxy_ctld_daemon() {
    disable_as="${OPENSHIFT_REPO_DIR}/.openshift/markers/disable_auto_scaling"
    [ -f "$disable_as" ]  &&  return 0
    _stop_haproxy_ctld_daemon  ||  :
    haproxy_ctld_daemon start 2>&1
}

function _start_haproxy_service() {
    if ! isrunning
    then
        ping_server_gears
        /usr/sbin/haproxy -f $OPENSHIFT_HAPROXY_DIR/conf/haproxy.cfg > $OPENSHIFT_HAPROXY_LOG_DIR/haproxy.log 2>&1
        _start_haproxy_ctld_daemon
        wait_to_start
    else
        echo "HAProxy already running" 1>&2
        wait_to_start
    fi
}

function _stop_haproxy_service() {
    _stop_haproxy_ctld_daemon
    [ -f $HAPROXY_PID ]  &&  pid=$( /bin/cat "${HAPROXY_PID}" )
    if `ps -p $pid > /dev/null 2>&1`; then
        /bin/kill $pid
        ret=$?
        if [ $ret -eq 0 ]; then
            TIMEOUT="$STOPTIMEOUT"
            while [ $TIMEOUT -gt 0 ] && [ -f "$HAPROXY_PID" ]; do
                /bin/kill -0 "$pid" >/dev/null 2>&1 || break
                sleep .5
                let TIMEOUT=${TIMEOUT}-1
            done
        fi
    else
        if `pgrep -x haproxy > /dev/null 2>&1`
        then
            echo "Warning: HAProxy process exists without a pid file.  Use force-stop to kill." 1>&2
        else
            echo "HAProxy already stopped" 1>&2
        fi
    fi
}

function _restart_haproxy_service() {
    _stop_haproxy_service || pkill haproxy || :
    _start_haproxy_service
}

function _reload_haproxy_service() {
    [ -n "$1" ]  &&  zopts="-sf $1"
    ping_server_gears
    /usr/sbin/haproxy -f $OPENSHIFT_HAPROXY_DIR/conf/haproxy.cfg ${zopts} > /dev/null 2>&1
}

function _reload_service() {
    [ -f $HAPROXY_PID ]  &&  zpid=$( /bin/cat "${HAPROXY_PID}" )
    i=0
    while (! _reload_haproxy_service "$zpid" )  && [ $i -lt 60 ]; do
        sleep 2
        i=$(($i + 1))
        echo "`date`: Retrying HAProxy service reload - attempt #$((i+1)) ... "
    done

    wait_to_start
}

function start() {
    _start_haproxy_service
    isrunning  &&  client_result "HAProxy instance is started"
}

function stop() {
    _stop_haproxy_service
    isrunning  ||  client_result "HAProxy instance is stopped"
}

function restart() {
    _restart_haproxy_service
    isrunning  &&  client_result "Restarted HAProxy instance"
}

function reload() {
    if ! isrunning; then
       _start_haproxy_service
    else
       echo "`date`: Reloading HAProxy service " 1>&2
       _reload_service
       _start_haproxy_ctld_daemon
    fi

    isrunning  &&  client_result "Reloaded HAProxy instance"
}

function force-reload() {
    if isrunning; then
        echo "`date`: Conditionally reloading HAProxy service " 1>&2
        _reload_service
        _start_haproxy_ctld_daemon
        isrunning  &&  client_result "Conditionally reloaded HAProxy"
    fi
}

function status() {
    if isrunning; then
        client_result "HAProxy instance is running"
    else
        client_result "HAProxy instance is stopped"
    fi
    print_user_running_processes `id -u`
}

function deploy() {
    $OPENSHIFT_HAPROXY_DIR/bin/deploy
}

#
# main():
#

# And then on the haproxy and haproxy_ctld.
case "$1" in
    start)        start         ;;
    stop)         stop          ;;
    restart)      restart       ;;
    reload)       reload        ;;
    force-reload) force-reload  ;;
    status)       status        ;;
    deploy)       deploy        ;;
esac
