#!/bin/sh

if [ "$(whoami)" != "root" ]; then
    SUDO=sudo
fi

debianVersion=$(lsb_release -sc)
availableDebianVersion=(buster bullseye bookworm)

if ! [[ "${availableDebianVersion[*]}" =~ (^|[[:space:]])${debianVersion}($|[[:space:]]) ]]; then
        echo "This repo is only available to Debian Buster, Bullseye and bookworm";
        exit;
fi

rm -rv /etc/apt/sources.list.d/apterix.list

${SUDO} apt-get update
${SUDO} apt-get -y install lsb-release ca-certificates curl
${SUDO} curl -sSLo /usr/share/keyrings/gpg_sury.pub https://apterix.xyz/keys/gpg_sury.pub
${SUDO} curl -sSLo /usr/share/keyrings/gpg_sury-nginx-mainline.gpg https://apterix.xyz/keys/gpg_sury-nginx-mainline.gpg
${SUDO} curl -sSLo /usr/share/keyrings/gpg_apterix.pub https://apterix.xyz/keys/gpg_apterix.pub

## Sury Packages
if [[ ${debianVersion} == "bookworm" ]]; then
	${SUDO} tee -a /etc/apt/sources.list.d/apterix.list <<EOF
deb [signed-by=/usr/share/keyrings/gpg_sury.pub] https://apterix.xyz/php-sury/snapshots/latest ${debianVersion} main
deb [signed-by=/usr/share/keyrings/gpg_sury-nginx-mainline.gpg] https://apterix.xyz/nginx-sury/snapshots/latest ${debianVersion} main
EOF

fi

# Kernelboil and Utils Packages
${SUDO} tee -a /etc/apt/sources.list.d/apterix.list <<EOF
deb [arch=amd64 signed-by=/usr/share/keyrings/gpg_apterix.pub] https://apterix.xyz/repo/kernelboil-${debianVersion} ${debianVersion} main
deb [arch=amd64 signed-by=/usr/share/keyrings/gpg_apterix.pub] https://apterix.xyz/repo/utils-${debianVersion} ${debianVersion} main
EOF

${SUDO} curl -sSLo /etc/apt/preferences.d/apterix https://apterix.xyz/apterix_apt_preferences

if [[ ${debianVersion} == "bookworm" ]]; then
	${SUDO} tee -a /etc/apt/sources.list.d/apterix.list <<EOF
deb [arch=amd64 signed-by=/usr/share/keyrings/gpg_apterix.pub] https://apterix.xyz/repo/mariadb-${debianVersion} ${debianVersion} main
EOF

fi
