#!/bin/bash
#
# Script to create and format partitions for testing
#  
# (Assumes device size of 24 MiB or larger)

# function to echo and execute
echodo()
{
 echo "$*"
 eval "$*"
}

if [ $# -eq 0 ]; then
  echo "Usage:  init-partitions /path-to-device"
  echo "        Where:  /path-to-device is something like /dev/sde"
  echo " "
  echo "WARNING!  This command will erase the entire disk device!"
  exit 1
fi

if [ ! -b $1 ]; then
  echo "ERROR:  \"$1\" is not a block special file (e.g., device file)."
  exit 2
fi

DEV=$1

# Create MSDOS partition table
echo "*** Create MSDOS partition table ***"
echodo "sudo parted $DEV mklabel msdos yes"

# Create and format primary partitions
echo "*** Create and format primary partitions ***"
echodo "sudo parted $DEV mkpart primary ntfs 1mib 3mib"
echodo "sudo mkntfs -Q -v -L 'NTFS - One' ${DEV}1"

echodo "sudo parted $DEV mkpart primary ext2 3mib 5mib"
echodo "sudo mkfs.ext2 -L 'EXT2 - Two' ${DEV}2"

echodo "sudo parted $DEV mkpart primary ntfs 5ib 8mib"
echodo "sudo mkntfs -Q -v -L 'NTFS - Three' ${DEV}3"

# Create extended partition
echo "*** Create extended partition ***"
echodo "sudo parted $DEV mkpart extended 8mib 100%"

# Create and format logical partitions
echo "*** Create and format logical partitions ***"
echodo "sudo parted $DEV mkpart logical ntfs 9ib 12mib"
echodo "sudo mkntfs -Q -v -L 'NTFS - Five' ${DEV}5"

echodo "sudo parted $DEV mkpart logical ext2 12mib 15mib ignore"
echodo "sudo mkfs.ext2 -L 'EXT2 - Six' ${DEV}6"

echodo "sudo parted $DEV mkpart logical ntfs 15ib 17mib ignore"
echodo "sudo mkntfs -Q -v -L 'NTFS - Seven' ${DEV}7"
