#! /bin/bash

PROXY_CONF="/etc/nginx/proxy.conf"

function append_upstream_conf() {
    sed -i "/stream {/ r /opt/nginx/upstream_conf/$1/$2.conf" $3
}


# if [ ${MIDDLEWARE} = "cluster" ];then
#     exec /etc/nginx/get_redis_role.sh &
#     cp /etc/nginx/proxy-cluster.conf /etc/nginx/nginx.conf
# else
#     cp /etc/nginx/proxy.conf /etc/nginx/nginx.conf
# fi

if [ ! -z ${PROXY_SERVICES} ]; then
    _proxy_services=$(echo ${PROXY_SERVICES} | tr "," " ")
    for svc in ${_proxy_services[@]}; do
        #append_upstream_conf ${svc} ${MIDDLEWARE} ${PROXY_CONF}
        csvc=`echo ${svc}|awk -F "-" '{print $1}'`
        append_upstream_conf ${csvc} ${MIDDLEWARE} ${PROXY_CONF}
    done
    /bin/cp -f ${PROXY_CONF} /etc/nginx/nginx.conf
    if [ "${MIDDLEWARE}" == "cluster" ];then
        bash /etc/nginx/service_status_check.sh &
    fi
fi

/usr/bin/iperf3 -s -D
cat /etc/nginx/nginx.conf
nginx -g "daemon off;"
