#!/usr/bin/env bash

# Add a sources.list that points to the gitea instance.
# https://docs.gitea.com/usage/packages/debian
# sudo apt-get install -y tea-cli

GITEA_URL="git2.ring-zero.co.uk"
OWNER="cyteen"
DISTRIBUTION="ceres"
COMPONENT="main"
KEYRING="/etc/apt/trusted.gpg.d/gitea-${OWNER}.gpg"

# system wide keyrings conflict with trusted.gpg
if [ -f /etc/apt/keyrings/gitea-cyteen.asc ]; then
  rm /etc/apt/keyrings/gitea-cyteen.asc
fi

# the existance of the apt_auth is the only difference.
# cat <<-EOF | sudo tee /etc/apt/sources.list.d/gitea.list
# 	deb [signed-by=${KEYRING}] https://${GITEA_URL}/api/packages/${OWNER}/debian ${DISTRIBUTION} ${COMPONENT}
# EOF

USERNAME="cyteen"
PASSWORD="mlpfinsonik"
# cat <<-EOF | sudo tee /etc/apt/sources.list.d/gitea-private.list
# 	deb [signed-by=${KEYRING}] https://${GITEA_URL}/api/packages/${OWNER}/debian ${DISTRIBUTION} ${COMPONENT}
# EOF
cat <<-EOF | sudo tee /etc/apt/sources.list.d/gitea-private.sources
  Types: deb
  URIs: https://${GITEA_URL}/api/packages/${OWNER}/debian/
  Suites: ${DISTRIBUTION}
  Components: ${COMPONENT} 
  Signed-By: ${KEYRING}
EOF

# fetch the gitea signed-by key
curl -fsSL https://${GITEA_URL}/api/packages/${OWNER}/debian/repository.key | gpg --dearmor -q | sudo tee ${KEYRING} >/dev/null

# cat <<-EOF | sudo gpg --dearmor -o ${KEYRING}
# 	-----BEGIN PGP ARMORED FILE-----
# 	Comment: Use "gpg --dearmor" for unpacking
#
# 	xsBNBGYk0YoBCACqBlpqZ8oNJFsTgfLEzuIUdKRKDGeK2MD05/KF5PrDBMxeJEJP
# 	wGk5q0V/ol/bQ8PxWJgpn+UitZ5Np33ksOZG0EeEb2Ejiw1idpDEEFR6jzCUj5xV
# 	Mmlg825r4iwnrM4XK80fGv2MdSa0pV7NXyrL7Jk5ReufdjX9LRtFV9nvtYRHm6sP
# 	QPTm0jLsi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H7xC34XjFV3rcfgO8w8M1qau3fc
# 	INdouQ682agGcgJQV56fBoNl1ffLdYqAs43zmr1Bnb8odgTiwgg8tx6luoF+tAMa
# 	43gPBfG87AuV2OIcubxit0I2tQuhiVRrBtNfhP/qogWZMzWg/0ruRB8KCxzmh6Ru
# 	crYHRRqetSl+hc3npVGPvX7T8mVcC+BNNiTQScM/1hZEnAlvEZ+aVD+IlaGxEmTn
# 	zORnycVVrieEG7dpquBjOTJGUOgtmQi4KrF4wd340CnC1u8Qr0PMwCRaybqI11fV
# 	ZoAVBYYnhJhC/QhCHvs/5REn2DPwePiTTXDvrNIhE/dzmlEu2qnG+6ZP
# 	=65hZ
#	 -----END PGP ARMORED FILE-----
# EOF

# rather than put username:password@ in the url to gitea we use apt_auth
conf_print_apt_auth() {
  cat <<EOF
machine ${GITEA_URL}
login ${USERNAME}
password ${PASSWORD}
EOF
}

if [ ! -f /etc/apt/auth.conf.d/${GITEA_URL} ]; then
  conf_print_apt_auth | sudo tee /etc/apt/auth.conf.d/${GITEA_URL} >/dev/null
fi

# APT pinning to prioritize git2.ring-zero.co.uk globally:
conf_print_apt_priority() {
  cat <<EOF
Package: *
Pin: origin "git2.ring-zero.co.uk"
Pin-Priority: 900
EOF
}
conf_print_apt_priority | sudo tee /etc/apt/preferences.d/gitea.pref >/dev/null

sudo apt-get update

# # Publish a package (see also /var/tmp/automate/gitea-scripts/)
# ARCHIVE="/var/cache/apt/archives/"
# PACKAGE="rustc_1.70.0+dfsg1-7_amd64.deb"
# curl --user your_username:your_password_or_token \
# 	--upload-file ${ARCHIVE}/${PACKAGE} \
# 	https://${GITEA_URL}/api/packages/${OWNER}/debian/pool/${DISTRIBUTION}/${COMPONENT}/upload
#
# # Delete a package
# curl --user ${USERNAME}:${PASSWORD} -X DELETE \
# 	https://${GITEA_URL}/api/packages/${OWNER}/debian/pools/${DISTRIBUTION}/main/test-package/1.0.0/amd64
