#!/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
mysql -e "insert into cacti.settings set value = 0, name = 'i18n_auto_detection' ;" cacti
# Disable netbios, see bug 1129582
mysql -e "update cacti.automation_networks set enable_netbios = '' ;" 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)
debian/tests/check-all-pages

tests/tools/check_all_pages.sh -du cacti -dp ${database_pw}
