# shellcheck shell=bash
# Source this file from any script.
# On exit of the script (success or error): it copies /var/log/debusine
# to $AUTOPKGTEST_ARTIFACTS/logs/name_of_script making it available
# as artifact.

function copy_logs ()
{
	SUBDIRECTORY=$(basename "$0")
	DESTINATION="$AUTOPKGTEST_ARTIFACTS/logs/$SUBDIRECTORY"

	echo "Copying journals for debusine units to $DESTINATION"
	mkdir -p "$DESTINATION"
	journalctl -u 'debusine-server*.service' > "$DESTINATION/debusine-server.log"
	journalctl -u 'debusine-signing*.service' > "$DESTINATION/debusine-signing.log" 2>/dev/null || true
	journalctl -u 'debusine-worker*.service' > "$DESTINATION/debusine-worker.log" 2>/dev/null || true
	journalctl -u nginx.service > "$DESTINATION/nginx.log"

	echo "Copying list of work requests to $DESTINATION/list-work-requests.yaml"
	debusine work-request list --yaml --limit 10000 > "$DESTINATION/list-work-requests.yaml"

	WR_DESTINATION="$DESTINATION/work-requests"
	echo "Copying work request debug logs to $WR_DESTINATION"
	debian/tests/utils/get-work-request-debug-logs.py "$WR_DESTINATION"
}

trap copy_logs EXIT
