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

export BOOT_DISK2="/dev/sdc"
export BOOT_PART2="1"
export BOOT_DEVICE2="${BOOT_DISK2}${BOOT_PART2}"
export POOL_DISK2="/dev/sdc"
export POOL_PART2="2"
export POOL_DEVICE2="${POOL_DISK2}${POOL_PART2}"

zpool labelclear -f "$POOL_DISK"
zpool labelclear -f "$POOL_DISK2"

wipefs -a "$POOL_DISK"
wipefs -a "$POOL_DISK2"
wipefs -a "$BOOT_DISK"
wipefs -a "$BOOT_DISK2"

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

echo 'Dredge,DeathScrape1428' > /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 encryption=aes-256-gcm \
 -O keylocation=file:///etc/zfs/zroot.key \
 -O keyformat=passphrase \
 -o autotrim=on \
 -o compatibility=openzfs-2.3-linux \
 -m none \
 zroot mirror "$POOL_DEVICE" "$POOL_DEVICE2"


zfs create -o mountpoint=none zroot/ROOT
zfs create -o mountpoint=/ -o canmount=noauto zroot/ROOT/${ID}
zfs create -o mountpoint=/home zroot/home

zfs set sync=disabled zroot/ROOT
zfs set sync=disabled zroot/ROOT/${ID}
zfs set sync=disabled zroot/home

zpool set bootfs=zroot/ROOT/${ID} zroot

zpool export zroot
zpool import -N -R /mnt zroot
zfs load-key -L prompt zroot

zfs mount zroot/ROOT/${ID}
zfs mount zroot/home

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)"

udevadm trigger

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

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

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

cp ./script2.sh /mnt/home/

# 2. CHROOT

chroot /mnt /bin/bash << 'EOF'
chmod a+x /home/script2.sh
cd /home
./script2.sh
EOF

# 3. REBOOT

umount -n -R /mnt

zpool export zroot
reboot

#chroot /mnt /bin/bash chmod a+x /home/script2.sh; /home/script2.sh
