#!/bin/bash
#
# return next free port for key
#
# usage: $OMD_ROOT/lib/omd/next_free_port APACHE_TCP_PORT 5000


port_is_used () {
    local KEY="${1}"
    local PORT="${2}"
    for S in $(ls /omd/sites); do
        if [ ! -d /omd/sites/"${S}" ]; then
            continue
        fi

        site_conf="/omd/sites/${S}/etc/omd/site.conf"

        if [ "${S}" = "${OMD_SITE}" ]; then
            # happens when a new site is created
            if [ ! -f "${site_conf}" ]; then
                continue
            fi
            if [ $(grep "='${PORT}'" "${site_conf}" | grep -v "${KEY}" | wc -l) -gt 0 ]; then
                return 0
            fi
        else
            if ! ls "${site_conf}" >/dev/null 2>&1; then
                echo "ERROR: Failed to read config ${site_conf} of site ${S}. ${KEY} port will possibly be allocated twice" >&2
                continue
            fi

            if [ $(grep -v ^#  < "${site_conf}" | grep "='${PORT}'" | wc -l) -gt 0 ]; then
                return 0
            fi
        fi
    done
    return 1
}

KEY="${1}"
PORT="${2}"
while port_is_used "${KEY}" "${PORT}"; do
    PORT=$((PORT + 1))
done
echo "${PORT}"
