#!/bin/bash -xu

config_file=/var/lib/haproxy/conf/haproxy.config
pid_file=/var/lib/haproxy/run/haproxy.pid
old_pid=""
haproxy_conf_dir=/var/lib/haproxy/conf

# sort the path based map files for the haproxy map_beg function
for mapfile in "$haproxy_conf_dir"/*.map; do
  sort -r "$mapfile" -o "$mapfile"
done

if [ -f $pid_file ]; then
  old_pid=$(<$pid_file)
fi

if [ -n "$old_pid" ]; then
  /usr/sbin/haproxy -f $config_file -p $pid_file -sf $old_pid
else
  /usr/sbin/haproxy -f $config_file -p $pid_file
fi
