#!/bin/bash

# On the Snapdragon X1 laptops, omarchy-qualcomm-hibernate.service stops the DSPs
# and Wi-Fi before a hibernation and brings them back when systemd stops it
# afterwards. When a hibernation fails, a driver refusing to freeze say,
# systemd leaves the unit active and its restore never runs. So after every
# hibernation attempt the unit is stopped 10 s later. After a resume it is
# already stopping, and this changes nothing.

if [[ $1 == "post" ]]; then
  case ${SYSTEMD_SLEEP_ACTION:-$2} in
    hibernate | hybrid-sleep | suspend-then-hibernate)
      if systemctl is-active --quiet omarchy-qualcomm-hibernate.service; then
        systemd-run --quiet --no-block --collect --on-active=10 --unit=omarchy-qualcomm-hibernate-recover \
          /usr/bin/systemctl --no-block stop omarchy-qualcomm-hibernate.service || true
      fi
      ;;
  esac
fi
