#!/bin/bash
#
# AirNav RadarBox feeder installer - STAGING.
#
# Points at apt-tpa-1.rb24.com (the new repository host) instead of apt.rb24.com.
# For pre-cutover validation only. Otherwise identical to the production script.
#
# The archive signing key is fetched over HTTPS from apt.rb24.com and its
# fingerprint verified before use. It is installed as a scoped keyring with
# signed-by=, so it authenticates only this repository and not every other
# source configured on the machine.

RED='\033[0;31m'
NC='\033[0m' # No Color
YELLOW='\033[1;33m'
WHITE='\033[1;37m'
LGREEN='\033[1;32m'
BOLD='\033[1m'
RESET_BOLD='\033[2m'

KEY_URL="https://apt-tpa-1.rb24.com/airnav-archive-2026.gpg"
KEY_PATH="/etc/apt/keyrings/airnav-archive-2026.gpg"
KEY_FPR="52FD03F453DD205BA98244F403C685A6082BCF18"

if [ "$(id -u)" != "0" ]; then
	echo -e "You must run this script with root privileges" 1>&2
	exit 1
fi

echo -e "${YELLOW}================================================================${NC}"
echo -e "${YELLOW} STAGING INSTALLER - uses apt-tpa-1.rb24.com, not apt.rb24.com${NC}"
echo -e "${YELLOW} For validating the new repository host before DNS cutover.${NC}"
echo -e "${YELLOW}================================================================${NC}"
echo -e ""

# Codename detection: lsb_release is not present on every minimal image.
VERS=""
if command -v lsb_release >/dev/null 2>&1; then
	VERS=$(lsb_release -cs 2>/dev/null)
fi
if [ -z "$VERS" ] && [ -r /etc/os-release ]; then
	VERS=$(. /etc/os-release; echo "${VERSION_CODENAME:-}")
fi
if [ -z "$VERS" ] && [ -r /etc/debian_version ]; then
	case "$(cut -d. -f1 /etc/debian_version)" in
		10) VERS=buster ;; 11) VERS=bullseye ;; 12) VERS=bookworm ;; 13) VERS=trixie ;;
	esac
fi
if [ -z "$VERS" ]; then
	echo "Could not determine the distribution codename." >&2
	exit 1
fi

case "$VERS" in
	buster|bullseye|bookworm|trixie) ;;
	*)
		echo "Don't know how to install for a distribution named $VERS" >&2
		exit 1
		;;
esac

# Install packages needed by this script
apt-get update -y
apt-get install -y gnupg ca-certificates curl

# Fetch the archive signing key over HTTPS and verify it before trusting it.
mkdir -p /etc/apt/keyrings
TMPKEY=$(mktemp)
if ! curl -fsSL --retry 3 "$KEY_URL" -o "$TMPKEY"; then
	echo -e "${RED}Failed to download the archive signing key from $KEY_URL${NC}" >&2
	rm -f "$TMPKEY"; exit 1
fi

GOT_FPR=$(gpg --show-keys --with-colons "$TMPKEY" 2>/dev/null | awk -F: '/^fpr:/{print $10; exit}')
if [ "$GOT_FPR" != "$KEY_FPR" ]; then
	echo -e "${RED}Archive key fingerprint mismatch - refusing to install.${NC}" >&2
	echo "  expected: $KEY_FPR" >&2
	echo "  received: ${GOT_FPR:-<none>}" >&2
	rm -f "$TMPKEY"; exit 1
fi

install -m 0644 "$TMPKEY" "$KEY_PATH"
rm -f "$TMPKEY"

# Replace any previous source definition
/bin/rm -f /etc/apt/sources.list.d/rb24.list
echo "deb [signed-by=$KEY_PATH] https://apt-tpa-1.rb24.com/ $VERS main" > /etc/apt/sources.list.d/rb24.list

# Update apt and install software
apt-get update -y
apt-get install rbfeeder -y

if [ "$VERS" != "trixie" ]; then
	while true; do
		read -p "Do you wish to install dump978-rb program? (y/n) " yn
		case $yn in
			[Yy]* )
				apt-get install dump978-rb soapysdr-module-rtlsdr -y;
				break;;
			[Nn]* )
				break;;
			* ) echo "Please answer yes or no.";;
		esac
	done
fi

echo -e "\033c"

echo -e ""
echo -e "${RED}!!!! ${BOLD}IMPORTANT${RESET_BOLD} !!!!${NC}"
echo -e ""
echo -e "${WHITE}By default, RBFeeder is configured to connect to your local dump1090 instance (localhost,"
echo -e "port 30005). If you want to use a USB RTL-SDR dongle instead, please run this command:"
echo -e ""
echo -e "${LGREEN}sudo rbfeeder --set-network-mode off --no-start${WHITE}"
echo -e ""
echo -e "Then, restart the daemon:"
echo -e ""
echo -e "${LGREEN}sudo systemctl restart rbfeeder${WHITE}"
echo -e ""
echo -e "After a few seconds, RBFeeder will connect to the AirNav servers and you can view your"
echo -e "sharing-key with this command:"
echo -e ""
echo -e "${LGREEN}sudo rbfeeder --showkey${WHITE}"
echo -e ""
echo -e "If you already have a sharing-key from previous a installation, you can set the same"
echo -e "key using this command:"
echo -e ""
echo -e "${LGREEN}sudo rbfeeder --setkey <your sharing key>${WHITE}"
echo -e ""
echo -e ""
echo -e "Installation finished."
echo -e ""
echo -e "${NC}"
