#cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
#disk       := /dev/disk/by-id/usb-Generic-_Multi-Card_20071114173400000-0:0
#disk       := /dev/disk/by-id/nvme-Samsung_SSD_970_EVO_Plus_250GB_S4EUNJ0N211426T
hostName    := patate
disk_ssd    := /dev/disk/by-id/ata-CT250MX500SSD1_2004E2849DD1
zpool       := $(hostName)
cipher      := aes-128-gcm
autotrim    := on
reservation := 1G

wipe:
	sudo modprobe zfs
	sudo zpool labelclear -f /dev/disk/by-partlabel/$(hostName)_ssd_zpool || true
	sudo $$(which sgdisk) --zap-all $(disk_ssd)

part: wipe
	# https://wiki.archlinux.org/index.php/BIOS_boot_partition
	sudo $$(which sgdisk) -a1 -n0:34:2047  -t0:EF02 -c0:"$(hostName)_ssd_bios"  $(disk_ssd)
	sudo $$(which sgdisk)     -n0:1M:+32M  -t0:EF00 -c0:"$(hostName)_ssd_efi"   $(disk_ssd)
	sudo $$(which sgdisk)     -n0:0:+256M  -t0:8300 -c0:"$(hostName)_ssd_boot"  $(disk_ssd)
	sudo $$(which sgdisk)     -n0:0:+4G    -t0:8200 -c0:"$(hostName)_ssd_swap"  $(disk_ssd)
	sudo $$(which sgdisk)     -n0:0:0      -t0:BF01 -c0:"$(hostName)_ssd_zpool" $(disk_ssd)
	# https://wiki.archlinux.org/index.php/Partitioning#Tricking_old_BIOS_into_booting_from_GPT
	printf '\200\0\0\0\0\0\0\0\0\0\0\0\001\0\0\0' | sudo dd of=$(disk_ssd) bs=1 seek=462
	sudo $$(which sgdisk) --randomize-guids $(disk_ssd)
	sudo $$(which sgdisk) --backup=$(hostName)_ssd.sgdisk $(disk_ssd)

format: umount format-efi format-boot format-zpool
format-efi:
	sudo blkid /dev/disk/by-partlabel/$(hostName)_ssd_efi -t TYPE=vfat || \
	sudo mkfs.vfat -F 16 -s 1 -n EFI /dev/disk/by-partlabel/$(hostName)_ssd_efi
format-boot:
	sudo mkdir -p /mnt/$(hostName)
	sudo blkid -t TYPE=ext2 /dev/disk/by-partlabel/$(hostName)_ssd_boot; test $$? != 2 || \
	sudo mkfs.ext2 /dev/disk/by-partlabel/$(hostName)_ssd_boot
format-zpool:
	sudo zpool list $(zpool) 2>/dev/null || \
	sudo zpool create -o ashift=12 \
	 $(if $(cipher),-O encryption=$(cipher) \
	 -O keyformat=passphrase \
	 -O keylocation=prompt) \
	 -R /mnt/$(hostName) $(zpool) /dev/disk/by-partlabel/$(hostName)_ssd_zpool
	sudo zpool set \
	 autotrim=$(autotrim) \
	 $(zpool)
	sudo zfs set \
	 acltype=off \
	 atime=off \
	 canmount=off \
	 compression=lz4 \
	 dnodesize=auto \
	 relatime=on \
	 xattr=off \
	 mountpoint=/ \
	 $(zpool)
	# https://nixos.wiki/wiki/NixOS_on_ZFS#Reservations
	sudo zfs list $(zpool)/reserved 2>/dev/null || \
	sudo zfs create -o canmount=off -o mountpoint=none $(zpool)/reserved
	sudo zfs set refreservation=$(reservation) $(zpool)/reserved
	# /
	# mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
	sudo zfs list $(zpool)/root 2>/dev/null || \
	sudo zfs create \
	 -o canmount=on \
	 -o mountpoint=legacy \
	 $(zpool)/root
	# /*
	for p in \
	 home \
	 home/Documents \
	 home/Images \
	 home/Videos \
	 nix \
	 var \
	 var/cache \
	 var/log \
	 var/tmp \
	 ; do \
		sudo zfs list $(zpool)/"$$p" 2>/dev/null || \
		sudo zfs create \
		 -o canmount=on \
		 -o mountpoint=legacy \
		 $(zpool)/"$$p" ; \
	 done
	sudo zfs set sync=disabled $(zpool)/var/tmp
	sudo zfs set copies=2 $(zpool)/home/Documents
	sudo zfs set compression=off $(zpool)/home/Images
	sudo zfs set compression=off $(zpool)/home/Videos

