xen_boot() {
    cat << __EOF__
You are not running xen unless you boot xen.
Possible Xen boot paths:
EFI boot -> grubx64.efi -> multiboot2 -> [xen.gz, vmlinuz, ramdisk]
BIOS boot -> grub -> multiboot(2) -> [xen.gz, vmlinuz, ramdisk]

EFI boot -> xen.efi

##########
grub multiboot2 preparation:
Install grub: https://wiki.parabola.nu/index.php/GRUB

At this time, some modifications are needed to 20_linux_xen.
These are included in this package as 21_linux_xen

Set the values needed for your configuration in /etc/default/grub
Detailed here: https://www.gnu.org/software/grub/manual/html_node/Simple-configuration.html
Needed:
GRUB_CMDLINE_XEN
GRUB_CMDLINE_LINUX_XEN_REPLACE
These values are not required but can be used; they are appended to the previous values, then this is used for the non-recovery (default) entry:
GRUB_CMDLINE_XEN_DEFAULT
GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT

run grub-mkconfig

To boot xen as default:
suggested: inspect and use this config to boot with. Check if the xen entry works as expected
find the id of the xen entry and set this as DEFAULT in /etc/default/grub.
This may look something like:
GRUB_DEFAULT="xen-gnulinux-simple-a-unique-id-from-your-grub-goes-here"

run grub-mkconfig

##########
Direct EFI boot preperation:
Create a xen.cfg file in the same directory as xen.efi.
These need to be in ESP, or in a directory accessible from you EFI bootloader.
Put settings relevant to your system into xen.cfg
Detailed here: https://xenbits.xen.org/docs/4.9-testing/misc/efi.html
Needed:
kernel
ramdisk
Add the xen.efi file to your EFI bootloader (such as Refind).
And / or add the xen.efi file to you EFI boot options (efibootmgr).
__EOF__
}

install_msg() {
    cat << __EOF__
===> IMPORTANT NOTICES:

In order to complete the installation, and enable Xen,
at the very least you must:

1. Configure your bootloader to boot Xen:
__EOF__
    xen_boot
    cat << __EOF__
2. Issue the following commands to allow you to create and start VMs:

    systemctl enable xen-qemu-dom0-disk-backend.service
    systemctl enable xen-init-dom0.service
    systemctl enable xenconsoled.service

   Other optional services are:
    systemctl enable xen-watchdog.service

3. If you want some domains to automatically start up/shutdown, run the following:
    systemctl enable xendomains.service

For more information refer to the Wiki:
    https://wiki.parabola.nu/index.php/Xen

__EOF__
}

upgrade_msg() {
    cat << __EOF__
Xen 4.9
Release notes
http://wiki.xen.org/wiki/Xen_Project_4.9_Release_Notes
Feature list
http://wiki.xen.org/wiki/Xen_Project_4.9_Feature_List
__EOF__
}

upgrade_msg_grub_multiboot2() {
    cat << __EOF__
##########
Xen 4.9 can now use grub>=2.02 multiboot2.
If you previously booted using xen.efi, you have an alternative.
If you previously relied on the packaged 09_xen for grub-mkconfig:
It is now removed.
You will need to do the following under grub multiboot2 preparation:
##########
__EOF__
    xen_boot
}

post_install() {
    install_msg
    upgrade_msg
    systemd-tmpfiles --create
}

post_upgrade() {
    if [[ "$2" < 4.9.0 || "$2" == *'4.9.0rc'* ]]; then
        upgrade_msg
    fi
    if [[ "$2" < 4.9.0 || "$2" == *'4.9.0rc'* ]]; then
        upgrade_msg_grub_multiboot2
    fi
    systemd-tmpfiles --create
}

pre_remove() {
   systemctl stop xendomains.service
   systemctl stop xen-watchdog.service
   systemctl stop xenconsoled.service
   systemctl stop xen-init-dom0.service
   systemctl stop xen-qemu-dom0-disk-backend.service

   systemctl disable xendomains.service
   systemctl disable xen-watchdog.service
   systemctl disable xenconsoled.service
   systemctl disable xen-init-dom0.service
   systemctl disable xen-qemu-dom0-disk-backend.service
}

post_remove() {
    cat << __EOF__
===> IMPORTANT NOTICE:

In order to finish removing Xen, you will need to modify
your bootloader configuration files to load your Linux-libre
kernel instead of Xen kernel.
__EOF__
}
