#!/bin/sh

set -e

. ./helpers.sh

# A degraded package, with a version that rmadison does not know: each of
# the five operations must do an atomic commit with the right title, and
# the bullets must be appended to the current changelog entry.

set_stub_rmadison "" 0
make_fixture "1.0.0-1" unstable "openstack-pkg-tools" \
	"UPSTREAM_GIT := https://github.com/openstack/openstack-pkg-tools.git"

run_modernize
assert_eq "exit code" "${MODERNIZE_RC}" "0"

assert_eq "commit count" "$(git log --format=%s | wc -l)" "6"
assert_contains "commit 1" "$(git log --format=%s | sed -n 6p)" "Initial packaging of openstack-pkg-tools"
assert_contains "commit 2" "$(git log --format=%s | sed -n 5p)" "Update debian/salsa-ci.yml"
assert_contains "commit 3" "$(git log --format=%s | sed -n 4p)" "Update debian/watch to version=4 mode=git"
assert_contains "commit 4" "$(git log --format=%s | sed -n 3p)" "Remove the Priority: field from debian/control"
assert_contains "commit 5" "$(git log --format=%s | sed -n 2p)" "Upgrade the package to debhelper-compat (= 13)"

BULLETS=$(sed -n '/Initial release/,$p' debian/changelog)
assert_contains "changelog bullet salsa-ci" "${BULLETS}" "* Update debian/salsa-ci.yml"
assert_contains "changelog bullet watch" "${BULLETS}" "* Update debian/watch to version=4 mode=git"
assert_contains "changelog bullet priority" "${BULLETS}" "* Remove the Priority: field from debian/control"
assert_contains "changelog bullet compat" "${BULLETS}" "* Upgrade the package to debhelper-compat (= 13)"
assert_contains "changelog bullet SV" "${BULLETS}" "* Update Standards-Version to 4.7.4"
assert_contains "version kept" "$(dpkg-parsechangelog -SVersion)" "1.0.0-1"

assert_contains "salsa-ci content" "$(cat debian/salsa-ci.yml)" "SALSA_CI_DISABLE_BUILD_PACKAGE_I386: 1"
assert_not_contains "priority removed" "$(cat debian/control)" "Priority:"
assert_contains "compat upgraded" "$(cat debian/control)" "debhelper-compat (= 13)"
assert_not_contains "SV updated" "$(cat debian/control)" "4.6.2"
assert_contains "SV value" "$(cat debian/control)" "Standards-Version: 4.7.4"
assert_eq "compat file removed" "$(test -f debian/compat && echo yes || echo no)" "no"

WATCH=$(cat debian/watch)
assert_contains "watch version" "${WATCH}" "version=4"
assert_contains "watch opts" "${WATCH}" 'opts="mode=git,uversionmangle=s/\.0rc/~rc/;s/\.0b1/~b1/;s/\.0b2/~b2/;s/\.0b3/~b3/" \'
assert_contains "watch url" "${WATCH}" "https://github.com/openstack/openstack-pkg-tools refs/tags/(\d[brc\d\.]+)"

assert_eq "clean tree" "$(git status --porcelain | wc -l)" "0"

exit 0
