#!/bin/bash

# Adds a gear to the haproxy configuration.

# Exit on any errors
set -e

source /etc/openshift/node.conf
source ${CARTRIDGE_BASE_PATH}/abstract/info/lib/util

function print_help {
    echo "Usage: $0 app-name namespace uuid"
    echo "Start a running application"

    echo "$0 $@" | logger -p local0.notice -t openshift_origin_haproxy_set_proxy
    exit 1
}

while getopts 'd' OPTION
do
    case $OPTION in
        d) set -x
        ;;
        ?) print_help
        ;;
    esac
done


[ $# -gt 3 ] || print_help

setup_configure "$1" $2 $3

import_env_vars

exitcode=0

declare -A curr_server_gears

haproxy_cfg=$OPENSHIFT_HOMEDIR/haproxy-1.4/conf/haproxy.cfg
# Remove the first 3 args and process all the remaining args of the form k=v.
# The values are the gear endpoints.
kvargs=$(echo "${@:4}" | tr -d "\n" )
for arg in $kvargs; do
    zinfo=$(echo "$arg" | cut -f 2 -d '=' | tr -d "'")
    zarr=(${zinfo//|/ })
    ep="${zarr[1]}"
    #  Add gear to the proxy configuration if not already in there.
    #  Gear end-point is the form: $gear-ipaddress:$gear-port
    gear_name=$(echo "${zarr[0]}" | cut -f 1 -d '.')
    curr_server_gears[$gear_name]="$ep"

    # Ensure endpoint is valid.
    [ -z "$ep" ]  &&  continue

    # And of the form: $ipv4-address:$port-number
    if [[ ! $ep =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{4,5} ]]; then
       # write/log error, set exit code but continue/process remaining entries.
       echo "${@:1:3} - Invalid endpoint '$ep' passed in input - $zinfo" 1>&2
       exitcode=22
       continue
    fi

    if grep "server gear-$gear_name" "$haproxy_cfg" > /dev/null 2>&1; then
        sed -i "/\s*server\s*gear-${gear_name}\s.*/d" $haproxy_cfg
    fi

    echo "    server gear-$gear_name $ep check fall 2 rise 3 inter 2000 cookie $gear_name" >> "$haproxy_cfg"
done

# Now remove all the gears from the haproxy configuration which are not in the
# current set. No recreate permissions on haproxy.cfg, so need to use a temp
# file to operate on and then overlay the contents of haproxy.cfg
srvgears=$(grep -E "server\s*gear" "$haproxy_cfg" |  \
           sed "s/\s*server\s*gear-\([A-Za-z0-9\-]*\)\s.*/\\1/g" | tr "\n" " ")
cp -f "$haproxy_cfg" /tmp/haproxy.cfg.$$
for sg in $srvgears; do
    if [ -z "${curr_server_gears[$sg]}" ]; then
        sed -i "/\s*server\s*gear-$sg\s.*/d" /tmp/haproxy.cfg.$$
    fi
done

# Get the local app server endpoint.
local_ep=$OPENSHIFT_INTERNAL_IP:8080

# Disable/enable local endpoint based on number of serving gears.
if [ $(grep -E "server\s*gear" /tmp/haproxy.cfg.$$ | wc -l) -ge 2 ]; then
    # disable local-gear serving with weight 0.
    sed -i "/\s*server\s*local-gear\s.*/d" /tmp/haproxy.cfg.$$
    echo "    server local-gear $local_ep weight 0" >> /tmp/haproxy.cfg.$$
else
    # re-add local-gear with maxconn 2.
    sed -i "/\s*server\s*local-gear\s.*/d" /tmp/haproxy.cfg.$$
    echo "    server local-gear $local_ep maxconn 2 check fall 2 rise 3 inter 2000 cookie local-$OPENSHIFT_GEAR_UUID" >> /tmp/haproxy.cfg.$$
fi

cat /tmp/haproxy.cfg.$$ > "$haproxy_cfg"
rm -f /tmp/haproxy.cfg.$$

uuid=$3
setup_user_vars
# echo "$0: reloading haproxy for application $1 - uuid $uuid ... "
run_as_user "app_ctl.sh cond-reload"
appctl_exitcode=$?

# Pass the appropriate exit code back.
[ $exitcode -ne 0 ]  &&  exit $exitcode
exit $appctl_exitcode
