#!/bin/sh
# DEP-8 test for the PSStatus plugin.
#
# PSStatus determines whether a process is running by calling pidof(1) (or
# pgrep(1) in regex mode); both are provided by the procps package. We enable
# the plugin, start a process we control (sleep) and assert that phpsysinfo
# reports it as running (Status="1").
#
# Without pidof/procps the executeProgram() call fails, the process is never
# added to the plugin result, and phpsysinfo silently reports Status="0" for a
# process that is actually running -- so this test fails, proving that
# phpsysinfo needs procps for this feature.
set -e

tests_dir=$(dirname "$0")
work_ini="${AUTOPKGTEST_TMP:-/tmp}/psstatus.ini"

# Derive the config from the full one shipped with the tests (avoids
# duplicating it): enable only PSStatus and look for the process we start.
# The [psstatus] section already defaults to ACCESS="command", USE_REGEX=false.
sed -e 's/^PLUGINS=.*/PLUGINS="PSStatus"/' \
    -e 's/^PROCESSES=.*/PROCESSES="sleep"/' \
    "$tests_dir/phpsysinfo.ini" > "$work_ini"

# Start a long-running, well-known process to look for.
sleep 3600 &
sleep_pid=$!
trap 'kill "$sleep_pid" 2>/dev/null || true' EXIT
# Give it a moment to appear in the process table.
sleep 1

xml=$(php "$tests_dir/psstatus-webpage.php" "$work_ini")
printf '%s\n' "$xml"

if printf '%s' "$xml" | grep -q 'Name="sleep" Status="1"'; then
    echo "PASS: PSStatus reported the running 'sleep' process as up (pidof works)"
else
    echo "FAIL: PSStatus did not report 'sleep' as running -- is pidof (procps) missing?" >&2
    exit 1
fi
