#!/bin/sh
# setup-cfdisk
# Launch cfdisk to edit the harddisk
#
# Author: Eko M. Budi for Vector Linux
# License : GNU GPL

BACKTITLE="Vector Linux: Hard Disk Partitioning" 
vdir=$(dirname $0)
. $vdir/setup-functions

build_disk_menu()
{
   dbug "HD detection"
   fdisk -l | grep -e "^Disk " | while read line; do
       disk=$( echo $line | cut -f 1 -d ':' | cut -f 2 -d ' ') 
       desc=$( echo $line | cut -f 2 -d ':')
       echo "$disk" "'$desc' \\"
   done
}

###########################################
###########################################
# REBOOT,  restart
menu()
{
TITLE="ZERO PARTITION TABLE?"
TEXT="\n
Would you like to start cfdisk with a zero partition table?\n
If you have a new hard drive then you will need to start with an empty table.
or if you have partitions you want to ignore this is a good idea also. 
If you have partition you want to keep then just choose No here.\n
Choose wisely:"
DIMENSION="18 76 3"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
"ZERO" "start cfdisk with a zero partition table." \
"NO" "start cfdisk with the existing partition table." \
    2> $freply
   
    status=$?
    [ $status != 0 ] && return $status

    choice=$(cat $freply)
   
    dbug choice=$choice
    case $choice in
       "ZERO") 
    	    ZERO="-z"
       ;;
       "NO") 
	    ZERO=" "
	    ;;
	*)
	    ZERO=" "
	    ;;
    esac
    return 0
}


# Hints and choose partitions
menuA()
{
TITLE="HARDDISK PARTITIONING"
DIMENSION="20 76 2"
TEXT="\n
This program allows you to make swap, root and home partitions using cfdisk.
Before starting, here is a brief how-to:\n
  - If necessary, use the [Delete] menu to free some space.\n
  - Select some free space then use the [New] menu to create\n
    the required Linux partitions (type 83).\n
  - Select a partition, then use the [Type] menu to change it as\n
    Linux-swap (type 82).\n
  - To finish, use [Write] menu then [Quit].\n
    Do not forget to [Write] unless you want to discard the changes !\n
That's it. Now select your harddisk then hit <OK>:"

   echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
   echo '--menu "$TEXT" $DIMENSION \' >> $fmenu
   build_disk_menu >> $fmenu
   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 cfdisk $FDISK_DEV"

   cfdisk $ZERO $FDISK_DEV
   return 0  
}

###########################################
# REBOOT,  restart
menuB()
{
TITLE="DISK PARTITIONING IS DONE"
TEXT="\n
Hopefully you have the partitions setup 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 'RETRY'. Choose RETURN only 
if you did not make any modification and want to continue the 
installation.\n
The choice is yours:"
DIMENSION="18 76 3"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
"REBOOT" "Reboot the system to add the new partitions." \
"RETRY" "Return to the partition program. I want to try again." \
"RETURN" "Return to setup (Not recommended if you have windows partitions)" \
    2> $freply
   
    status=$?
    [ $status != 0 ] && return $status

    choice=$(cat $freply)
   
    dbug choice=$choice
    case $choice in
       "RETRY") 
    	    return 3
       ;;
       "REBOOT") 
	    reboot_exit
	    ;;
	*)
	    infobox "As you wish ..., back to the main menu"
	    sleep 2
	    clean_exit 0
	    ;;
    esac
    return 0
}

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

