#!/bin/sh -e

_dev="$1"

[ -z $_dev ] && printf "usage: %s <sd2>\n" "$(basename $0)" && exit 2
[ $(id -u) -gt  0 ] && printf "you need superuser rights\n" && exit 2

dmesg | grep "$_dev" | grep -A1 scsibus | tail -n 2

printf "Format ${_dev} [y/N]? "
read
case $REPLY in
    [yY]) ;;
    *) exit 0; ;;
esac

printf "Overwriting first MBs with zeros:"
dd of=/dev/r${_dev}c if=/dev/zero bs=1M count=1 > /dev/null 2>&1
printf " ✅\n"

printf "Creating exFAT / NTFS partition:"
echo "edit 0\n07\n\n2048\n*\nw\nq\n" | fdisk -e "$_dev" > /dev/null 2>&1
printf " ✅\n"

printf "Creating exFAT file system (this may take a while)...\n"
mkexfatfs -s 2048 "/dev/${_dev}i"
printf "Creating exFAT file system: ✅\n"

printf "Mount:\ndoas mount.exfat /dev/%si /mnt\n" "$_dev"
