#!/bin/bash
# Healthcheck for gokapi (public file-share, https://share.okit.pl via npm@VPS)

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# The port is bound to the Tailscale interface only, so localhost won't answer.
# Read the bind IP from .env (same file compose uses for the port mapping).
if [ -f "$SCRIPT_DIR/.env" ]; then
    # shellcheck disable=SC1091
    source "$SCRIPT_DIR/.env"
fi
BIND_IP="${TAILSCALE_BIND_IP:-127.0.0.1}"

# Container must be running
if ! docker ps --filter "name=gokapi" --filter "status=running" | grep -qw "gokapi"; then
    echo "[FAIL] gokapi container is not running"
    exit 1
fi

# Web server must answer (setup wizard or login page, either is fine)
if ! curl -sf --max-time 5 "http://${BIND_IP}:53842/" > /dev/null; then
    echo "[FAIL] gokapi is not responding on ${BIND_IP}:53842"
    exit 1
fi

echo "[OK] gokapi is healthy"
exit 0
