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

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

ntfs_hd="`echo $2 | cut -b 1-8`"
ntfs_pt="`echo $2 | cut -b 9`"

if [ ! -b $ntfs_hd ]; then
    echo "Can't find device \`$ntfs_hd'!"
    exit
fi

if ! fdisk -l $ntfs_hd | grep "^$2" | grep -q -E "NTFS|SFS" ; then
    echo "The partition \`$2' is not type 7 (NTFS)!"
    exit
elif [ "`fdisk -l $ntfs_hd | grep "^/dev/" | wc -l`" -gt "3" ]; then
    echo "You have more than 3 partitions on $ntfs_hd, I don't know how to handle that!"
    exit
elif mount 2>/dev/null | grep " on / " | grep -q "^$ntfs_hd" ; then
    echo "Run this script from CD etc. in memory!"
    exit
elif mount 2>/dev/null | grep " on " | grep -q "^$ntfs_hd" ; then
    echo "There's a partition on \`$ntfs_hd' being used!"
    mount 2>/dev/null | grep " on " | grep "^$ntfs_hd"
    echo "Unmount any mounted partitions on \`$ntfs_hd'!"
    exit
fi

echo "****** Running ntfsresize on \`$2' in test mode! ******"
ntfsresize -n -s $1"M" $2 || exit

active=yes
if [ "`fdisk -l $ntfs_hd | grep "^/dev/" | wc -l`" -gt "1" ]; then
if ! fdisk -l $ntfs_hd | grep "^$2" | grep -q " \* " ; then
if fdisk -l $ntfs_hd | grep "^/dev/" | grep -q " \* " ; then
  active=no
fi
fi
fi

echo "****** Running fdisk on \`$ntfs_hd' in test mode! ******"
if [ "$active" = "yes" ]; then
if [ "`fdisk -l $ntfs_hd | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "d\n$ntfs_pt\nn\np\n$ntfs_pt\n\n+$1"M"\nt\n7\na\n$ntfs_pt\nq\n" | fdisk $ntfs_hd 1>/dev/null 2>/dev/null || fdisk_error=yes
else
  echo -e "d\n$ntfs_pt\nn\np\n$ntfs_pt\n\n+$1"M"\nt\n$ntfs_pt\n7\na\n$ntfs_pt\nq\n" | fdisk $ntfs_hd 1>/dev/null 2>/dev/null || fdisk_error=yes
fi
else
if [ "`fdisk -l $ntfs_hd | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "d\n$ntfs_pt\nn\np\n$ntfs_pt\n\n+$1"M"\nt\n7\nq\n" | fdisk $ntfs_hd 1>/dev/null 2>/dev/null || fdisk_error=yes
else
  echo -e "d\n$ntfs_pt\nn\np\n$ntfs_pt\n\n+$1"M"\nt\n$ntfs_pt\n7\nq\n" | fdisk $ntfs_hd 1>/dev/null 2>/dev/null || fdisk_error=yes
fi
fi

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

echo "****** Backing up MBR from \`$ntfs_hd' to \`/tmp/mbr.`echo $ntfs_hd | cut -b 6-8`'! ******"
dd if=$ntfs_hd of=/tmp/mbr.`echo $ntfs_hd | cut -b 6-8` 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

echo "****** Running ntfsresize on \`$2' for real! ******"
echo "*** If everything is alright, answer 'y' when asked to proceed."
ntfsresize -s $1"M" $2 || exit

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

echo "****** Running fdisk on \`$ntfs_hd' for real! ******"
if [ "$active" = "yes" ]; then
if [ "`fdisk -l $ntfs_hd | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "d\n$ntfs_pt\nn\np\n$ntfs_pt\n\n+$1"M"\nt\n7\na\n$ntfs_pt\nw\n" | fdisk $ntfs_hd 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
else
  echo -e "d\n$ntfs_pt\nn\np\n$ntfs_pt\n\n+$1"M"\nt\n$ntfs_pt\n7\na\n$ntfs_pt\nw\n" | fdisk $ntfs_hd 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
fi
else
if [ "`fdisk -l $ntfs_hd | grep "^/dev/" | wc -l`" -eq "1" ]; then
  echo -e "d\n$ntfs_pt\nn\np\n$ntfs_pt\n\n+$1"M"\nt\n7\nw\n" | fdisk $ntfs_hd 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
else
  echo -e "d\n$ntfs_pt\nn\np\n$ntfs_pt\n\n+$1"M"\nt\n$ntfs_pt\n7\nw\n" | fdisk $ntfs_hd 1>/tmp/fdisk_error 2>/dev/null || fdisk_error=yes
fi
fi

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

I restored the MBR from the backup I made before I ran fdisk!

You should now run nsfsresize to restore the original size of the filesystem!
ntfsresize -f $2
"
   exit
else
      partprobe $ntfs_hd 2>/dev/null
      sleep 1
      rm -f /tmp/pt
      dd if=$2 of=/tmp/pt bs=512 count=1 1>/dev/null 2>/dev/null

    if ! grep -q -E "BOOTMGR|NTLDR" /tmp/pt ; then
      dd if=/tmp/mbr.`echo $ntfs_hd | cut -b 6-8` of=$ntfs_hd bs=512 count=1 2>/dev/null
      partprobe $ntfs_hd 2>/dev/null
      sleep 1
      echo y | ntfsresize -f $2
      echo "----------------------------------------------------------------"
      echo "*** ERROR: There was a problem, I restored the original layout!"
      echo "***        The next time you boot Windows, it will run chkdsk."
      echo "----------------------------------------------------------------"
      exit
    else
      partprobe $ntfs_hd 2>/dev/null
      sleep 1
      echo
      tail -n 4 /tmp/fdisk_error
    fi
fi
fi

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

echo "
The next time you boot Windows it will run 'chkdsk'!
Done!"

else
echo "---------------------------------------------------------------------------
Shrink Windows XP/Vista etc. NTFS filesystem!

   # shrinkntfs 80000 /dev/hda1

The above example would shrink an NTFS filesystem on /dev/hda1 to 80000
MegaBytes (80GB)!

After the NTFS filesystem is strunk by ntfsresize, fdisk is used to rewrite
the partition table.

This script is specifically for the case where you have an NTFS system
using the whole hard drive and want to free up space for another
operating system.

You can use 'mkpart' to create a Linux etc. partition!

This script won't run if you have more than 3 partitions on the specified
hard drive!
---------------------------------------------------------------------------"
fi
