#!/usr/bin/env bash

set -u

main() {
    CROWDSEC_CONFIG="/etc/crowdsec/config.yaml"

    echo "===== ACTIVACIÓN HYD CROWDSEC ====="

    command -v systemctl >/dev/null 2>&1 || {
        echo "FAIL: systemctl no está disponible"
        return 1
    }

    command -v crowdsec >/dev/null 2>&1 || {
        echo "FAIL: CrowdSec no está instalado"
        return 1
    }

    test -f "$CROWDSEC_CONFIG" || {
        echo "FAIL: no existe $CROWDSEC_CONFIG"
        return 1
    }

    systemctl daemon-reload || {
        echo "FAIL: systemctl daemon-reload"
        return 1
    }

    echo
    echo "===== ESCENARIOS Y PARSERS ====="

# BEGIN HYD SELF-IP PARSER
echo
echo "===== HYD self IP parser ====="

if test ! -x /usr/local/sbin/hyd-install-self-ip-parser.sh; then
    echo "FAIL: falta /usr/local/sbin/hyd-install-self-ip-parser.sh"
    return 1
fi

if ! /usr/local/sbin/hyd-install-self-ip-parser.sh; then
    echo "FAIL: no se pudo instalar hyd-self-ip parser"
    return 1
fi

echo "OK: hyd-self-ip parser instalado"
# END HYD SELF-IP PARSER

    systemctl start hyd-crowdsec-rules-sync.service || {
        echo "FAIL: sincronización inicial de escenarios y parsers"
        return 1
    }

    systemctl enable --now hyd-crowdsec-rules-sync.timer || {
        echo "FAIL: no se pudo habilitar rules-sync"
        return 1
    }

    echo "OK: sincronización de reglas activa"

    echo
    echo "===== ADQUISICIONES DIRECTADMIN ====="

    if test -d /usr/local/directadmin; then
        systemctl start \
            hyd-crowdsec-directadmin-acquis-sync.service || {
            echo "FAIL: generación inicial de acquisitions"
            return 1
        }

        systemctl enable --now \
            hyd-crowdsec-directadmin-acquis-sync.timer || {
            echo "FAIL: no se pudo habilitar acquisitions-sync"
            return 1
        }

        echo "OK: sincronización de acquisitions activa"
    else
        systemctl disable --now \
            hyd-crowdsec-directadmin-acquis-sync.timer \
            >/dev/null 2>&1 || true

        echo "INFO: DirectAdmin no detectado"
    fi

    echo
    echo "===== VALIDACIÓN CROWDSEC ====="

    crowdsec -c "$CROWDSEC_CONFIG" -t || {
        echo "FAIL: configuración CrowdSec inválida"
        return 1
    }

    echo "OK: configuración CrowdSec válida"

    echo
    echo "===== BOUNCER CSF ====="

    if command -v csf >/dev/null 2>&1 ||
       test -x /usr/sbin/csf ||
       test -x /usr/local/csf/bin/csf; then

        systemctl start hyd-crowdsec-csf-bouncer.service || {
            echo "FAIL: primera ejecución del bouncer CSF"
            return 1
        }

        systemctl enable --now \
            hyd-crowdsec-csf-bouncer.timer || {
            echo "FAIL: no se pudo habilitar el bouncer CSF"
            return 1
        }

        echo "OK: bouncer CSF activo"
    else
        systemctl disable --now \
            hyd-crowdsec-csf-bouncer.timer \
            >/dev/null 2>&1 || true

        echo "WARN: CSF no detectado; bouncer desactivado"
    fi

    echo
    echo "===== ESTADO FINAL ====="

    systemctl --no-pager --full status \
        hyd-crowdsec-rules-sync.timer \
        hyd-crowdsec-directadmin-acquis-sync.timer \
        hyd-crowdsec-csf-bouncer.timer \
        2>/dev/null |
    grep -E \
        '^[●○ ]|Loaded:|Active:|Trigger:|Triggers:' ||
    true

    echo
    echo "OK: cliente HYD CrowdSec activado"
}

main "$@"
