#!/bin/sh
# PCP QA Test No. 9000
# If systemd is in the mix, check systemctl status for all PCP
# units.
#
# Copyright (c) 2025 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

verbose=false
[ $# -gt 0 -a X"$1" = X--verbose ] && verbose=true

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ "`$PCP_PS_PROG -p 1 -o comm=`" != systemd ] && _notrun "systemd not active"
[ -d "$PCP_SYSTEMDUNIT_DIR" ] || _notrun "systemd dir $PCP_SYSTEMDUNIT_DIR not found"

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
    # end
}

# real QA test starts here
echo "Silence is golden ..."

fmt="%-28.28s %-9.9s %-13.13s %-11.11s %-12.12s %-12.12s %-12.12s %-12.12s\n"
$verbose && printf "$fmt" "Unit" "LoadState" "UnitFileState" "ActiveState" "TriggeredBy" "SubState" "ConditionResult"

( cd $PCP_SYSTEMDUNIT_DIR; ls -d pcp-*.service pcp-*.timer pm*.service pm*.timer 2>/dev/null ) \
| while read unit
do
    [ -f "$PCP_SYSTEMDUNIT_DIR/$unit" ] || continue
    systemctl status -l --no-pager "$unit" >>$seq_full
    systemctl show --no-pager "$unit" >$tmp.tmp
    LoadState='missing'
    eval `grep ^LoadState= $tmp.tmp`
    UnitFileState='missing'
    eval `grep ^UnitFileState= $tmp.tmp`
    ActiveState='missing'
    eval `grep ^ActiveState= $tmp.tmp`
    TriggeredBy='missing'
    eval `grep ^TriggeredBy= $tmp.tmp`
    SubState='missing'
    eval `grep ^SubState= $tmp.tmp`
    ConditionResult='missing'
    eval `grep ^ConditionResult= $tmp.tmp`
    $verbose && printf "$fmt" "$unit" "$LoadState" "$UnitFileState" "$ActiveState" "$TriggeredBy" "$SubState" "$ConditionResult"
    echo "unit=$unit LoadState=$LoadState UnitFileState=$UnitFileState ActiveState=$ActiveState TriggeredBy=$TriggeredBy SubState=$SubState ConditionResult=$ConditionResult" >>$seq_full
    if [ "$LoadState" = loaded -a "$UnitFileState" = disabled ]
    then
	echo "$unit: loaded but disabled" >>$seq_full
	continue
    fi
    if [ "$LoadState" = loaded -a "$ActiveState" = active ]
    then
	echo "$unit: loaded and active" >>$seq_full
	continue
    fi
    if [ "$LoadState" = loaded -a "$ActiveState" = inactive -a "$SubState" = dead -a "$TriggeredBy" != missing ]
    then
	echo "$unit: loaded and waiting for $TriggeredBy" >>$seq_full
	continue
    fi
    # pcp-reboot-init.service is a special "one" shot and
    # the resulting ActiveState (SubState) can be "inactive (dead)" or
    # "active (exited)"
    #
    if [ "$unit" = pcp-reboot-init.service ]
    then
	if [ "$ActiveState+$SubState" = "active+exited" -o \
	     "$ActiveState+$SubState" = "inactive+dead" ]
	then
	    continue
	fi
    fi
    if [ "$SubState" = dead ]
    then
	echo "$unit: BAD LoadState=$LoadState UnitFileState=$UnitFileState ActiveState=$ActiveState Substate=dead TriggeredBy=$TriggeredBy"
	systemctl status -l --no-pager "$unit"
	continue
    fi
    echo "$unit: BAD Unclassified LoadState=$LoadState UnitFileState=$UnitFileState ActiveState=$ActiveState TriggeredBy=$TriggeredBy SubState=$SubState"
    systemctl status -l --no-pager "$unit"
done

# success, all done
exit
