#!/usr/bin/env bash
set -euo pipefail

HOSTNAME=""
EMAIL=""
SSH_PORT="22"
SERVER_PROFILE="small"
PREFLIGHT_ONLY=0
UPSTREAM_INSTALLER_URL="${THCZ_INSTALLER_URL:-https://freewebpanel.com/downloads/thcz-install.sh}"
BUNDLE_URL="${THCZ_BUNDLE_URL:-https://freewebpanel.com/downloads/thcz-foundation-latest.tar.gz}"

usage() {
  cat <<'EOF'
FreeWebPanel installer for OpenZero AIOS / ZeroMint Linux

Example for an OpenZero AIOS web node:
  curl -fsSL https://freewebpanel.com/downloads/freewebpanel-openzero-aios-install.sh | sudo bash -s -- --hostname web.example.com --email admin@example.com

Safe preflight first:
  curl -fsSL https://freewebpanel.com/downloads/freewebpanel-openzero-aios-install.sh | sudo bash -s -- --hostname web.example.com --email admin@example.com --preflight-only

Cloudflare DNS before install:
  Create DNS-only grey-cloud A records for:
    web.example.com      -> your OpenZero AIOS server IPv4
    ns1.web.example.com  -> your OpenZero AIOS server IPv4
    ns2.web.example.com  -> your OpenZero AIOS server IPv4
  Do not proxy the panel host or nameserver hostnames through the orange cloud.

For your own domain, replace web.example.com with the hostname you want
customers to use for the panel. Keep ns1.<panel-host> and ns2.<panel-host>
on DNS-only A records so registrar glue, DNS checks, and AutoSSL can see
the real server IP.

Flags:
  --hostname <fqdn>        Panel hostname pointed to this OpenZero AIOS server
  --email <address>        Email for Let's Encrypt certificate notices
  --ssh-port <port>        SSH port to keep open in the firewall
  --server-profile <name>  small or provider; default small for AIOS desktops
  --bundle-url <url>       Optional FreeWebPanel foundation bundle URL
  --preflight-only         Write the report and exit before package changes
  --help                   Show this help

This wrapper keeps the normal Ubuntu installer untouched and enables the
OpenZero AIOS/Linux Mint compatibility lane only for this run.
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --hostname)
      HOSTNAME="$2"
      shift 2
      ;;
    --email)
      EMAIL="$2"
      shift 2
      ;;
    --ssh-port)
      SSH_PORT="$2"
      shift 2
      ;;
    --server-profile)
      SERVER_PROFILE="$2"
      shift 2
      ;;
    --bundle-url)
      BUNDLE_URL="$2"
      shift 2
      ;;
    --preflight-only|--dry-run)
      PREFLIGHT_ONLY=1
      shift
      ;;
    --help)
      usage
      exit 0
      ;;
    *)
      echo "Unknown argument: $1" >&2
      usage >&2
      exit 1
      ;;
  esac
done

if [[ "${EUID}" -ne 0 ]]; then
  echo "Run this wrapper with root privileges through sudo." >&2
  usage >&2
  exit 1
fi

if [[ -z "${HOSTNAME}" || -z "${EMAIL}" ]]; then
  echo "Both --hostname and --email are required." >&2
  usage >&2
  exit 1
fi

if [[ ! -f /etc/os-release ]]; then
  echo "Unable to detect the operating system." >&2
  exit 1
fi

source /etc/os-release

if [[ "${ID:-}" != "linuxmint" && "${ID:-}" != "ubuntu" ]]; then
  echo "This wrapper is for OpenZero AIOS, ZeroMint Linux, Linux Mint, or Ubuntu-family hosts." >&2
  echo "Detected: ${PRETTY_NAME:-unknown}" >&2
  exit 1
fi

if [[ "${SERVER_PROFILE}" != "small" && "${SERVER_PROFILE}" != "provider" && "${SERVER_PROFILE}" != "micro" ]]; then
  echo "Invalid --server-profile value. Use small, provider, or micro." >&2
  exit 1
fi

echo "FreeWebPanel OpenZero AIOS installer"
echo "Host: ${HOSTNAME}"
echo "OS: ${PRETTY_NAME:-unknown}"
echo "Profile: ${SERVER_PROFILE}"
echo "Bundle: ${BUNDLE_URL}"
echo "OpenZero detected: $([[ -d /home/zero/openzero || -f /etc/systemd/system/openzero-watchdog.service ]] && echo yes || echo not-yet)"

tmp_dir="$(mktemp -d)"
trap 'rm -rf "${tmp_dir}"' EXIT

curl -fsSL "${UPSTREAM_INSTALLER_URL}" -o "${tmp_dir}/thcz-install.sh"
chmod 700 "${tmp_dir}/thcz-install.sh"

args=(
  --hostname "${HOSTNAME}"
  --email "${EMAIL}"
  --ssh-port "${SSH_PORT}"
  --server-profile "${SERVER_PROFILE}"
  --bundle-url "${BUNDLE_URL}"
  --allow-openzero-aios
)

if [[ "${PREFLIGHT_ONLY}" -eq 1 ]]; then
  args+=(--preflight-only)
fi

"${tmp_dir}/thcz-install.sh" "${args[@]}"
