#!/bin/sh
# tns-deposit-service-preflight — Standalone preflight dependency checker
#
# Usage:
#   tns-deposit-service-preflight
#
# Exits 0 if all required checks pass.
# Exits 1 if any required check fails.

set -e

PACKAGE="tns-deposit-service"

ok()    { printf "  [\033[32m OK \033[0m] %s\n" "$1"; }
warn()  { printf "  [\033[33mWARN\033[0m] %s\n" "$1"; }
fail()  { printf "  [\033[31mFAIL\033[0m] %s\n" "$1"; }
info()  { printf "         %s\n" "$1"; }

PREFLIGHT_FAILED=0
PREFLIGHT_WARNED=0

mark_failed()  { PREFLIGHT_FAILED=$((PREFLIGHT_FAILED + 1)); }
mark_warned()  { PREFLIGHT_WARNED=$((PREFLIGHT_WARNED + 1)); }

echo ""
echo "=========================================================="
echo "  ${PACKAGE} — Preflight Dependency Check"
echo "=========================================================="
echo ""

# ---- Python 3.8+ ----
if command -v python3 >/dev/null 2>&1; then
    PYVER=$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])' 2>/dev/null || echo "unknown")
    PYMAJOR=$(echo "$PYVER" | cut -d. -f1)
    PYMINOR=$(echo "$PYVER" | cut -d. -f2)
    if [ "$PYMAJOR" = "3" ] && [ "$PYMINOR" -ge 8 ] 2>/dev/null; then
        ok "Python ${PYVER} found"
    else
        fail "Python 3.8+ required, found ${PYVER}"
        mark_failed
    fi
else
    fail "python3 not found on PATH"
    info "Install with: apt-get install python3   (or dnf install python3)"
    mark_failed
fi

# ---- Python modules ----
check_pymod() {
    modname="$1"
    debpkg="$2"
    rpmpkg="$3"
    pippkg="$4"
    if python3 -c "import ${modname}" >/dev/null 2>&1; then
        ok "Python module '${modname}' available"
    else
        fail "Python module '${modname}' not importable"
        info "Debian/Ubuntu: apt-get install ${debpkg}"
        info "RHEL/CentOS:   dnf install ${rpmpkg}"
        info "Or via pip:    pip3 install ${pippkg}"
        mark_failed
    fi
}
check_pymod "yaml"      "python3-yaml"     "python3-pyyaml"   "pyyaml"
check_pymod "requests"  "python3-requests" "python3-requests" "requests"
check_pymod "scapy.all" "python3-scapy"    "python3-scapy"    "scapy"

# ---- libpcap ----
if ldconfig -p 2>/dev/null | grep -q libpcap; then
    PCAPVER=$(ldconfig -p | grep -m1 libpcap | awk '{print $1}')
    ok "libpcap available (${PCAPVER})"
else
    warn "libpcap shared library not found in ld cache"
    info "Debian/Ubuntu: apt-get install libpcap0.8"
    info "RHEL/CentOS:   dnf install libpcap"
    mark_warned
fi

# ---- systemd ----
if [ -d /run/systemd/system ]; then
    ok "systemd is running"
else
    warn "systemd does not appear to be the active init system"
    info "Service unit installed but cannot be managed via systemctl."
    mark_warned
fi

# ---- setcap (capability tooling) ----
if [ -x /usr/sbin/setcap ] || [ -x /sbin/setcap ]; then
    ok "setcap available"
else
    warn "setcap not available — only root deployment supported"
    info "Debian/Ubuntu: apt-get install libcap2-bin"
    info "RHEL/CentOS:   dnf install libcap"
    mark_warned
fi

# ---- Service config file ----
if [ -f /etc/${PACKAGE}/config.yaml ]; then
    ok "Config file present at /etc/${PACKAGE}/config.yaml"
else
    fail "Config file missing at /etc/${PACKAGE}/config.yaml"
    info "Reinstall the package to restore the default config."
    mark_failed
fi

# ---- Service binary ----
if [ -x /usr/sbin/${PACKAGE} ]; then
    ok "Service binary present at /usr/sbin/${PACKAGE}"
else
    fail "Service binary missing at /usr/sbin/${PACKAGE}"
    info "Reinstall the package."
    mark_failed
fi

# ---- Running user (informational) ----
if [ "$(id -u)" = "0" ]; then
    ok "Running as root (required for packet capture)"
else
    info "Note: preflight is running as non-root user. The service runs as root by default."
fi

echo ""
echo "----------------------------------------------------------"
if [ "$PREFLIGHT_FAILED" -gt 0 ]; then
    echo "  ${PREFLIGHT_FAILED} REQUIRED check(s) failed."
    echo "  Service will not start until resolved."
    echo "----------------------------------------------------------"
    echo ""
    exit 1
elif [ "$PREFLIGHT_WARNED" -gt 0 ]; then
    echo "  Required checks PASSED."
    echo "  ${PREFLIGHT_WARNED} warning(s) — review above."
    echo "----------------------------------------------------------"
    echo ""
    exit 0
else
    echo "  All checks PASSED."
    echo "----------------------------------------------------------"
    echo ""
    exit 0
fi
