Only run power restore on AC losses

Added a meson build option that allows the customer to enable automatic
power restore policy only for AC losses where the chassis power was
on prior to a BMC reset(and off when it comes back online).

The default is set to 'false' which provides the standard behavior of
APR which is to always run APR on an BMC reboot.
If the option 'only-run-apr-on-power-loss' is 'true' then APR is only
run in the case of the chassis losing power. AC power losses trigger the
creation of a file called 'chassis@0-lost-power' that identifies an
AC power loss has occurred. This generated file can be found in the
directory '/run/openbmc/'.

Tested:
    - Verified that when built with option 'only-run-apr-on-power-loss'
      set to false(default) that APR was ran.
    - Verified that when built with option 'only-run-apr-on-power-loss'
      set to true that apr was not ran when a power loss did not occur.
    - Verified that when built with option 'only-run-apr-on-power-loss'
      set to true that apr was only ran when a power loss occured.

Jun 08 01:55:13 p10bmc phosphor-chassis-state-manager[3009]:
            The system suffered a power blackout.

root@p10bmc:/lib/systemd/system# ls /run/openbmc/
chassis@0-lost-power

Signed-off-by: Corey Hardesty <corey.hardesty@icloud.com>
Change-Id: Iac37761416d699e3a469936e00868a8d9b6dfdb1
diff --git a/discover_system_state.cpp b/discover_system_state.cpp
index 1623922..837f0d1 100644
--- a/discover_system_state.cpp
+++ b/discover_system_state.cpp
@@ -6,6 +6,8 @@
 #include "xyz/openbmc_project/Common/error.hpp"
 #include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
 
+#include <fmt/format.h>
+#include <fmt/printf.h>
 #include <getopt.h>
 #include <systemd/sd-bus.h>
 
@@ -14,6 +16,7 @@
 #include <sdbusplus/exception.hpp>
 #include <sdbusplus/server.hpp>
 
+#include <filesystem>
 #include <iostream>
 #include <map>
 #include <string>
@@ -120,6 +123,18 @@
         {
             // one_time is set to None so use the customer setting
             info("One time not set, check user setting of power policy");
+
+#ifdef ONLY_RUN_APR_ON_POWER_LOSS
+            std::string chassisLostPowerFileFmt =
+                fmt::sprintf(CHASSIS_LOST_POWER_FILE, hostId);
+            fs::path chassisPowerLossFile{chassisLostPowerFileFmt};
+            if (!fs::exists(chassisPowerLossFile))
+            {
+                info(
+                    "Chassis power was not on prior to BMC reboot so do not run any power policy");
+                return 0;
+            }
+#endif
             auto reply = bus.call(methodUserSetting);
             reply.read(result);
             powerPolicy = std::get<std::string>(result);