#!/bin/bash

CONF="/etc/ntopng/ntopng.conf"
SETTINGS="/var/efw/ntopng/settings"
START_LOCAL="/var/efw/inithooks/start.local"
MONIT_DIR="/etc/monit.d"
MONIT_FILE="$MONIT_DIR/ntopng.conf"

# valida se o arquivo de settings existe
if [ ! -f "$SETTINGS" ]; then
    echo "Arquivo de settings não encontrado: $SETTINGS" >&2
    exit 1
fi

#Cria pasta /var/run/ntopng
mkdir -p /var/run/ntopng

# importa variáveis do settings
source "$SETTINGS"

# Função para configurar auto start
configure_startup_ntopng() {
    echo "Configurando auto-start do ntopng..."

    # Cria arquivo se não existir
    if [ ! -f "$START_LOCAL" ]; then
        echo "#!/bin/bash" > "$START_LOCAL"
        chmod +x "$START_LOCAL"
    fi

    # Garante shebang
    if ! head -1 "$START_LOCAL" | grep -q "^#!/bin/bash"; then
        sed -i '1i#!/bin/bash' "$START_LOCAL"
    fi

    # Remove entradas antigas
    sed -i '/ntopng/d' "$START_LOCAL"

    if [ "$1" = "enable" ]; then
        echo "/etc/init.d/ntopng start" >> "$START_LOCAL"
    fi
}

# Função para configurar auto start
configure_startup_redis() {
    echo "Configurando auto-start do redis..."

    # Cria start.local se não existir
    if [ ! -f "$START_LOCAL" ]; then
        echo "#!/bin/bash" > "$START_LOCAL"
        chmod +x "$START_LOCAL"
    fi

    # garante shebang
    if ! head -1 "$START_LOCAL" | grep -q "^#!/bin/bash"; then
        sed -i '1i#!/bin/bash' "$START_LOCAL"
    fi

    # remove linha antiga
    sed -i '\|/etc/init.d/redis start|d' "$START_LOCAL"

    if [ "$1" = "enable" ]; then
        echo "/etc/init.d/redis start" >> "$START_LOCAL"
    fi
}

# Função para configurar monit
configure_monit_redis() {

    REDIS_MONIT="$MONIT_DIR/redis.conf"

    monit unmonitor redis 2>/dev/null

    if [ "$1" = "disable" ]; then
        [ -f "$REDIS_MONIT" ] && rm -f "$REDIS_MONIT"
        monit reload
        return
    fi

    cat <<EOF > "$REDIS_MONIT"
check process redis with pidfile /var/run/redis/redis.pid
   start program = "/etc/init.d/redis start"
   stop program  = "/etc/init.d/redis stop"
   if failed host 127.0.0.1 port 6379 then restart
   if 5 restarts within 5 cycles then timeout
EOF

    monit reload
    monit monitor redis 2>/dev/null
}

# Função para configurar monit
configure_monit_ntopng() {
    echo "Configurando monit para ntopng..."

    monit unmonitor ntopng 2>/dev/null

    if [ "$1" = "disable" ]; then
        [ -f "$MONIT_FILE" ] && rm -f "$MONIT_FILE"
        monit reload
        return
    fi

    cat <<EOF > "$MONIT_FILE"
check process ntopng with pidfile /var/run/ntopng/ntopng.pid
   start program = "/etc/init.d/ntopng start"
   stop program  = "/etc/init.d/ntopng stop"
   if failed port 3000 protocol http then restart
   if 5 restarts within 5 cycles then timeout
EOF

    monit reload
    monit monitor ntopng 2>/dev/null
}

# Configura auto-start e monit baseado em NTOPNG_ENABLE
if [ "$NTOPNG_ENABLE" = "on" ]; then
    configure_startup_redis enable
    configure_monit_redis
    /etc/init.d/redis restart
    configure_startup_ntopng enable
    configure_monit_ntopng enable
else
    configure_startup_redis disable
    configure_startup_ntopng disable
    configure_monit_redis disable
    configure_monit_ntopng disable
    /etc/init.d/ntopng stop 2>/dev/null
    exit 0
fi

# gera configuração ntopng
{
    echo "-G=/var/run/ntopng/ntopng.pid"
    echo "-d=/var/lib/ntopng"

    # interfaces
    IFS=',' read -ra IFACES <<< "$INTERFACES"
    for i in "${IFACES[@]}"; do
        [ -n "$i" ] && echo "-i=$i"
    done

    # redes internas
    echo "-m=10.0.0.0/8,192.168.0.0/16,172.16.0.0/12"

    # outros parâmetros
    echo "-n=1"
    echo "--redis=127.0.0.1:6379"
    echo "--max-num-flows=500000"
    echo "--max-num-hosts=200000"
    echo "-w=3000"
} > "$CONF"

# reinicia serviço
if ! /etc/init.d/ntopng restart >/dev/null 2>&1; then
    echo "Erro ao reiniciar ntopng!" >&2
    exit 1
fi