#!/bin/bash

# Alias: TCP port number for MK Livestatus
# Menu: Distributed Monitoring
# Description:
#  If Livestatus is configured to listen on a TCP port, you
#  can configure that port here.

case "$1" in
    default)
        PORT=$($OMD_ROOT/lib/omd/next_free_port LIVESTATUS_TCP_PORT 6557)
        echo "$PORT"
    ;;
    choices)
        echo "[0-9]{1,5}"
    ;;
    set)
        PORT=$($OMD_ROOT/lib/omd/next_free_port LIVESTATUS_TCP_PORT $2)

        if [ "$PORT" != "$2" ]; then
            echo "Livestatus port $2 is in use. I've choosen $PORT instead." >&2
            echo "$PORT"
        fi

        sed -ri 's/^([[:space:]]*port[[:space:]]*=[[:space:]]*)([0-9]+)/\1'$PORT'/' $OMD_ROOT/etc/mk-livestatus/xinetd.conf
    ;;
    depends)
        [ "$CONFIG_CORE" != none -a "$CONFIG_LIVESTATUS_TCP" = on ]
    ;;
esac

