#!/bin/bash

# omarchy:summary=Remove hibernation setup including swap and boot resume settings
# omarchy:requires-sudo=true

MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf"
RESUME_REBUILD_PENDING="/var/lib/omarchy/hibernation-resume-rebuild"
SWAP_SUBVOLUME="/swap"
SWAP_FILE="$SWAP_SUBVOLUME/swapfile"

# Check if hibernation is configured (including partial setup or stale resume parameters)
if ! grep -qs "^HOOKS+=(resume)$" "$MKINITCPIO_CONF" &&
  [[ -z $(omarchy-boot-cmdline get resume) && ! -f $RESUME_REBUILD_PENDING && ! -f $SWAP_FILE ]] &&
  ! grep -Fq "$SWAP_FILE" /etc/fstab 2>/dev/null; then
  echo "Hibernation is not set up"
  exit 0
fi

if ! gum confirm "Remove hibernation setup?"; then
  exit 0
fi

# Disable swap if active
if swapon --show | grep -q "$SWAP_FILE"; then
  echo "Disabling swap on $SWAP_FILE"
  sudo swapoff "$SWAP_FILE"
fi

# Remove swapfile
if [[ -f $SWAP_FILE ]]; then
  echo "Removing swapfile"
  sudo rm "$SWAP_FILE"
fi

# Remove swap subvolume
if sudo btrfs subvolume show "$SWAP_SUBVOLUME" &>/dev/null; then
  echo "Removing Btrfs subvolume $SWAP_SUBVOLUME"
  sudo btrfs subvolume delete "$SWAP_SUBVOLUME"
fi

# Remove fstab entry
if grep -Fq "$SWAP_FILE" /etc/fstab; then
  echo "Removing swapfile from /etc/fstab"
  sudo cp -a /etc/fstab "/etc/fstab.$(date +%Y%m%d%H%M%S).back"
  sudo sed -i "\|$SWAP_FILE|d" /etc/fstab
  sudo sed -i '/^# Btrfs swapfile for system hibernation$/d' /etc/fstab
fi

# Remove mkinitcpio resume hook and the kernel parameters setup added
rebuild_initramfs=false
if [[ -f $MKINITCPIO_CONF ]]; then
  echo "Removing resume hook"
  sudo rm -f "$MKINITCPIO_CONF" || exit 1
  rebuild_initramfs=true
fi

if [[ -n $(omarchy-boot-cmdline get resume) || -f $RESUME_REBUILD_PENDING ]]; then
  echo "Removing resume kernel parameters"
  sudo omarchy-boot-cmdline unset resume || exit 1
  sudo rm -f "$RESUME_REBUILD_PENDING" || exit 1
  rebuild_initramfs=true
fi

if [[ -n $(omarchy-boot-cmdline get rtc-alarm) ]]; then
  sudo omarchy-boot-cmdline unset rtc-alarm || exit 1
  rebuild_initramfs=true
fi

if $rebuild_initramfs; then
  echo "Regenerating initramfs..."
  sudo omarchy-boot-rebuild
fi

echo "Hibernation removed"
