#!/bin/sh

set -u
set -e

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

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

if [ "${DOWNLOADSERVER}" = "download.bareos.com" ]; then
    scheme="$(echo "${URL}" | cut -d / -f 1)"
    rest="$(echo "${URL}" | cut -d / -f 3-)"
    URL="${scheme}//${BAREOS_USERNAME}:${BAREOS_PASSWORD}@${rest}"
fi

mkdir -p "${PREFIX_DIR}/usr/local/etc/pkg/repos"
  cat << EOT > "${PREFIX_DIR}/usr/local/etc/pkg/repos/bareos.conf"
Bareos: {
  url: "${URL}",
  mirror_type: "none",
  signature_type: "none",
  enabled: yes
}
EOT
chmod 0640 "${PREFIX_DIR}/usr/local/etc/pkg/repos/bareos.conf"

echo "Repository ${URL} successfully added."
