#!/bin/bash
# backendsidemon-info — show how to reach BackendSideMon: URLs, login, files, commands.
# Run by the .deb/.rpm after install or upgrade; run it again any time:
#   sudo /opt/backendsidemon/backendsidemon-info
APP_DIR=${BSM_DIR:-/opt/backendsidemon}
CFG="$APP_DIR/config.json"

# Value of a top-level-ish key in config.json (written by the service with Go's indentation,
# but tolerate any spacing). Strings come back without quotes.
cfg() {
    grep -o "\"$1\"[[:space:]]*:[[:space:]]*[^,}]*" "$CFG" 2>/dev/null | head -n1 |
        sed -e 's/^[^:]*:[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/^"//' -e 's/"$//'
}

if [ ! -r "$CFG" ]; then
    echo "BackendSideMon: cannot read $CFG (run as root)." >&2
    exit 1
fi

LISTEN_IP=$(cfg listen_ip);   LISTEN_IP=${LISTEN_IP:-0.0.0.0}
HTTP_PORT=$(cfg listen_port); HTTP_PORT=${HTTP_PORT:-8080}
HTTPS_PORT=$(cfg https_port); HTTPS_PORT=${HTTPS_PORT:-8443}
TLS=$(grep -o '"tls"[[:space:]]*:[[:space:]]*{[^}]*' "$CFG" | grep -o '"enabled"[[:space:]]*:[[:space:]]*true')
ADMIN_USER=$(cfg admin_username); ADMIN_USER=${ADMIN_USER:-admin}
ADMIN_PASS=$(cfg admin_password)
DB_PATH=$(cfg path)
case "$DB_PATH" in /*) ;; *) DB_PATH="$APP_DIR/${DB_PATH#./}" ;; esac

# Addresses a browser or MonDesk can use. 0.0.0.0 / :: means every interface: list the host's
# non-loopback addresses instead (127.0.0.1 is useless from another machine).
ADDRS=()
if [ "$LISTEN_IP" = "0.0.0.0" ] || [ "$LISTEN_IP" = "::" ] || [ -z "$LISTEN_IP" ]; then
    if command -v ip >/dev/null 2>&1; then
        while read -r a; do ADDRS+=("$a"); done < <(ip -o -4 addr show scope global 2>/dev/null | awk '{print $4}' | cut -d/ -f1)
    fi
    if [ ${#ADDRS[@]} -eq 0 ]; then
        for a in $(hostname -I 2>/dev/null); do case "$a" in *:*|127.*) ;; *) ADDRS+=("$a") ;; esac; done
    fi
    [ ${#ADDRS[@]} -eq 0 ] && ADDRS+=("<server-ip>")
else
    ADDRS+=("$LISTEN_IP")
fi

STATE=$(systemctl is-active backendsidemon 2>/dev/null)

echo ""
echo "================================================================"
echo "  BackendSideMon"
echo "================================================================"
echo "  Service      : backendsidemon ($STATE)"
echo ""
echo "  Open in a browser or add to the MonDesk app (server type Linux):"
for a in "${ADDRS[@]}"; do
    [ -n "$TLS" ] && echo "    https://$a:$HTTPS_PORT   (self-signed certificate: accept it once)"
    echo "    http://$a:$HTTP_PORT"
done
echo ""
echo "  Username     : $ADMIN_USER"
echo "  Password     : $ADMIN_PASS"
echo ""
echo "  Config file  : $CFG"
echo "                 (admin_password, listen_ip, listen_port, https_port, tls;"
echo "                  restart the service after editing)"
echo "  Database     : $DB_PATH"
echo "  Certificates : $APP_DIR/certs/"
echo ""
echo "  Restart      : systemctl restart backendsidemon"
echo "  Status       : systemctl status backendsidemon"
echo "  Logs         : journalctl -u backendsidemon -f"
echo "  Show again   : $APP_DIR/backendsidemon-info"
if systemctl is-active firewalld >/dev/null 2>&1; then
    echo ""
    echo "  firewalld is active; to allow remote access:"
    echo "    firewall-cmd --permanent --add-port=$HTTP_PORT/tcp && firewall-cmd --reload"
elif command -v ufw >/dev/null 2>&1 && ufw status 2>/dev/null | grep -q "Status: active"; then
    echo ""
    echo "  ufw is active; to allow remote access:"
    echo "    ufw allow $HTTP_PORT/tcp"
fi
if [ "$STATE" != "active" ]; then
    echo ""
    echo "  The service is not running. Start it: systemctl start backendsidemon"
fi
echo "  Use HTTPS (Settings → TLS in the web UI) before exposing it to the internet."
echo "================================================================"
echo ""
