#! /bin/bash
##########################################################
# $Id: format,v 0.2 2000/04/22 17:25:45 bish Exp $
# (C) 2002 USM Bish <bish@nde.vsnl.net.in>
# Licence: GPL v2 - http://www.gnu.org/copyleft/gpl.html
#
# Purpose: Frontend for fdformat and mkfs/ mke2fs together
# to cut short a dual step mechanism for floppy formatting
# for 3.52 inch floppies. For floppies of other sizes, the
# script needs tinkering in function command_line_chk
#
# Being a command line utility, the interactive  mode also
# prompts for parameters, as expected on the command line.
#
##########################################################

##########################################################
# Constants and command line parameters
##########################################################

PROGNAME=$(basename $0)
VERSION="0.2"

CDEV=$1
CSIZ=$2
CTYP=$3

#########################################################
# functions
#########################################################

function chk_root
{
   if ! [ $UID -eq 0 ]; then
      echo "$PROGNAME : For root access only"  
      exit 1
   fi
}      

function chk_progs
{  
   # This scripts depends on fdformat, mkfs mkdosfs and mke2fs 
   # Quit if not installed
   
   for i in fdformat fsck mkdosfs mke2fs mkfs.minix; do
       TEMP=`which $i`
       if [ -z "$TEMP" ]; then
          echo "$PROGNAME : -ERROR- $i not installed on this box"
          exit 1
       fi
   done
}
           
function command_line_chk
{
        # Ensure that devices as below exist on your system
        # Some distros name them with an upper case "U".
        # Change if necessary
        
        case $CDEV in
        fd0) case $CSIZ in
               1440) SIZE="/dev/fd0u1440" ;;
               1680) SIZE="/dev/fd0u1680" ;;
               1760) SIZE="/dev/fd0u1760" ;;
               *) menu
             esac  
             ;;
        fd1) case $CSIZ in
               1440) SIZE="/dev/fd1u1440" ;;
               1680) SIZE="/dev/fd1u1680" ;;
               1760) SIZE="/dev/fd1u1760" ;;
               *) shift; menu
             esac  
             ;;     
          *) menu
        esac      
              
        case $CTYP in
          ext2) echo -en "        o Enter volume label for disk : "
                read VOL
                CMD="mke2fs -i 2000 -L $VOL /dev/$CDEV" 
                ;;
         fat32) CMD="mkdosfs -F 12 /dev/$CDEV" ;;
         fat16) CMD="mkdosfs -F 16 /dev/$CDEV" ;;   
         fat12) CMD="mkdosfs -F 32 /dev/$CDEV" ;;
         minix) CMD="mkfs.minix /dev/$CDEV" ;;
             *) menu                        
        esac
 }
 
 function do_it
 {       
        clear
        echo
        echo -en "[33;40;1m        $PROGNAME [m                [[33;40mVersion[m : $VERSION]\n\n"
        echo -en "        [32;40mCommand [33;40m#1[m : fdformat $SIZE\n"
        echo -en "        [32;40mCommand [33;40m#2[m : $CMD\n\n"
        echo -en "        [36;40mContinue ?[m [y/n] : "
        read YN
        case $YN in
        y|Y) echo 
             echo -en "        o [31;40;1mformatting[m ...\n\n\n"
             fdformat $SIZE
             if [ $? -eq 0 ]; then
                echo
                echo -en "        o Press [[36;40mReturn[m] to make [35;40m$CTYP[m partition\n"
                read
                $CMD
             else
                echo
                echo -en "        o [31;40;1mError[m at formatting. Aborting ...\n"
                exit 1
             fi           
             ;;

          *) echo "        o Quitting ..."
             exit 1
        esac  
}
         
function menu
{
        clear
	local tab=$(echo -en "\t\t")
		
	cat <<- -EOF-

[33;40;1m        $PROGNAME [m                                    [[33;40mVersion[m : $VERSION]

        Frontend for the fdformat program. [For [31;40;1mroot[m access only].
	
        [36;40mUsage[m: $PROGNAME [37;40;1m[device] [size] [type][m
        
        [32;40mOptions[m:  o [35;40mDevice[m = fd0    fd1
                  o [35;40mSize[m   = 1440   1680    1760 
                  o [35;40mType[m   = ext2   fat32   fat16   fat12   minix 

        [33;40mNote[m: for DOS file systems (fat32, fat16 and fat12) the size 
        has to be 1440. Higher capacities okay for ext2 and minix.
                
        Type in last 3 parameters like : #$PROGNAME [37;40;1mfd0 1440 ext2[m
        Or type "[33;40;1mexit[m" to quit
                   			
-EOF-
        echo -en "        [34;40;1m#format [m"
        read PARAM
        CDEV=$(echo $PARAM | awk '{print $1}')
        if [ "$CDEV" = "exit" ]; then
           exit 0
        fi   
        CSIZ=$(echo $PARAM | awk '{print $2}')
        CTYP=$(echo $PARAM | awk '{print $3}')
        command_line_chk        
}	


#########################################################
# Main program begins here
#########################################################

chk_progs
chk_root

if [ "$3" ]; then
   command_line_chk
   do_it
else   
   menu
   do_it
fi
exit

#########################################################
# Everything below this ignored
#########################################################
