#!/bin/sh
#
# Copyright 2022 Johannes Schauer Marin Rodrigues <josch@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

set -eux

# This test is to prevent bugs like https://bugs.debian.org/1006771

VERSION="$(dpkg-parsechangelog -SVersion)"
apt-cache show "dash=$VERSION" >/dev/null || exit 1

DIST="$(dpkg-parsechangelog -SDistribution)"
if [ "$DIST" = "UNRELEASED" ]; then
    # take Distribution from the previous entry instead
    DIST="$(dpkg-parsechangelog -o1 -c1 -SDistribution)" || DIST="unstable"
    echo "WARN: Using Distribution: $DIST instead of UNRELEASED" >&2
fi

# regex from src:dropbear/debian/tests/remote-unlocking by Guilhem Moulin
AUTOPKGTEST_LOCAL_ARCHIVE="$(apt-get indextargets --format "\$(REPO_URI)" "Created-By: Packages" | \
    sed -nr '\,^(file|copy):((/+[^/[:blank:]]+)*/autopkgtest[.-].*[^/])/?$, {s//\2/p;q}')"

# If running on salsaci AUTOPKGTEST_LOCAL_ARCHIVE must be non-empty and an
# existing directory. If running on debci, the variable will be empty.
[ -z "$AUTOPKGTEST_LOCAL_ARCHIVE" ] || [ -d "$AUTOPKGTEST_LOCAL_ARCHIVE" ]

# We cannot use debootstrap because debootstrap cannot install from multiple
# mirrors. Since dash is Essential:yes it does not test the right thing if we
# first install with dash from unstable and then upgrade to dash from salsaci.
mmdebstrap --verbose --variant=essential \
	--customize-hook='chroot "$1" dpkg-query --showformat "\${Status} \${Version}\n" --show dash | grep "^install ok installed '"$VERSION"'"' \
	$DIST /dev/null \
	http://deb.debian.org/debian \
	${AUTOPKGTEST_LOCAL_ARCHIVE:+"deb [trusted=yes] copy://$AUTOPKGTEST_LOCAL_ARCHIVE /"}
