#!/bin/bash
set -euo pipefail

ZIP_URL="https://frostygmrs.com/software/asl3.zip"
REPO_URL="http://repo.frostygmrs.com"
KEY_URL="${REPO_URL}/key/frostygmrs-archive-keyring.gpg"
KEYRING_PATH="/usr/share/keyrings/frostygmrs-archive-keyring.gpg"
SOURCE_LIST="/etc/apt/sources.list.d/frostygmrs.list"
CRON_FILE="/etc/cron.d/frostygmrs-asl3"
TMP_DIR="$(mktemp -d /tmp/frostygmrs-asl3.XXXXXX)"

cleanup() {
    rm -rf "$TMP_DIR"
}
trap cleanup EXIT

require_root() {
    if [ "${EUID}" -ne 0 ]; then
        echo "Please run this script as root."
        exit 1
    fi
}

need_cmd() {
    command -v "$1" >/dev/null 2>&1
}

install_pkg_if_missing() {
    local pkg="$1"
    if ! dpkg -s "$pkg" >/dev/null 2>&1; then
        apt-get update
        DEBIAN_FRONTEND=noninteractive apt-get install -y "$pkg"
    fi
}

download_file() {
    local url="$1"
    local out="$2"

    if need_cmd curl; then
        curl -fsSL "$url" -o "$out"
    elif need_cmd wget; then
        wget -qO "$out" "$url"
    else
        echo "Neither curl nor wget is installed."
        exit 1
    fi
}

write_repo_file() {
    cat > "$SOURCE_LIST" <<EOF
deb [signed-by=${KEYRING_PATH}] ${REPO_URL} stable main
EOF
    chmod 644 "$SOURCE_LIST"
}

write_cron_file() {
    cat > "$CRON_FILE" <<'EOF'
* * * * * root /usr/bin/flock -n /tmp/asl3-update-astdb.lock /usr/bin/asl3-update-astdb >> /var/log/asl3-update-astdb.log 2>&1
* * * * * root /usr/bin/flock -n /tmp/asl3-update-nodelist.lock /usr/bin/asl3-update-nodelist >> /var/log/asl3-update-nodelist.log 2>&1
EOF
    chmod 644 "$CRON_FILE"
}

main() {
    require_root

    echo "Installing required packages..."
    install_pkg_if_missing unzip
    install_pkg_if_missing ca-certificates
    if ! need_cmd curl && ! need_cmd wget; then
        install_pkg_if_missing curl
    fi

    echo "Downloading ASL3 helper package..."
    download_file "$ZIP_URL" "$TMP_DIR/asl3.zip"

    echo "Extracting files..."
    unzip -o "$TMP_DIR/asl3.zip" -d "$TMP_DIR/unpacked" >/dev/null

    if [ ! -f "$TMP_DIR/unpacked/asl3-update-astdb" ]; then
        echo "Missing asl3-update-astdb in asl3.zip"
        exit 1
    fi

    if [ ! -f "$TMP_DIR/unpacked/asl3-update-nodelist" ]; then
        echo "Missing asl3-update-nodelist in asl3.zip"
        exit 1
    fi

    echo "Installing scripts..."
    install -m 755 "$TMP_DIR/unpacked/asl3-update-astdb" /usr/bin/asl3-update-astdb
    install -m 755 "$TMP_DIR/unpacked/asl3-update-nodelist" /usr/bin/asl3-update-nodelist

    echo "Installing FrostyGMRS repo key..."
    mkdir -p /usr/share/keyrings
    download_file "$KEY_URL" "$KEYRING_PATH"
    chmod 644 "$KEYRING_PATH"

    echo "Installing FrostyGMRS repo source..."
    write_repo_file

    echo "Creating cron entries..."
    write_cron_file

    echo "Updating package lists..."
    apt-get update

    echo "Running initial ASL3 updates..."
    /usr/bin/asl3-update-astdb || true
    /usr/bin/asl3-update-nodelist || true

    echo
    echo "FrostyGMRS ASL3 install complete."
    echo "Installed:"
    echo "  /usr/bin/asl3-update-astdb"
    echo "  /usr/bin/asl3-update-nodelist"
    echo "  $KEYRING_PATH"
    echo "  $SOURCE_LIST"
    echo "  $CRON_FILE"
    echo
    echo "You can now install FrostyGMRS packages with:"
    echo "  apt install frostygmrs-supermon"
    echo
    echo "Future package updates:"
    echo "  apt update && apt upgrade -y"
}

main "$@"