#!/usr/bin/env bash

# Sections:
# 1. LIVEOS
# 2. CHROOT
# 3. REBOOT

# 1. LIVEOS

source /etc/os-release
export ID
rpm -e --nodeps zfs-fuse
dnf config-manager setopt updates.enabled=0
dnf --releasever=${VERSION_ID} install -y \
  https://zfsonlinux.org/fedora/zfs-release-3-0$(rpm --eval "%{dist}").noarch.rpm

dnf install -y https://dl.fedoraproject.org/pub/fedora/linux/releases/${VERSION_ID}/Everything/x86_64/os/Packages/k/kernel-devel-$(uname -r).rpm

dnf install -y zfs sgdisk
zgenhostid -f 0x00bab10c
export BOOT_DISK="/dev/disk/by-id/ata-Samsung_SSD_850_EVO_M.2_500GB_S24CNXAH103603Y"
export BOOT_PART="-part1"
export BOOT_DEVICE="${BOOT_DISK}${BOOT_PART}"
export POOL_DISK="/dev/disk/by-id/ata-Samsung_SSD_850_EVO_M.2_500GB_S24CNXAH103603Y"
export POOL_PART="-part2"
export POOL_DEVICE="${POOL_DISK}${POOL_PART}"


zpool labelclear -f "$POOL_DISK"

wipefs -a "$POOL_DISK"
wipefs -a "$BOOT_DISK"

sgdisk --zap-all "$POOL_DISK"
sgdisk --zap-all "$BOOT_DISK"
sgdisk -n "${BOOT_PART}:1m:+512m" -t "${BOOT_PART}:ef00" "$BOOT_DISK"
sgdisk -n "${POOL_PART}:0:-10m" -t "${POOL_PART}:bf00" "$POOL_DISK"

echo 'Dredge,DeathScrape2027' > /etc/zfs/zroot.key
chmod 000 /etc/zfs/zroot.key

zpool create -f \
 -o ashift=12 \
 -O compression=lz4 \
 -O acltype=posixacl \
 -O xattr=sa \
 -O relatime=on \
 -O keylocation=file:///etc/zfs/zroot.key \
 -O keyformat=passphrase \
 -o autotrim=on \
 -o compatibility=openzfs-2.3-linux \
 -m none \
 vore "$POOL_DEVICE"

# -O encryption=aes-256-gcm \
echo 'Dredge,DeathScrape2027'

# zfs create -O encryption=aes-256-gcm -o mountpoint=none  zroot/ROOT
	
zfs create -o mountpoint=/ -o canmount=noauto vore/rootfs/${ID}

zfs create -o mountpoint=/home vore/home -O encryption=aes-256-gcm
zfs create -o mountpoint=/home vore/root -O encryption=aes-256-gcm

#zfs set sync=disabled zroot/ROOT
zfs set sync=disabled vore/rootfs/${ID}
zfs set sync=disabled vore/home
zfs set sync=disabled vore/root

zpool set bootfs=vore/rootfs/${ID} vore

echo 'Dredge,DeathScrape2027'

zpool export vore
zpool import -N -R /mnt vore
zfs load-key -L prompt vore/home
zfs load-key -L prompt vore/root

zfs mount vore/rootfs/${ID}
zfs mount vore/home
zfs mount vore/root

mount | grep mnt

echo should see:
echo 
echo "zroot/ROOT/fedora on /mnt type zfs (rw,relatime,xattr,posixacl)"
echo "zroot/home on /mnt/home type zfs (rw,relatime,xattr,posixacl)"

sleep 10

udevadm trigger

mkdir /run/install
mount /dev/live-base /run/install

sleep 5
rsync -pogAXtlHrDx \
 --stats \
 --exclude=/boot/efi/* \
 --exclude=/etc/machine-id \
 --info=progress2 \
 /run/install/ /mnt

mv /mnt/etc/resolv.conf /mnt/etc/resolv.conf.orig
cp /etc/hostid /mnt/etc
cp -L /etc/resolv.conf /mnt/etc
mkdir -p /mnt/etc/zfs
cp /etc/zfs/zroot.key /mnt/etc/zfs

# 2. CHROOT

mount -t proc proc /mnt/proc
mount -t sysfs sys /mnt/sys
mount -B /dev /mnt/dev
mount -t devpts pts /mnt/dev/pts

chroot /mnt /bin/bash << 'TOP_LEVEL_EOF'
#/usr/bin/env bash

#ID=fedora
#export ID
#export BOOT_DISK="/dev/sda"
#export BOOT_PART="1"
#export BOOT_DEVICE="${BOOT_DISK}${BOOT_PART}"
#export POOL_DISK="/dev/sda"
#export POOL_PART="2"
#export POOL_DEVICE="${POOL_DISK}${POOL_PART}"


cat << EOF > /etc/dracut.conf.d/zol.conf
nofsck="yes"
add_dracutmodules+=" zfs "
omit_dracutmodules+=" btrfs "
install_items+=" /etc/zfs/zroot.key "
EOF

source /etc/os-release

rpm -e --nodeps zfs-fuse

dnf config-manager setopt updates.enabled=0

dnf --releasever=${VERSION_ID} install -y \
  https://zfsonlinux.org/fedora/zfs-release-3-0$(rpm --eval "%{dist}").noarch.rpm

dnf install -y https://dl.fedoraproject.org/pub/fedora/linux/releases/${VERSION_ID}/Everything/x86_64/os/Packages/k/kernel-devel-$(uname -r).rpm

dnf install -y zfs

dnf install -y zfs-dracut

dnf config-manager setopt updates.enabled=1

dracut --force --regenerate-all

zfs set org.zfsbootmenu:commandline="quiet rhgb" zroot/ROOT

zfs set org.zfsbootmenu:keysource="zroot/ROOT/${ID}" zroot

mkfs.vfat -F32 "$BOOT_DEVICE"

cat << EOF >> /etc/fstab
$( blkid | grep "$BOOT_DEVICE" | cut -d ' ' -f 2 ) /boot/efi vfat defaults 0 0
EOF

mkdir -p /boot/efi
mount /boot/efi

mkdir -p /boot/efi/EFI/ZBM
curl -o /boot/efi/EFI/ZBM/VMLINUZ.EFI -L https://get.zfsbootmenu.org/efi
cp /boot/efi/EFI/ZBM/VMLINUZ.EFI /boot/efi/EFI/ZBM/VMLINUZ-BACKUP.EFI

mount -t efivarfs efivarfs /sys/firmware/efi/efivars


#efibootmgr -c -d "$BOOT_DISK" -p "$BOOT_PART" \
#  -L "ZFSBootMenu (Backup)" \
#  -l '\EFI\ZBM\VMLINUZ-BACKUP.EFI'

#efibootmgr -c -d "$BOOT_DISK" -p "$BOOT_PART" \
#  -L "ZFSBootMenu" \
#  -l '\EFI\ZBM\VMLINUZ.EFI'



mv /etc/resolv.conf.orig /etc/resolv.conf

#exit
TOP_LEVEL_EOF

# 3. REBOOT

#umount -n -R /mnt
#zpool export zroot
#reboot
