#!/bin/sh

set -e

save_log_files() {
    # debian test
    cp -rf /var/log/cacti "${AUTOPKGTEST_ARTIFACTS}" || true
    # upstream test
    cp -rf /tmp/check-all-pages/ "${AUTOPKGTEST_ARTIFACTS}" || true
}

database_pw=$(grep -m1 database_password /etc/cacti/debian.php | \
		  awk '{print $3}' | sed "s/';//" | sed "s/'//")
database_pw=${database_pw%%\";}
database_pw=${database_pw#\"}

## Stop the service from interfering
systemctl stop cactid

# Test suite should run fine with non-English locale
mysql -e "insert into cacti.settings set value = 'nl-NL', name = 'i18n_default_language' ;" cacti || true
mysql -e "insert into cacti.settings set value = 0, name = 'i18n_auto_detection' ;" cacti || true
# Disable netbios, see bug 1129582
mysql -e "update cacti.automation_networks set enable_netbios = '' ;" cacti

# As we don't skip installation anymore, we need to advance the system a bit
cacti_upstream_ver=$(dpkg-query -f '${Version}' -W 'cacti' | sed -e s/\+ds[0-9]-.*// -e s/~beta[0-9]*//)
mysql -e "UPDATE version SET cacti='$cacti_upstream_ver' ;" cacti

# To make sure that the autopkgtest/CI sites store the information
if [ -n "${AUTOPKGTEST_ARTIFACTS}" ] ; then
    trap save_log_files 0
fi

# https://salsa.debian.org/salsa-ci-team/pipeline/-/issues/417
# artifacts aren't copied; I hope swapping the trap and the test works (but needs checking)
if [ $1 = "debian" ] ; then
    debian/tests/check-all-pages
elif [ $1 = "upstream" ] ; then
    tests/tools/check_all_pages.sh -du cacti -dp ${database_pw}
else
    echo "missing or unknown option"
    exit 1
fi
