#!/bin/sh

set -u
set -e

PREFIX_DIR=""
# declare the Bareos repository
DOWNLOADSERVER="download.bareos.org"
URL="https://download.bareos.org/experimental/PR-2618/Debian_12"

# setup credentials for apt auth
# (required for download.bareos.com, subscription)
BAREOS_USERNAME="username_at_example.com"
BAREOS_PASSWORD="MySecretBareosPassword"

if [ "${DOWNLOADSERVER}" = "download.bareos.com" ] && [ -d "${PREFIX_DIR}/etc/apt/auth.conf.d/" ]; then
    cat <<EOT >"${PREFIX_DIR}/etc/apt/auth.conf.d/download_bareos_com.conf"
machine download.bareos.com
login ${BAREOS_USERNAME}
password ${BAREOS_PASSWORD}
EOT
    chmod 0600 "${PREFIX_DIR}/etc/apt/auth.conf.d/download_bareos_com.conf"
fi

# add the Bareos repository
cat <<EOT >"${PREFIX_DIR}/etc/apt/sources.list.d/bareos.sources"
Types: deb deb-src
URIs: ${URL}
Suites: /
Architectures: amd64
Signed-By: ${PREFIX_DIR}/etc/apt/keyrings/bareos-experimental.gpg
EOT

# add package key
mkdir -p "${PREFIX_DIR}/etc/apt/keyrings/"
# download key via
# wget -O /etc/apt/keyrings/bareos-experimental.gpg ${URL}/bareos-keyring.gpg
# or
cat << EOT | gpg --dearmor -o "${PREFIX_DIR}/etc/apt/keyrings/bareos-experimental.gpg"
-----BEGIN PGP PUBLIC KEY BLOCK-----

[payload removed: 1568 characters in 25 lines]
=GxhN
-----END PGP PUBLIC KEY BLOCK-----
EOT

echo "Repository ${URL} successfully added."
