#!/bin/sh
# Small local model: records manager wire arguments, never runs a service or sudo.
set -eu
name=${0##*/}
printf '%s\n' "$name" "$@" >> "$FAKE_STATE/calls"
case "$name" in
    sudo)
        [ "$1" = -n ]; shift
        case "$1" in /usr/bin/systemctl|/usr/bin/systemd-run)
            tool=${1##*/}; shift; exec "$(dirname -- "$0")/$tool" "$@" ;;
            *) exit 99 ;;
        esac
        ;;
    systemd-run)
        if [ "${1:-}" = --help ]; then echo --expand-environment; exit 0; fi
        unit=
        timer=no
        for arg do
            case "$arg" in
                --unit=*) unit=${arg#--unit=} ;;
                --on-active=*) timer=yes ;;
            esac
        done
        [ -n "$unit" ]
        [ ! -e "$FAKE_STATE/$unit.args" ]
        printf '%s\n' "$@" > "$FAKE_STATE/$unit.args"
        printf 'active\n' > "$FAKE_STATE/$unit.state"
        case "$unit" in *-owner.service)
            [ -f "$FAKE_STATE/${unit%-owner.service}-cleanup.timer.state" ]
            mkdir -m 700 "$FAKE_WORKSPACE"
            printf '%s\n' "$FAKE_NONCE" > "$FAKE_WORKSPACE/.pi-c-owner"
            chmod 600 "$FAKE_WORKSPACE/.pi-c-owner"
            ;;
            *-worker.service)
                [ -f "$FAKE_WORKSPACE/worker.log" ] || {
                    echo 'FAIL: manager would create an unreadable root-owned log' >&2
                    exit 1
                }
                ;;
        esac
        if [ "$timer" = yes ]; then
            printf 'active\n' > "$FAKE_STATE/${unit%.service}.timer.state"
        fi
        ;;
    systemctl)
        if [ "$1" = --version ]; then printf 'systemd 259 (test fake)\n'; exit; fi
        action=$1
        shift
        case "$action" in
            show)
                unit=$1
                shift
                property=$1
                case "$property" in
                    --property=LoadState)
                        if [ -f "$FAKE_STATE/$unit.state" ]; then
                            printf 'loaded\n'
                        else
                            printf 'not-found\n'
                        fi
                        ;;
                    --property=ActiveState)
                        if [ "${FAIL_TIMER_VERIFY:-}" = yes ]; then exit 1; fi
                        if [ -f "$FAKE_STATE/$unit.state" ]; then
                            cat "$FAKE_STATE/$unit.state"
                        else
                            printf 'inactive\n'
                        fi
                        ;;
                    --property=ControlGroup) printf '\n' ;;
                    --property=Unit) printf '%s\n' "${unit%.timer}.service" ;;
                    --property=NextElapseUSecMonotonic) printf '600000000\n' ;;
                    *) printf 'Result=success\nExecMainStatus=0\nSubState=exited\n' ;;
                esac
                ;;
            stop)
                for unit do rm -f "$FAKE_STATE/$unit.state"; done
                ;;
            start)
                [ "$1" = "pi-c-s0-$FAKE_NONCE-cleanup.service" ]
                sh "$FAKE_CLEANUP" "$FAKE_HOME" "$FAKE_WORKSPACE" "$FAKE_NONCE"
                ;;
            reset-failed) : ;;
            *) exit 1 ;;
        esac
        ;;
    *) exit 1 ;;
esac
