#!/bin/bash
set -e

dev="$1"

[ -z "$dev" ] && {
	echo need arg 1: sda/sdb/sdc/...
	exit 1
}

# locate device in /sys/block
pdev="$dev"
blkpath="/sys/block/$dev"
while [ ! -e "$blkpath" ]
do
	pdev="${pdev%?}"
	blkpath="/sys/block/$pdev/$dev"
done
[ -z "$pdev" ] && {
	echo bug
	exit 1
}

# list all aliases for this disk
echo
find /dev/disk/by-id -printf '%f %l\n' |
awk -v v="$pdev" '$0~"/"v"$" {print $1}'

# and lsblk info
echo
lsblk | grep -E "NAME|($dev|$pdev)"

# and blkid info
echo
blkid | grep -E "/dev/($dev|$pdev)"

echo
printf 'ok? press enter:'
read -u1 -r

cryptsetup --type=luks2 -c aes-xts-essiv:sha256 -h sha512 -s 512 --pbkdf=argon2id --pbkdf-force-iterations=50 --pbkdf-memory=1048576 --pbkdf-parallel=4 luksFormat /dev/$dev

printf '\n\033[36mpartprobe...\033[0m\n'
partprobe /dev/$dev || true

printf '\n\033[36muopen...\033[0m\n'
uopen.sh
