#!/bin/bash -e

MACHINES=""
for nspawn in $(ls /etc/systemd/nspawn/*.nspawn); do
    nspawn_file=$(basename $nspawn)
    machine=${nspawn_file%.*}
    MACHINES="$MACHINES$machine "
done
STARTED=""
DEGRADED=""
found=true
idx=0
while [ $found = true ]; do
    found=false
    echo "tentative $idx"
    for machine in $MACHINES; do
        if ! echo $STARTED | grep -q " $machine "; then
            status=$(machinectl -q shell $machine /usr/bin/systemctl is-system-running 2>/dev/null || echo "not started")
            if echo "$status" | grep -q degraded; then
                STARTED="$STARTED $machine "
                DEGRADED="$DEGRADED $machine"
            elif echo "$status" | grep -q running; then
                STARTED="$STARTED $machine "
            else
                found=true
                echo "status actuel de $machine : $status"
            fi
        fi
    done
    sleep 2
    idx=$((idx+1))
    if [ $idx = 60 ]; then
        break
    fi
done
retcode=0
for machine in $MACHINES; do
    if ! echo "$STARTED" | grep -q " $machine "; then
        echo
        echo "========= $machine"
        machinectl -q shell $machine /usr/bin/systemctl is-system-running 2>/dev/null || systemctl status systemd-nspawn@$machine.service || true
    fi
done
echo $DEGRADED
for machine in $DEGRADED; do
    echo
    echo "========= $machine"
    machinectl -q shell $machine /usr/bin/systemctl --state=failed --no-legend --no-pager
    retcode=1
done

exit $retcode