#!/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"

# Check if hibernation is configured
if [[ ! -f $MKINITCPIO_CONF ]] || ! grep -q "^HOOKS+=(resume)$" "$MKINITCPIO_CONF"; then
  echo "Hibernation is not set up"
  exit 0
fi

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

SWAP_SUBVOLUME="/swap"
SWAP_FILE="$SWAP_SUBVOLUME/swapfile"

# 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
echo "Removing resume hook"
sudo rm "$MKINITCPIO_CONF"

echo "Regenerating initramfs..."
sudo limine-mkinitcpio

echo "Hibernation removed"
