#!/bin/sh
# Copyright 2024 Simon McVittie
# SPDX-License-Identifier: LGPL-2.1-or-later

# Ensure that if the content of libgtk2.0-0t64 is taken over by some
# other package libgtk2.0-0xyz, then that other package will not trigger
# a bug equivalent to #1065493.

set -eux

export DEBIAN_FRONTEND=noninteractive
n=0
failed=0
binary_package="libgtk2.0-0t64"
future_binary_package="libgtk2.0-0xyz"
srcdir="$(pwd)"
tmpdir="$(mktemp -d)"
cd "$tmpdir"

assert () {
    n=$(( n + 1 ))

    if "$@"; then
        echo "ok $n - $*"
    else
        echo "not ok $n - $* exit status $?"
        failed=1
    fi
}

assert_not () {
    n=$(( n + 1 ))

    if ! "$@"; then
        echo "ok $n - unsuccessful as expected: $*"
    else
        echo "not ok $n - should not have succeeded: $*"
        failed=1
    fi
}

# Add a deb822-formatted apt source at this location if you are testing a
# locally-built gtk+2.0 before upload
if [ -e "$srcdir/debian/tests/manual/local-1065493.sources" ]; then
    install -m644 -t /etc/apt/sources.list.d/ -D \
        "$srcdir/debian/tests/manual/local-1065493.sources"
fi

# For more convenient manual testing
if ! dpkg-query -W dpkg-repack; then
    apt-get -y update
    apt-get -y install "$binary_package"
    apt-get -y install dpkg-dev dpkg-repack
fi

this_tuple="$(dpkg-architecture -qDEB_HOST_MULTIARCH)"

# This assumes that libgtk2.0-0t64 has at least one each of Breaks, Provides
# and Replaces, and will need to be adjusted if that assumption is broken in
# the future for whatever reason.
dpkg-repack --generate "$binary_package"
grep -q '^Breaks:' dpkg-repack.*/DEBIAN/control
grep -q '^Provides:' dpkg-repack.*/DEBIAN/control
grep -q '^Replaces:' dpkg-repack.*/DEBIAN/control
# The $ substitutions in the Perl expressions are to be expanded by Perl,
# not by the shell, so:
# shellcheck disable=SC2016
env \
    binary_package="$binary_package" \
    future_binary_package="$future_binary_package" \
    perl -p -i \
        -e 's/^Package:.*$/Package: $ENV{future_binary_package}/;' \
        -e 's/^(Breaks:.*)$/$1, $ENV{binary_package}/;' \
        -e 's/^(Provides:.*)$/$1, $ENV{binary_package}/;' \
        -e 's/^(Replaces:.*)$/$1, $ENV{binary_package}/;' \
        dpkg-repack.*/DEBIAN/control
dpkg-deb --build dpkg-repack.* "$future_binary_package.deb"
apt-get -y install ./"$future_binary_package.deb"

assert test -e "/usr/lib/$this_tuple/gtk-2.0/2.10.0/immodules.cache"
assert test -s "/usr/lib/$this_tuple/gtk-2.0/2.10.0/immodules.cache"

# Purging the "old" (pre-transition) binary package does not destroy the
# IM modules cache
apt-get -y purge "$binary_package"

assert test -e "/usr/lib/$this_tuple/gtk-2.0/2.10.0/immodules.cache"
assert test -s "/usr/lib/$this_tuple/gtk-2.0/2.10.0/immodules.cache"

# Purging the "new" (post-transition) binary package still *does* destroy the
# IM modules cache
apt-get -y purge "$future_binary_package"

assert_not test -e "/usr/lib/$this_tuple/gtk-2.0/2.10.0/immodules.cache"
assert_not test -s "/usr/lib/$this_tuple/gtk-2.0/2.10.0/immodules.cache"

echo "1..$n"
exit "$failed"

# vim:set sw=4 sts=4 et:
