#!/bin/sh
if [ $# != 2 ]; then
 echo "Usage: $0 /dev/usb-drive LABEL" >&2
 exit 1
fi

if [ $(id -u) != 0 ]; then
 echo "Error: You must be root to run this script!" >&2
 exit 2
fi

TARGET="$1"
LABEL="$2"

[ -z "$LABEL" ] && LABEL="USBSTICK"

REM=$(cat /sys/block/${TARGET#/dev/}/removable 2>/dev/null)

if [ "$REM" != "1" ]; then
 echo "Error: No removeable drive $TARGET" >&2
 exit 4
fi

OPTIONS="umask=000,shortname=mixed,quiet,utf8"

SFDISK=/sbin/sfdisk
MKDOSFS=/sbin/mkdosfs

dd if=/dev/zero of=$TARGET bs=1M count=1

#$SFDISK -D -uM $TARGET <<EOT
#,,b,*
#EOT

$SFDISK -f -L -uS $TARGET <<EOT
2048,,b,*
EOT

$MKDOSFS -F32 -n$LABEL ${TARGET}1
