#!/bin/sh
PATH=$PATH:/usr/local/bin
export PATH
clear

free() {
pct=0
if [ ! -d /mnt/loop ]; then
mkdir -p /mnt/loop
fi
mount $dev$num /mnt/loop
df | grep /mnt/loop > /tmp/amount.tmp
amount=`cat /tmp/amount.tmp`
for i in $amount; do
pct=`expr $pct + 1`
if [ $pct = 4 ]; then
amount1=$i
break
fi
done
umount /mnt/loop
rm -f /tmp/amount.tmp
total=`expr $amount1 / 1024`
echo " You have $total (megabytes) of free space on partition $num."
echo -n " Are you sure this is the right partition ? [y/n] "
read ans
if [ ! "$ans" = "y" -o "$ans" = "Y" ]; then
echo " OK then we start again........"
sleep 2
main
fi
}

main() {
probedisk > /tmp/probe.tmp
probe=`cat /tmp/probe.tmp | grep disk | cut -f 1 -d '|'`
rm -f /tmp/probe.tmp
dialog --title "AVAILABLE HARD DRIVES" \
 --inputbox "Hard drives in linux are referred to as devices and
 assigned letters for each drive. Ide style drives are referred to as
 'HD' and scsi style drives are called 'SD'. Using that logic then your
 first IDE style drive would be called /dev/hda the second /dev/hdb and
 so on. SCSI style drives work the same, the first SCSI drive would be
 /dev/sda the second /dev/sdb etc.
 Here are the available hard disks on your system as referenced
 above.\n\
 $probe \n\
 Please enter the one you want for partitioning in the box below.\
 Input the entire field i.e. /dev/hda." 2>/tmp/dev 16 74
 case $? in
 0)
 dev=`cat /tmp/dev`
 rm -f /tmp/dev
 ;;
 1)
 exit 1
 ;;
 255)
 exit 1
 ;;
 esac
 
dialog --title "PARTTION LIST" --msgbox "OK now we are going to list all
of the partitions on $dev. 
Then we will ask you which partition you wish to resize (1,2,3, etc).
The starting point and the ending point of the partition. Not to worry
tho as all the information required will be displayed on the screen.
You just need to make a few choices and enter the correct response to the
program questions. OK ready to begin ?" 12 78

case $? in

0)
clear
parted --script $dev print > /tmp/parted.tmp quit
;;
255)
echo " escape key pressed exiting the program......."
exit 1
;;
esac
echo "Part#   Start Pt   End Pt      Kind"
echo "-----------------------------------------------"
cat /tmp/parted.tmp | grep "primary" | grep "FAT" > /tmp/main.tmp
cat /tmp/parted.tmp | grep "logical" | grep "FAT" >> /tmp/main.tmp
cat /tmp/parted.tmp | grep "primary" | grep "ext2" >> /tmp/main.tmp
cat /tmp/parted.tmp | grep "logical" | grep "ext2" >> /tmp/main.tmp
cat /tmp/main.tmp
echo
# starting the main program
echo "---------OK LETS START------------"
echo
echo "From the above table enter a partition # from $dev to resize."
echo "The partition numbers are listed under the heading 'Part#' (1,2,3) "
echo -n "Which partition do you want to resize ? : "
read num
echo
free
clear
echo
field=`grep ^$num /tmp/main.tmp`
start=`echo $field | cut -d ' ' -f2 | cut -d '.' -f1`
end=`echo $field | cut -d ' ' -f3 | cut -d '.' -f1`
ptype=`echo $field | cut -d ' ' -f4`
rm -f /tmp/main.tmp


clear
dialog --title "JUST ABOUT DONE" --msgbox "Alrighty we are just about
done. We have a few more simple questions to ask then we will make the
new partition(s). Take a big deep breath and press enter to continue.
If your about to pass out from the stress hit escape grab a nap and
return later" 12 74

