#!/bin/sh
# Sets this machine's apt up for the ET packages repository (Debian 13, as root), once;
# then apt install et-server (the base; plugins: etadm, et-nq, et-jaymod, et-omnibot,
# et-maps), apt upgrade for updates:
#
#   rm -f /tmp/et-setup.sh && /usr/lib/apt/apt-helper download-file http://deb.sonak.fr/et/setup.sh /tmp/et-setup.sh && sh /tmp/et-setup.sh
#
# apt-helper comes with apt: no curl nor wget needed on a minimal Debian. It downloads as
# the _apt user and doesn't replace a file _apt can't write (a copy left by root): it
# keeps the old one and still succeeds, hence the rm first.
set -e

URL=http://deb.sonak.fr/et

if [ "$(id -u)" != 0 ]; then
	echo "run as root" >&2
	exit 1
fi

# The engine, the mod and Omni-Bot are 32-bit: apt only resolves their i386 libraries
# once the architecture is enabled.
if ! dpkg --print-foreign-architectures | grep -qx i386; then
	dpkg --add-architecture i386
fi

# The repository's signing key, checked against the one this script was published with.
# Downloaded into a folder of _apt's (apt-helper's user), then installed by root.
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
chown _apt "$tmp"
/usr/lib/apt/apt-helper download-file "$URL/et.asc" "$tmp/et.asc" SHA256:a6a05db38a969eb872f1680408b88eb8ba18f1a3495ce98df5348ef8612a1e19
install -d -m 0755 /etc/apt/keyrings
install -m 0644 "$tmp/et.asc" /etc/apt/keyrings/et.asc

cat > /etc/apt/sources.list.d/et.sources <<EOF
Types: deb
URIs: $URL
Suites: trixie
Components: main
Architectures: amd64 i386
Signed-By: /etc/apt/keyrings/et.asc
EOF

apt-get update

echo "ET packages repository ready: apt install et-server (then etadm, et-nq, et-jaymod, et-omnibot, et-maps when wanted)"
