#!/bin/sh
set -eu

REPO_URL="${AL_BLASTER_APT_URL:-https://kucingcoder.github.io/al-blaster-gowa/apt}"
KEY_URL="${AL_BLASTER_KEY_URL:-${REPO_URL}/al-blaster.gpg}"
SUITE="${AL_BLASTER_APT_SUITE:-stable}"
COMPONENT="${AL_BLASTER_APT_COMPONENT:-main}"

KEYRING_DIR="/etc/apt/keyrings"
KEYRING_PATH="${KEYRING_DIR}/al-blaster.gpg"
SOURCE_LIST="/etc/apt/sources.list.d/al-blaster.list"
LEGACY_SOURCES="/etc/apt/sources.list.d/al-blaster.sources"

if [ "$(id -u)" -ne 0 ]; then
  echo "Error: Please run this script as root or via sudo:" >&2
  echo "  curl -fsSL https://kucingcoder.github.io/al-blaster-gowa/install.sh | sudo sh" >&2
  exit 1
fi

if ! command -v apt-get >/dev/null 2>&1 || ! command -v dpkg >/dev/null 2>&1; then
  echo "Error: This installer only supports Debian/Ubuntu-based systems (requires apt-get and dpkg)." >&2
  exit 1
fi

ARCH="$(dpkg --print-architecture)"
case "$ARCH" in
  amd64|arm64) ;;
  *)
    echo "Error: Architecture '$ARCH' is not supported. Supported architectures: amd64 and arm64." >&2
    exit 1
    ;;
esac

need_pkgs=""
if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then
  need_pkgs="${need_pkgs} curl"
fi
if [ ! -e /etc/ssl/certs/ca-certificates.crt ]; then
  need_pkgs="${need_pkgs} ca-certificates"
fi

if [ -n "$need_pkgs" ]; then
  echo "==> Installing system prerequisites:${need_pkgs}"
  apt-get update -qq
  # shellcheck disable=SC2086
  DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends $need_pkgs
fi

TMP_KEY="$(mktemp)"
trap 'rm -f "$TMP_KEY"' EXIT

echo "==> Downloading APT GPG key from ${KEY_URL}"
if command -v curl >/dev/null 2>&1; then
  curl -fsSL "$KEY_URL" -o "$TMP_KEY"
else
  wget -qO "$TMP_KEY" "$KEY_URL"
fi

if [ ! -s "$TMP_KEY" ]; then
  echo "Error: Downloaded GPG key file is empty or invalid." >&2
  exit 1
fi

install -d -m 0755 "$KEYRING_DIR"
if grep -q "BEGIN PGP PUBLIC KEY BLOCK" "$TMP_KEY" 2>/dev/null; then
  if ! command -v gpg >/dev/null 2>&1; then
    apt-get update -qq
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends gnupg
  fi
  gpg --dearmor --yes -o "$KEYRING_PATH" "$TMP_KEY"
  chmod 0644 "$KEYRING_PATH"
else
  install -m 0644 "$TMP_KEY" "$KEYRING_PATH"
fi

echo "==> Registering APT repository at ${SOURCE_LIST}"
printf 'deb [arch=%s signed-by=%s] %s %s %s\n' \
  "$ARCH" "$KEYRING_PATH" "$REPO_URL" "$SUITE" "$COMPONENT" > "$SOURCE_LIST"
chmod 0644 "$SOURCE_LIST"
rm -f "$LEGACY_SOURCES"

echo "==> Updating package index and installing al-blaster (${ARCH})..."
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y \
  -o Dpkg::Options::="--force-confdef" \
  -o Dpkg::Options::="--force-confold" \
  al-blaster

INSTALLED_VERSION="$(dpkg-query -W -f='${Version}' al-blaster 2>/dev/null || echo 'installed')"

echo ""
echo "==> Installation complete! (al-blaster ${INSTALLED_VERSION})"
echo "    Check service : sudo systemctl status al-blaster"
echo "    Open Web UI   : http://<SERVER-IP>:16969"
echo "    Update later  : sudo apt-get update && sudo apt-get install --only-upgrade al-blaster"
