#!/bin/sh
# Check that purging the package removes its runtime directories.
#
# /var/lib/phpmyadmin is created by debian/dirs, and debian/postinst creates
# /var/backups/phpmyadmin to keep a weak blowfish_secret.inc.php or a leftover
# webbased config.inc.php. Both must be gone after a purge, and /etc/phpmyadmin
# (which holds the conffiles) with them.
set -eu

# SC1091: the library path is computed at run time, so it cannot be
# followed statically. Its contents are checked on their own.
# shellcheck disable=SC1091
. "$(dirname "$0")/lib/helpers.sh"

PKG=phpmyadmin
DIRS="/etc/phpmyadmin /var/lib/phpmyadmin /var/backups/phpmyadmin"

# The package under test has to be installed to be purged; autopkgtest does not
# install it for us because this test needs a database-less environment.
echo "Installing $PKG (no recommendation, no database configured) ..."
dep8_preseed "$PKG" "$PKG/dbconfig-install" boolean false
dep8_preseed "$PKG" "$PKG/reconfigure-webserver" multiselect ""
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends "$PKG" \
    || dep8_fail "apt-get install $PKG failed"

# Give every directory something to hold, so an empty removal cannot pass.
mkdir -p /var/backups/phpmyadmin /var/lib/phpmyadmin/tmp
echo secret >/var/backups/phpmyadmin/blowfish_secret.inc.php.dep8
echo cache >/var/lib/phpmyadmin/tmp/dep8-cache
: >/etc/phpmyadmin/dep8-leftover

echo "Purging $PKG ..."
DEBIAN_FRONTEND=noninteractive dpkg --purge "$PKG" || dep8_fail "dpkg --purge $PKG failed"

for dir in $DIRS; do
    [ ! -e "$dir" ] || dep8_fail "$dir survived the purge"
done

dep8_pass "purge removed $DIRS"
