#!/bin/sh -e
set -e

check_service_status() {
    SERVICE_NAME=$1
    PATTERN=$2

    systemctl is-enabled --quiet "$SERVICE_NAME" || (echo "Service $SERVICE_NAME is not enabled" && exit 1)
    echo "Service $SERVICE_NAME is enabled"
    journalctl --no-pager -b -u "$SERVICE_NAME"

    systemctl show -p Result "$SERVICE_NAME" | grep Result=success || (echo "Service $SERVICE_NAME did not complete successfully" && exit 1)
    echo "Service $SERVICE_NAME completed successfully"

    # parse the journal and find to expected lines, which correspond to the no-op case
    journalctl --no-pager -b -u "$SERVICE_NAME" | grep -q "$PATTERN" || (echo "Warning: Expected pattern not found" && exit 1)
    echo "Expected pattern found in journal for $SERVICE_NAME"
}

case "$AUTOPKGTEST_REBOOT_MARK" in
  "") 
    echo "test beginning"; 
    check_service_status nvme-interrupt-coalescing.service;
    check_service_status dgx-desktop-pro-activation.service;
    /tmp/autopkgtest-reboot mark1 
    ;;
  mark1) 
    echo "Testing again after a reboot";
    check_service_status nvme-interrupt-coalescing.service;
    check_service_status dgx-desktop-pro-activation.service;
    ;;
esac

echo "test end"
