#!/bin/sh
set -e

case "$1" in
  configure)
    systemctl daemon-reload || true
    systemctl enable proxdock.service >/dev/null 2>&1 || true
    systemctl restart proxdock.service || true

    # Print the getting-started banner whenever the node has not yet
    # completed setup — fresh installs AND reinstalls/upgrades of an
    # unconfigured node. Each run issues a fresh initial password
    # (replacing the previous one).
    if ! proxdock configured >/dev/null 2>&1; then
      PW="$(proxdock recovery-password -initial || true)"
      LISTEN_PORT=8443
      ADDRS="$(proxdock addrs 2>/dev/null | sed "s/$/:${LISTEN_PORT}/; s|^|https://|" | tr '\n' ' ')"
      [ -n "$ADDRS" ] || ADDRS="https://<this-host>:${LISTEN_PORT}"
      echo ""
      echo "──────────────────────────────────────────────────────────────"
      echo " proxdock installed and running."
      echo ""
      echo "   Web UI:            ${ADDRS}"
      if [ -n "$PW" ]; then
        echo "   Initial password:  ${PW}"
        echo ""
        echo " Log in with an EMPTY username to complete setup. The password"
        echo " is single-use and is invalidated when setup completes."
      else
        echo "   (could not generate an initial password — run:"
        echo "    proxdock recovery-password)"
      fi
      echo " If locked out later:  proxdock recovery-password"
      echo "──────────────────────────────────────────────────────────────"
      echo ""
      if ! docker compose version >/dev/null 2>&1; then
        echo "WARNING: 'docker compose' not found. Install Docker Engine +"
        echo "Compose v2 (docker-ce from Docker's apt repo, or docker.io)"
        echo "before creating applications."
        echo ""
      fi
    fi
    ;;
esac

exit 0
