#!/bin/sh
# Add Daniel Rosehill's APT repository (amd64), optionally installing packages.
#   curl -fsSL https://apps.danielrosehill.com/apt/setup.sh | sudo sh
#   curl -fsSL https://apps.danielrosehill.com/apt/setup.sh | sudo sh -s -- voice-notepad nfc-gui
# Package list: https://apps.danielrosehill.com/apt/
# Source: https://github.com/danielrosehill/APT-Public-Repo/blob/main/site/setup.sh
set -eu
FPR=5FAFDD5CBF6E8C1E9AA696DD5E3303E305985278
BASE=https://apps.danielrosehill.com/apt
KEYRING=/etc/apt/keyrings/danielrosehill-archive-keyring.gpg
SOURCES=/etc/apt/sources.list.d/danielrosehill.sources
[ "$(id -u)" = 0 ] || { echo "Run as root: curl -fsSL $BASE/setup.sh | sudo sh" >&2; exit 1; }
[ "$(dpkg --print-architecture)" = amd64 ] || { echo "This repository is amd64 only." >&2; exit 1; }
# No hard Ubuntu version check: packages differ. Some (voice-notepad, parlance) need
# Python 3.14, i.e. Ubuntu 26.04; apt reports it if a package cannot install here.
# Older instructions wrote a one-line .list with a keyring in /usr/share/keyrings. Remove
# it first: with its keyring missing, or alongside the .sources below with a different
# Signed-By, apt-get update fails, including the one that installs curl/gpg.
rm -f /etc/apt/sources.list.d/danielrosehill.list /usr/share/keyrings/danielrosehill-apt.gpg
command -v curl >/dev/null || { apt-get update -q && apt-get install -y -q curl; }
command -v gpg >/dev/null || { apt-get update -q && apt-get install -y -q gpg; }
tmp=$(mktemp); trap 'rm -f "$tmp"' EXIT
curl -fsSL "$BASE/key.gpg" -o "$tmp"
gpg --show-keys --with-colons "$tmp" | grep -q "^fpr:*$FPR:" || { echo "Key fingerprint mismatch; not installing." >&2; exit 1; }
install -d -m 0755 /etc/apt/keyrings
install -m 0644 "$tmp" "$KEYRING"
cat > "$SOURCES" <<SRC
Types: deb
URIs: $BASE
Suites: stable
Components: main
Architectures: amd64
Signed-By: $KEYRING
SRC
apt-get update -q
if [ "$#" -gt 0 ]; then
    apt-get install -y "$@"
fi
echo "Repository added. Install packages with: sudo apt install <name>  (list: $BASE/)"
