#!/bin/sh
#
if [ "$1" = "-t" -a -n "$2" ]; then

if ! echo "$2" | grep -q "[a-z]$" ; then
    echo "Not a proper device \`$2'!"
    exit
elif [ ! -b $2 ]; then
    echo "Can't find device \`$2'!"
    exit
fi

echo -e "l\nq\n" | fdisk $2 2>/dev/null | sed "/Command (m for help):/d" | sed "/^$/d"

elif [ -n "$1" -a -n "$2" -a -n "$3" ]; then

if [ ! -x "`type -path fdisk`" ]; then
    echo "Can't find \`fdisk'!"
    exit
elif [ "$2" = "all" ]; then
    all=yes
elif ! echo "$2" | grep "^[1-9]" | grep -q "[0-9]$" ; then
    echo "Not a proper number \`$2'!"
    exit
fi

if ! echo "$3" | grep -q "[a-z]$" ; then
    echo "Not a proper device \`$3'!"
    exit
elif [ ! -b $3 ]; then
    echo "Can't find device \`$3'!"
    exit
fi

if ! echo -e "l\nq\n" | fdisk $3 2>/dev/null | sed "/Command (m for help):/d" | sed "/^$/d" | grep -q -w "$1" ; then
    echo "Not fdisk partition type \`$1'!"
    exit
fi

if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -gt "2" ]; then
    echo "You have more than 2 partitions on $3, I don't know how to handle that!"
    exit
fi

for p in 1 2 3 ; do
if ! fdisk -l $3 | grep -q "^$3"$p"" ; then 
  pt=$p
  break
fi
done

    if grep -q "^$3" /proc/mounts ; then 
      for p in `grep "^$3" /proc/mounts | awk '{print $1}'`
      do
        umount $p 2>/dev/null || exit
      done 
    fi

echo "****** Running fdisk on \`$3' in test mode! ******"

if [ "$all" = "yes" ]; then
if [ "$4" = "-a" ]; then
if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "n\np\n$pt\n\n\nt\n$1\na\n$pt\nq\n" | fdisk $3 1>/dev/null 2>/dev/null || fdisk_error=yes
else
  echo -e "n\np\n$pt\n\n\nt\n$pt\n$1\na\n$pt\nq\n" | fdisk $3 1>/dev/null 2>/dev/null || fdisk_error=yes
fi
else
if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "n\np\n$pt\n\n\nt\n$1\nq\n" | fdisk $3 1>/dev/null 2>/dev/null || fdisk_error=yes
else
  echo -e "n\np\n$pt\n\n\nt\n$pt\n$1\nq\n" | fdisk $3 1>/dev/null 2>/dev/null || fdisk_error=yes
fi
fi
else
if [ "$4" = "-a" ]; then
if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "n\np\n$pt\n\n+$2"M"\nt\n$1\na\n$pt\nq\n" | fdisk $3 1>/dev/null 2>/dev/null || fdisk_error=yes
else
  echo -e "n\np\n$pt\n\n+$2"M"\nt\n$pt\n$1\na\n$pt\nq\n" | fdisk $3 1>/dev/null 2>/dev/null || fdisk_error=yes
fi
else
if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "n\np\n$pt\n\n+$2"M"\nt\n$1\nq\n" | fdisk $3 1>/dev/null 2>/dev/null || fdisk_error=yes
else
  echo -e "n\np\n$pt\n\n+$2"M"\nt\n$pt\n$1\nq\n" | fdisk $3 1>/dev/null 2>/dev/null || fdisk_error=yes
fi
fi
fi

if [ "$fdisk_error" = "yes" ]; then
echo "
There was an fdisk ERROR! 
I won't continue!
"
exit
fi

echo "****** Backing up MBR from \`$3' to \`/tmp/mbr.$3'! ******"
dd if=$3 of=/tmp/mbr.$3 bs=512 count=1 2>/tmp/dd_error || dd_error=yes

if [ "$dd_error" = "yes" ]; then
echo
cat /tmp/dd_error
echo "
There was an ERROR trying to backup the MBR! 
I won't continue!
"
exit
fi

fdisk -l $3 | sed -n '2,$p' >/tmp/fdisk.old

echo "****** Running fdisk on \`$3' for real! ******"

if [ "$all" = "yes" ]; then
if [ "$4" = "-a" ]; then
if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "n\np\n$pt\n\n\nt\n$1\na\n$pt\nw\n" | fdisk $3 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
else
  echo -e "n\np\n$pt\n\n\nt\n$pt\n$1\na\n$pt\nw\n" | fdisk $3 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
fi
else
if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "n\np\n$pt\n\n\nt\n$1\nw\n" | fdisk $3 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
else
  echo -e "n\np\n$pt\n\n\nt\n$pt\n$1\nw\n" | fdisk $3 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
fi
fi
else
if [ "$4" = "-a" ]; then
if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "n\np\n$pt\n\n+$2"M"\nt\n$1\na\n$pt\nw\n" | fdisk $3 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
else
  echo -e "n\np\n$pt\n\n+$2"M"\nt\n$pt\n$1\na\n$pt\nw\n" | fdisk $3 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
fi
else
if [ "`fdisk -l $3 | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "n\np\n$pt\n\n+$2"M"\nt\n$1\nw\n" | fdisk $3 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
else
  echo -e "n\np\n$pt\n\n+$2"M"\nt\n$pt\n$1\nw\n" | fdisk $3 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
fi
fi
fi

if [ "$fdisk_error" = "yes" ]; then
if ! grep -q "Re-reading the partition table failed" /tmp/fdisk_error ; then 
   dd if=/tmp/mbr.$3 of=$3 bs=512 count=1 2>/dev/null
   partprobe $3 2>/dev/null
echo "
There was an fdisk ERROR!

I restored the MBR from the backup I made before I ran fdisk!
"
   exit
else
   partprobe $3 2>/dev/null
   echo
   tail -n 4 /tmp/fdisk_error
fi
fi

echo "
*** OLD partition layout:"
cat /tmp/fdisk.old
echo "
*** NEW partition layout:"
fdisk -l $3 | sed -n '6,$p'

echo "
Done!"

else
echo "---------------------------------------------------------------------------
This example would create a type 83 (Linux) 20000 MegaBytes (20GB) primary
partition on /dev/hda!  It will use the next available partition!

   # mkpart 83 20000 /dev/hda  <Specify size of partition!>

   # mkpart 83 all /dev/hda    <Or, use 'all' of partition!>

If you want it made active, add '-a' at the end!

   # mkpart 83 20000 /dev/hda -a

This script won't run if you have more than 2 partitions on the specified
hard drive!

To get a listing of partition types, do this!
   
   # mkpart -t /dev/hda
---------------------------------------------------------------------------"
fi
