#!/bin/sh
# vcfdisk
# Launch cfdisk to edit the harddisk
#

vdir=$(dirname $0)
. $vdir/vasm-functions

check_root

build_disk_menu()
{
   dbug "HD detection"
   probedisk | grep -v "|cdrom|" | while read line; do
       disk=$( echo $line | cut -f 1 -d '|')
       desc=$( echo $line | cut -f 3 -d '|')
       echo "$disk" "'$desc' \\" >> $fmenu
   done
}

###########################################
# Hints and choose partitions
menuA()
{
TITLE="HARDDISK PARTITION"
DIMENSION="16 70 4"
TEXT="\n
We are going to launch cfdisk hard disk utility. It allows you
to create, delete, as well as change partition type. A hint,
do not forget to WRITE your changes before you QUIT.\n
Select the harddisk then hit <OK>:"

   echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
   echo '--menu "$TEXT" $DIMENSION \' >> $fmenu
   build_disk_menu 
   if [ $? != 0 ]; then
      errorbox "Can not find any harddisk. Check your hardware." "FATAL ERROR"
      clean_exit 
   fi
   echo '2> $freply' >> $fmenu
   
   . $fmenu
   
   status=$?
   [ $status != 0 ] && return $status

   FDISK_DEV=$(cat $freply)
   dbug "launching fdisk $FDISK_DEV"

   $CMD cfdisk $FDISK_DEV
   return 0  
}

###########################################
# REBOOT,  restart
menuB()
{
TITLE="FDISK FINISHED"
TEXT="\n
I wish you have the good partitions now. In that case choose REBOOT
as the system needs to reboot for the changes to take place.
But if you made an error during the partition program and 
canceled or exited then choose 'RETURN'. Choose EXIT only 
if you did not make any modification and want to continue.\n
The choice is yours:"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
"REBOOT" "Reboot the system to add the new partitions." \
"RETURN" "Return to the partition program. I want to try again." \
"EXIT"   "Don't know how I got here but I want out." \
    2> $freply
   
    status=$?
    [ $status != 0 ] && return $status

    choice=$(cat $freply)
   
    dbug choice=$choice
    case $choice in
       "RETURN") 
    	    return 3
       ;;
       "REBOOT") 
	    clear
	    echo "Rebooting the system in 3 seconds....................."
	    sleep 2
	    reboot
	    clean_exit 0
	    ;;
	"EXIT")
	    clean_exit 0
	    ;;
    esac
    return 0
}

#############################################################
# Main Program
wizard menuA menuB
clean_exit $?

