#!/bin/bash

# Open this file
# sed -i "s#^HOOKS=.*#HOOKS=(base udev autodetect modconf block keymap encrypt lvm2 resume  filesystems keyboard fsck)#g" /etc/mkinitcpio.conf
hooks="base udev autodetect modconf block keymap encrypt lvm2 resume filesystems keyboard fsck"
hooks="$hooks $(perl -ne 'print $1 if /^HOOKS=\((.*)\)/' /etc/mkinitcpio.conf)"
hooks="$(perl -pe 's|\s+|\n|g' <<< "$hooks" | sort | uniq | tr '\n' ' ')"
sed -i "s#^HOOKS=.*#HOOKS=($hooks)#g" /etc/mkinitcpio.conf
vim /etc/mkinitcpio.conf
# and replace HOOKS="..." with HOOKS="base udev autodetect modconf block keymap encrypt lvm2 resume filesystems keyboard fsck"
# use "i" key to edit (insert something), ESC and ":wq" to write changes and quit

# Regenerate initrd image
mkinitcpio -p linux

# If you got warnings about missing firmware for wd719x and aic94xx, you can ignore it, with high probability you don't even have this hardware
# But you can install it from AUR if you actually use it

# Change grub config
echo "GRUB_ENABLE_CRYPTODISK=y" >> /etc/default/grub
sed -i "s#^GRUB_CMDLINE_LINUX=.*#GRUB_CMDLINE_LINUX=\"cryptdevice=UUID=$(blkid /dev/sda3 -s UUID -o value):lvm resume=/dev/mapper/vg0-swap\"#g" /etc/default/grub
grub-mkconfig -o /boot/grub/grub.cfg

# If you got errors "/run/lvm/lvmetad.socket: connect failed: No such file or directory", that's OK
# you can get rid of this errors with some workarounds, but this is not really necessary
# but in any case DO NOT disable lvmetad! This installation will not work without it

# Install grub
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ArchLinux

