#!/bin/sh

if [ "$#" -ne 1 ]; then
    echo "Usage: flash_usb.sh <target disk>"
    echo ""
    echo "E.g. flash_usb.sh /dev/sde"
    exit 1
fi

set -xe

IMGFILE="byod_usb.img"
TARGET="$1"

# oflag=direct bypasses the disk cache
# conv=fsync makes sure the disk is synced at the end
sudo dd if=$IMGFILE of="$TARGET" bs=4M oflag=direct conv=fsync status=progress

# move backup GPT to end of disk, fix up GPT size etc
sudo sgdisk -e "$TARGET"
# generate new unique GUIDs
sudo sgdisk -G "$TARGET"
# verify GPT integrity
sudo sgdisk -v "$TARGET"
