#!/bin/sh

set -xe

. /etc/os-release
supported_os="ubuntu debian"
dist=${dist:-$VERSION_CODENAME}
os=${os:-$ID}

if ! echo "${supported_os}" | grep "${os}"; then
    if [ "$ID_LIKE" = "ubuntu" ] && [ -n "$UBUNTU_CODENAME" ]; then
        os=$ID_LIKE
        dist=$UBUNTU_CODENAME
    fi
fi

if [ -n "$nightly" ]; then
    dist=${dist}-nightly
fi

echo "Detected $os $dist"

curl -Lso /usr/share/keyrings/zonemaster-archive-keyring.gpg https://package.zonemaster.net/zonemaster-archive-keyring.gpg

cat <<EOF > /etc/apt/sources.list.d/zonemaster.sources
Types: deb deb-src
URIs: https://package.zonemaster.net/${os}/
Suites: ${dist}
Architectures: $(dpkg --print-architecture)
Components: main
Signed-By: /usr/share/keyrings/zonemaster-archive-keyring.gpg
EOF

apt-get update