mount: mount-zpool mount-boot mount-efi
mount-zpool:
	# scan needed zpools
	sudo zpool list $(zpool) || \
	sudo zpool import -f $(zpool)
	# load encryption key
	sudo zfs get -H encryption $(zpool) | \
	grep -q '^$(zpool)\s*encryption\s*off' || \
	sudo zfs get -H keystatus $(zpool) | \
	grep -q '^$(zpool)\s*keystatus\s*available' || \
	sudo zfs load-key $(zpool)
	# /
	sudo mkdir -p /mnt/$(hostName)
	sudo mountpoint /mnt/$(hostName) || \
	sudo mount -v -t zfs $(zpool)/root /mnt/$(hostName)
	# /*
	for p in \
	 home \
	 nix \
	 var \
	 var/cache \
	 var/log \
	 var/tmp \
	 ; do \
		sudo mkdir -p /mnt/$(hostName)/"$$p"; \
		sudo mountpoint /mnt/$(hostName)/"$$p" || \
		sudo mount -v -t zfs $(zpool)/"$$p" /mnt/$(hostName)/"$$p" ; \
	 done
	sudo chmod 1777 /mnt/$(hostName)/var/tmp
mount-boot:
	sudo mkdir -p /mnt/$(hostName)/boot
	sudo mountpoint /mnt/$(hostName)/boot || \
	sudo mount -v /dev/disk/by-partlabel/$(hostName)_ssd_boot /mnt/$(hostName)/boot
	#sudo mount -v -t zfs bpool/boot /mnt/$(hostName)/boot
mount-efi: | mount-boot
	sudo mkdir -p /mnt/$(hostName)/boot/efi
	sudo mountpoint /mnt/$(hostName)/boot/efi || \
	sudo mount -v /dev/disk/by-partlabel/$(hostName)_ssd_efi /mnt/$(hostName)/boot/efi

bootstrap: mount
	# Workaround https://dev.gnupg.org/T3908
	chmod o+rw $$GPG_TTY $$XAUTHORITY
	
	sudo --preserve-env \
	$$(which nixos-install) \
	 --root /mnt/$(hostName) \
	 --flake '../..#$(hostName)' \
	 --no-root-passwd \
	 --no-channel-copy \
	 --show-trace
	
	# End workaround https://dev.gnupg.org/T3908
	chmod o-rw $$GPG_TTY $$XAUTHORITY

umount:
	for p in \
	 boot/efi \
	 boot \
	 home \
	 nix \
	 var/cache \
	 var/log \
	 var/tmp \
	 var \
	 "" \
	 ; do \
		! sudo mountpoint /mnt/$(hostName)/"$$p" || \
		sudo umount -v /mnt/$(hostName)/"$$p" ; \
	 done
	! sudo zpool list $(zpool) 2>/dev/null || \
	zfs get -H encryption $(zpool) | \
	grep -q '^$(zpool)\s*encryption\s*off' || \
	zfs get -H keystatus $(zpool) | \
	grep -q '^$(zpool)\s*keystatus\s*unavailable' || \
	sudo zfs unload-key $(zpool)
	#! sudo zpool list bpool 2>/dev/null || \
	#sudo zpool export bpool
	! sudo zpool list $(zpool) 2>/dev/null || \
	sudo zpool export $(zpool)