#!/bin/bash -e
# A script which is called in a host-reboot path to check the host reboot count
# and either initiate the host firmware boot, or move it to the quiesce state
# (if the reboot count has been reached)

# This script has a single required parameter, the instance number of the
# host that is being rebooted (or quiesced)

set -euo pipefail

get_reboot_count()
{
    busctl get-property xyz.openbmc_project.State.Host"$1" \
        /xyz/openbmc_project/state/host"$1" \
        xyz.openbmc_project.Control.Boot.RebootAttempts AttemptsLeft \
        | cut -d ' ' -f2
}

get_restart_cause()
{
    busctl get-property xyz.openbmc_project.State.Host"$1" \
        /xyz/openbmc_project/state/host"$1" \
        xyz.openbmc_project.State.Host RestartCause \
        | cut -d '"' -f2
}

# host instance id is input parameter to function
host_quiesce()
{
    systemctl start obmc-host-quiesce@"$1".target
}

# host instance id is input parameter to function
host_reboot()
{
    systemctl start obmc-host-startmin@"$1".target
}

# This service is run as a part of a systemd target to reboot the host
# As such, it has to first shutdown the host, and then reboot it. Due to
# systemd complexities with Conflicts statements between the targets to
# shutdown and to boot, need to start off by sleeping 5 seconds to ensure
# the shutdown path has completed before starting the boot (or quiesce)
sleep 5

host_instance=$1

# Normally the standard power on path will determine if the system has reached
# its reboot count and halt in Quiesce if so. But in the host-crash path this
# standard path is not utilized, as it goes only through systemd targets. To
# ensure the system does not end up in an endless reboot loop, put host into
# Quiesce if reboot count is exhausted and the reason for the reboot was a
# HostCrash
reboot_count=$(get_reboot_count "$host_instance")
restart_cause=$(get_restart_cause "$host_instance")
if [ "$reboot_count" -eq 0 ] && \
   [ "$restart_cause" == "xyz.openbmc_project.State.Host.RestartCause.HostCrash" ];
then
    echo "reboot count is 0 and host crashed, go to host quiesce"
    host_quiesce "$host_instance"
else
    echo "reboot count ($reboot_count) is greater then 0 or host did not" \
         "crash so reboot host"
    host_reboot "$host_instance"
fi