#!/usr/bin/env bash
# install-thunderbird.sh — Install Thunderbird.
# On Ubuntu: uses the Mozilla Team PPA (with APT pinning so Snap transition is bypassed).
# On Debian: uses the regular Debian package (no PPA available).
# Hardened: distro-detect, idempotent, --dry-run.

set -euo pipefail
IFS=$'\n\t'

readonly SCRIPT_NAME="${0##*/}"
DRY_RUN=0

usage() {
  cat <<EOF
Usage: sudo $SCRIPT_NAME [--dry-run] [--help]

Installs Thunderbird.
  - Ubuntu: removes Snap version (if any), adds Mozilla Team PPA, pins it,
    and installs the .deb. This avoids the Ubuntu snap transition wrapper.
  - Debian: installs from the standard Debian repos.

Options:
  --dry-run    Print actions without executing.
  --help, -h   Show this help.
EOF
}

log()  { printf '\033[1;34m[%s]\033[0m %s\n' "${SCRIPT_NAME%.sh}" "$*"; }
warn() { printf '\033[1;33m[%s] WARN:\033[0m %s\n' "${SCRIPT_NAME%.sh}" "$*" >&2; }
die()  { printf '\033[1;31m[%s] ERROR:\033[0m %s\n' "${SCRIPT_NAME%.sh}" "$*" >&2; exit 1; }
run()  { if (( DRY_RUN )); then printf '  DRY-RUN: %s\n' "$*"; else eval "$@"; fi; }
trap 'rc=$?; (( rc )) && printf "\033[1;31m[%s] failed at line %s (exit %d)\033[0m\n" "${SCRIPT_NAME%.sh}" "$LINENO" "$rc" >&2' ERR

while (( $# )); do
  case "$1" in
    --dry-run) DRY_RUN=1 ;;
    -h|--help) usage; exit 0 ;;
    *) die "Unknown argument: $1 (try --help)" ;;
  esac
  shift
done

(( EUID == 0 )) || die "Must run as root. Try: sudo $SCRIPT_NAME"

[[ -r /etc/os-release ]] || die "/etc/os-release not found."
# shellcheck disable=SC1091
. /etc/os-release
log "Detected: ${PRETTY_NAME:-unknown} (id=${ID:-?})"

export DEBIAN_FRONTEND=noninteractive

case "${ID:-}" in
  ubuntu)
    log "Removing Thunderbird Snap (if present)..."
    if command -v snap >/dev/null 2>&1; then
      run "snap remove --purge thunderbird >/dev/null 2>&1 || true"
    fi

    log "Installing prerequisites..."
    run "apt-get update -qq"
    run "apt-get install -y software-properties-common"

    log "Adding Mozilla Team PPA..."
    PPA_LIST=/etc/apt/sources.list.d/mozillateam-ubuntu-ppa-*.list
    # shellcheck disable=SC2086
    if ! ls $PPA_LIST >/dev/null 2>&1; then
      run "add-apt-repository -y ppa:mozillateam/ppa"
    else
      log "Mozilla Team PPA already configured."
    fi

    PIN=/etc/apt/preferences.d/mozillateamppa
    log "Pinning Mozilla Team PPA so .deb wins over Snap transition wrapper..."
    if [[ ! -f "$PIN" ]] || ! grep -q 'release o=LP-PPA-mozillateam' "$PIN"; then
      if (( DRY_RUN )); then
        printf '  DRY-RUN: write %s\n' "$PIN"
      else
        cat >"$PIN" <<'EOF'
Package: thunderbird*
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 1001
EOF
      fi
    fi

    log "Installing thunderbird from PPA..."
    run "apt-get update -qq"
    run "apt-get install -y --allow-downgrades thunderbird"
    ;;

  debian)
    log "Installing thunderbird from Debian repos..."
    run "apt-get update -qq"
    run "apt-get install -y thunderbird"
    ;;

  *)
    die "Unsupported distro: ${PRETTY_NAME:-unknown}. Supported: ubuntu, debian."
    ;;
esac

log "Done."
