#!/bin/sh

set -e

# Test that pkgos-debcrate generates a full template package out of a
# fixture crate: correct dependency mapping in debian/control (including
# unix target deps, dev deps with <!nocheck>, and skipped git/windows
# deps), feature based Provides, a binary package stanza, copyright,
# watch file, changelog, git tag/branch, and the regenerated orig tarball.

ME=$(basename $0)
WORK=$(mktemp -d)
trap 'rm -rf "${WORK}"' EXIT

TESTDIR=$(cd "$(dirname "$0")" && pwd)
. "${TESTDIR}/fixtures.sh"

cd "${WORK}"

SERDE_REQ=1 NIX_REQ=0.30 fixture_cargo_toml ${WORK}/Cargo.toml
fixture_pack_crate ${WORK} ${WORK}/Cargo.toml
mock_git ${WORK} ${WORK}/fixture/serde_test-0.1.0 v0.1.0

cat >${WORK}/policy.txt <<EOF
librust-serde-dev=1.0.200
librust-serde-json-dev=1.0.140
librust-tokio-dev=1.53.1
librust-tempfile-dev=3.20.0
librust-cc-dev=1.2.0
librust-nix-dev=0.30.1
EOF

mock_apt ${WORK} ${WORK}/policy.txt
debcrate_env ${WORK}

"${SCRIPT}" serde_test >${WORK}/out.log 2>&1 || {
	cat ${WORK}/out.log
	echo "${ME}: pkgos-debcrate failed"
	exit 1
}

PKGDIR=${WORK}/rust-serde-test
CTRL=${PKGDIR}/debian/control

check () {
	if ! grep -q "$2" "$1" ; then
		echo "${ME}: $1 doesn't contain: $2"
		exit 1
	fi
}

check_not () {
	if grep -q "$2" "$1" ; then
		echo "${ME}: $1 unexpectedly contains: $2"
		exit 1
	fi
}

check ${CTRL} 'librust-serde-json-dev'
check ${CTRL} 'librust-tokio-dev'
check ${CTRL} 'librust-nix-dev'
check ${CTRL} 'librust-cc-dev'
check ${CTRL} 'librust-tempfile-dev <!nocheck>'
check_not ${CTRL} 'librust-winapi-dev'
check_not ${CTRL} 'librust-git-dep-dev'
check ${CTRL} 'librust-serde-test-0.1+fancy-dev'
check ${CTRL} 'librust-serde-test-0-dev'
check ${CTRL} 'Package: serde-test'
check ${CTRL} 'third-party/rust-serde-test'
check_not ${CTRL} 'override_dh_auto_test'

check ${PKGDIR}/debian/copyright 'License: Apache-2.0-or-Expat'
check ${PKGDIR}/debian/copyright 'Alice Example <alice@example.com>'
check ${PKGDIR}/debian/watch 'upstream=crates.io/serde_test'
check ${PKGDIR}/debian/changelog 'rust-serde-test (0.1.0-1) unstable'
check ${PKGDIR}/debian/source/format '3.0 (quilt)'

check ${WORK}/out.log 'Target specific dependencies not added to debian/control'
check ${WORK}/out.log 'git or a path'
check ${WORK}/out.log 'serde_test declares a minimum supported rust version of 1.70'

if [ -e /usr/share/openstack-pkg-tools/pkgos.make ] ; then
	if ! [ -e ${WORK}/rust-serde-test_0.1.0.orig.tar.xz ] ; then
		echo "${ME}: orig tarball was not generated"
		exit 1
	fi
fi

# The git flow: the upstream repository must have been cloned, reset to
# the v-prefixed upstream tag, branched to debian/unstable, with the
# debian folder copied over and committed as "Initial packaging".
CLONE=${WORK}/rust-serde-test
if ! [ -d ${CLONE}/.git ] ; then
	echo "${ME}: the upstream repository was not cloned to ${CLONE}"
	exit 1
fi
cd ${CLONE}
if [ -z "$(git tag --list v0.1.0)" ] ; then
	echo "${ME}: upstream git tag v0.1.0 is missing"
	exit 1
fi
if [ "$(git branch --show-current)" != "debian/unstable" ] ; then
	echo "${ME}: debian/unstable branch is missing or not checked out"
	exit 1
fi
if [ "$(git log --format=%s -1)" != "Initial packaging" ] ; then
	echo "${ME}: last commit isn't 'Initial packaging'"
	exit 1
fi
if ! [ -e ${CLONE}/debian/control ] ; then
	echo "${ME}: the debian folder wasn't copied into the clone"
	exit 1
fi
if [ "$(git log --format=%s -2 | sed -n 2p)" != "upstream" ] ; then
	echo "${ME}: expected the upstream import commit below 'Initial packaging'"
	exit 1
fi
cd ${WORK}

exit 0
