
help () {
  echo "Before using pvgrub, the kernel you want to use needs to be regenerated with Qubes kernel modules in the TemplateVM:"
  echo "1/ Ensure that your kernel and kernel sources are installed (ex: pacman -S linux-lts linux-lts-headers)"
  echo "2/ Ensure that grub config file has been generated for your kernel (ex: grub-mkconfig > /boot/grub/grub.cfg)"
  echo "3/ Reinstall qubes-vm-kernel-support to ensure Qubes-OS kernel module is compiled and that the initcpio is rebuilt"
  echo "This should then be handled automatically in your next kernel updates"
}

## arg 1:  the new package version
post_install () {
  local begin='### BEGIN QUBES HOOKS ###' end='### END QUBES HOOKS ###'
  local combined="\\
$begin\\
# Set the kernel command line to what the QubesOS initramfs hook expects.\\
# If the kernel command line is wrong, the system will not boot.\\
\\
. /etc/default/grub.qubes\\
. /etc/default/grub.qubes-kernel-vm-support\\
$end"
  echo "Adding qubes required hooks to mkinitcpio.conf"
  if [[ ! -s /etc/default/grub ]]; then
    echo >> /etc/default/grub
  fi
  sed -Ei '/^HOOKS=/ s/(block)/\1 qubes/' /etc/mkinitcpio.conf
  echo 'Adding qubes required hooks to /etc/default/grub'
  sed -Ei "/^$begin\$/,/^$end\$/{
\$c$combined
d
}
\$a$combined
" /etc/default/grub
  grub-mkconfig -o /boot/grub/grub.cfg
  help
}

post_upgrade () {
  post_install
}

post_remove () {
  local begin='### BEGIN QUBES HOOKS ###' end='### END QUBES HOOKS ###'
  echo 'Removing qubes required hooks from mkinitcpio.conf'
  sed -Ei '/^HOOKS=/ s/(block) (lvm2 )?qubes/\1/' /etc/mkinitcpio.conf
  echo 'Removing qubes required hooks from /etc/default/grub'
  sed -Ei "/^$begin\$/,/^$end\$/d" /etc/default/grub
  grub-mkconfig -o /boot/grub/grub.cfg
}