case $? in
0)
clear
;;
255)
echo " escape key pressed exiting the program......."
exit 1
;;
esac
dialog --title "NEW PARTITION SIZE" --inputbox "Partition $num on hard
drive $dev has $total (megabytes) of free space. Now you need to tell me
how big of a partition you want me to make. Enter in the box below the
size in megabytes you want. Remember a 1000 megabytes equals 1 gigabyte
so if you have a 20 gig hard drive and want 2 gigs for linux enter 2000.
If you only want 500 megs then enter 500.
Enough of the math lessons already. How many megabytes do you want?" 14 76 2> /tmp/psize
case $? in
0)
clear
psize=`cat /tmp/psize`
rm -f /tmp/psize
;;
1)
exit 1
;;
255)
exit 1
;;
esac
dialog --title "Install Swap Space" --yesno "You now have the option to
make a Linux swap partition. If you already have a swap partition setup
then this is not neccesary unless you want multiple swap partitions which
linux can use. If This is your first installation and you don't have a
swap partition then this step is highly recommended. Enter <YES> if you would
like a swap partition made. Entering <NO> will not make a swap partition only
a linux one. Please enter <yes> or <NO>." 0 0

case $? in
0)
SWAP="on"
;;
1)
SWAP="off"
;;
255)
exit
;;
esac 
if [ $SWAP = "off" ]; then
swap=0
fi

if [ $SWAP = "on" ]; then
dialog --title "SWAP SPACE" --inputbox "Linux requires the use of a swap
partition. Please enter the amount of swap space you would
like. The general rule of thumb is 2 x your physical ram or 128 megs
whichever comes first. If in doubt just enter 100 which should be
plenty for most situations." 14 76 2> /tmp/swap
case $? in
0)
clear
swap=`cat /tmp/swap`
rm -f /tmp/swap
;;
1)
exit 1
;;
255)
exit 1
;;
esac
fi
sum=`expr $psize + $swap`

dialog --title "THE CRUCIAL MOMENT" --yesno "Here is what we have so
far. You are resizing partition $num and allowing for a linux
partition $psize megabytes in size. The swap partition will be $swap
megabytes. The total megabytes used will be $sum. 
If you are happy with this configuration then select yes if
not select no an we will try again." 12 74
case $? in
0)
clear
if [ $sum -gt $total ]; then
echo " OOPS !!....the partition size you selected is greater than the space"
echo "available on the partition."
echo "If you wish to start again enter yes here, no will exit the program."
echo -n "Do you wish to start over? [y/n] "
read ans
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
main
else
exit
fi
fi
;;
1)
main
;;
255)
exit 1
;;
esac
dialog --title "LAST CHANCE" --yesno "OK last chance to quit. If you say
yes now the new partition will be written.
Shall I proceed ?" 8 74
case $? in
0)
clear
;;
1)
exit 1
;;
255)
exit 1
;;
esac
 
ptotal=`expr $swap + $psize`
newend=`expr $end - $ptotal`
end1=`expr $end - $swap`
parted --script $dev resize $num $start $newend quit > /dev/null
echo "OK freespace is done...."
parted --script $dev mkpartfs $ptype ext2 $newend $end1 quit > /dev/null
echo "The new linux partition is made......"
if [ $SWAP = "on" ]; then
parted --script $dev mkpartfs $ptype linux-swap $end1 $end quit > /dev/null
echo "The swap partition has been made......"
fi
sleep 3

dialog --title "PARTITIONING COMPLETE" \
 --infobox "Well now that wasn't so hard was it.
Your new partitions are in place so you can continue the installation.
First you need to reboot the computer so all the changes can take
effect. As soon as this message clears the system will reboot the 
computer and you can restart the installation." 12 74
sleep 6

} 

dialog --backtitle "PARTED MAGIC vectorlinux 2001" --title "PARTED PARTITION RESIZER" --msgbox "Welcome to the parted partition resizer. This little
script will help guide you through resizing one or more existing
partitions on your machine to make room for a Linux and Linux swap
partition. If you wish to cancel this operation at any time use the
cntrl + c keys to do so. Press enter when you are ready to continue or
escape to quit now." 12 74

case $? in
0)
main
;;
255)
echo "chicken !!"
exit 1
;;
esac
