#!/bin/sh
#
# Resize a partition using parted
#
# Author: Eko M. Budi for Vector Linux
# License : GNU GPL

BACKTITLE="Vector Linux Parted Magic"
vdir=$(dirname $0)
. $vdir/setup-functions

# Get settings from argument
HD_SWAP=$1
HD_ROOT_MIN=$2
HD_ROOT_MAX=$3
HD_HOME=$3

if [ -z $HD_SWAP ] && [ -r $SETUP_CONF ]; then
    . $SETUP_CONF
fi

SPACE_MIN=`expr $HD_SWAP '+' $HD_ROOT_MIN '+' $HD_HOME`
SPACE_MAX=`expr $HD_SWAP '+' $HD_ROOT_MAX '+' $HD_HOME`

get_size()
{
    if (mount $1 /mnt/loop -t auto) >/dev/null 2>&1; then
	LINE3=`df | grep $1`
	SIZE=`echo $LINE3 | cut -f 2 -d ' '`
	UNUSED=`echo $LINE3 | cut -f 4 -d ' '`
	SIZE=`expr $SIZE '/' 1024`
	UNUSED=`expr $UNUSED '/' 1024`
	sumount /mnt/loop
    fi
}

build_part_menu()
{
    sumount /mnt/loop > /dev/null 2>&1
    
    probedisk | grep "|disk|" | while read LINE; do
	MAJOR=`echo $LINE | cut -f 1 -d '|'`
	for TYPE in FAT ext2; do
	    parted --script $MAJOR print | grep $TYPE | while read LINE2; do
		MINOR=`echo $LINE2 | cut -f 1 -d ' '`
		# Try to mount it, and check the free size
		DEV=$MAJOR$MINOR
		if get_size $DEV; then
		    echo "'$DEV' 'Type=$TYPE Size=$SIZE Unused=$UNUSED' \\">>/tmp/hdlist
		fi
	    done
	done
    done
}

infobox "Collecting existing partitions ..."
rm -f /tmp/hdlist
build_part_menu
if [ ! -f /tmp/hdlist ]; then
    msgbox "Sorry, cannot find FAT or EXT2 partition" "ERROR"
    clean_exit 0
fi

menu_part()
{

TEXT="\n
Welcome to the parted partition re-sizer. This little
utility will guide you through resizing one of the existing 
partitions on your harddisk to make room for Vector Linux 
(comprises a swap, root and home partition). The required
space is around $SPACE_MIN - $SPACE_MAX MB.\n
Here are your existing partitions.
Please choose the one with enough unused space:"

echo '$DCMD --backtitle "$BACKTITLE" --title "PARTITION RESIZING" \
--menu "$TEXT" 20 64 6 \' > $fmenu
cat /tmp/hdlist >> $fmenu
echo '2> $freply' >> $fmenu

. $fmenu

result=$?
[ $result != 0 ] && return $result

PARTITION=`cat $freply`
return 0
}

calculate_size()
{
    MAJOR=`echo $PARTITION | cut -b1-8`
    MINOR=`echo $PARTITION | cut -b9-`
    field=`parted --script $MAJOR print | grep -e "^$MINOR `
    START=`echo $field | cut -d ' ' -f2 | cut -d '.' -f1`
    end=`echo $field | cut -d ' ' -f3 | cut -d '.' -f1`
    NEWEND=`expr $end - $FREE_SPACE`
}

parted_resize()
{
    clear
    echo -e "${YELLOW}HERE WE GO .............. !!!${OFF}" 
    echo "Press Ctrl-Break to cancel"
    sleep 1
    echo "$(parted -v) will be started in 5 seconds ..."
    echo "Press Ctrl-Break to cancel"
    for II in 5 4 3 2 1 0; do
      echo -n "$II "
      sleep 1
    done
    echo
    echo "It is too late to stop it now"
    echo "Please wait and pray ..."
    parted --script $MAJOR resize $MINOR $START $NEWEND quit
}

menu_size()
{
    get_size $PARTITION

while [ 1 ]; do
TEXT="\n
Alright, you chose $PARTITION to be resized.
I have to warn you that this is a dangerous operation and
might damage the partition ! You better have a backup of
the important data in it.\n
If absolutely sure, please tell me the free 
space you want to reclaim ($UNUSED MB maximum):"

    $WCMD --backtitle "$BACKTITLE" --title "DEFINE FREE SPACE" \
    --inputbox "$TEXT" 16 60 2> $freply

    result=$?
    [ $result != 0 ] && return $result

    FREE_SPACE=`cat $freply`

    if [ $FREE_SPACE -lt $UNUSED ]; then
	break
    fi
    msgbox "The free space you are asking is too big" "RETRY"
done

    calculate_size

    TEXT="\n
I'm going to execute this danger danger danger command:\n
  parted $MAJOR resize $MINOR $START $NEWEND\n\n
If you are about to pass out from the stress, hit <No>\n
grab a nap and return later.\n
Otherwise, take a big deep breath and say <Yes>."

    $DCMD --backtitle "$BACKTITLE" --title "THE CRUCIAL MOMENT" \
    --yesno "$TEXT" 12 64 2> /dev/null
    
    result=$?
    if [ $result != 0 ]; then
	clear
	sleep 3
	echo -e "${GREEN}Chicken !!! ${OFF}"
	sleep 3
	clean_exit 0
    fi
    
    parted_resize
    
    if [  $? != 0 ]; then
	echo -e "${RED}Resizing has failed !!! $OFF"
	sleep 5
	errorbox "I'm really sorry. I hope your harddisk is still fine." "RESIZING ERROR"
	clean_exit 0
    fi
    echo -e "${GREEN}OK freespace is done....$OFF"
    sleep 3
    
TEXT="\n
Well now that wasn't so hard was it. Your new free space is in place.
If you saw a warning that the kernel was unable to re-read the 
partition table, REBOOT first. You can continue with the FDISK later.
Going straight to the FDISK is actually not recommended, but some
geeks told me that it is okay.\n
So up to you, what is next ?"

    $WCMD --backtitle "$BACKTITLE" --title "WE HAVE SURVIVED" \
    --menu "$TEXT" 16 74 3 \
"REBOOT" "reboot the system please. Safety first." \
"FDISK" "go straight to FDISK utility at my own risk !" \
"RETURN" "enough with this horror. Take me back to the main menu." \
2> $freply
   
    status=$?
    [ $status != 0 ] && return $status

    choice=$(cat $freply)
   
    dbug choice=$choice
    case $choice in
       "FDISK") 
    	    exec $vdir/setup-fdisk
       ;;
       "REBOOT") 
	    reboot_exit
	    ;;
	*)
	    infobox "As you wish ..., back to the main menu"
	    sleep 2
	    clean_exit 0
	    ;;
    esac
    return 0
}

wizard menu_part menu_size menu_resize
clean_exit $?
