#!/usr/bin/env bash
set -Eeuo pipefail

thisDir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
source "$thisDir/.constants.sh" \
	'<target-dir> <suite> <mirror> <secmirror>' \
	'rootfs stretch http://deb.debian.org/debian http://security.debian.org'

targetDir="${1:-}"; shift || eusage 'missing target-dir'
suite="${1:-}"; shift || eusage 'missing suite'
mirror="${1:-}"; shift || eusage 'missing mirror'
secmirror="${1:-}"; shift || eusage 'missing secmirror'
[ -n "$targetDir" ]

comp='main'

# https://github.com/tianon/go-aptsources/blob/e066ed9cd8cd9eef7198765bd00ec99679e6d0be/target.go#L16-L58
{
	case "$suite" in
		sid|unstable|testing)
			echo "deb $mirror $suite $comp"
			;;

		*)
			echo "deb $mirror $suite $comp"
			echo "deb $mirror $suite-updates $comp"
			echo "deb $secmirror $suite/updates $comp"
			;;
	esac
} > "$targetDir/etc/apt/sources.list"
chmod 0644 "$targetDir/etc/apt/sources.list"
