#!/bin/sh

# ----------------------------------------------
# fformat -- Linux Floppy Formatting Utility
# -----------------------------------------------
# (c) Nick Monyatovsky - nmonya01@barney.poly.edu
# --- PITFALLS ---
# Extra spaces after \ at the end of the line caused me much trouble.
# Another pitfall is to write "xxx""yyyyy". SPACE must be in between them!
# Manipulating block-size is not possible at the moment.
# So, this option exists there, but is really cosmetic.
# ALL disks are formatted with BLOCK_SIZE = 1024.


# set this to point to the README file that came with the package, eg:
# HELP_FILE="/usr/local/scripts/README"
HELP_FILE="README"

# This allows you to customize this program for future use.
# 
# -------------------------- 
# DEFAULT SETTINGS 
# --------------------------
# --- Disk size ("1440" or "720") in Kbytes
DISK_SIZE="1440"
# --- Linux device file that corresponds to that drive
#     May be either "/dev/fd0" or "/dev/fd1"; rarely anything else.
DEVICE_FILE="/dev/fd0"
# --- verbose mode of operation ("ON" or "OFF")
VERBOSE="ON"    
# --- check for bad clusters ("ON" or "OFF")
CHECK="ON"
# --- Block size. (Usual values are "512" or "1024")
BLOCK_SIZE="1024"
# --- Inode density. (Usual values are "1024", "2048" or "4096")
INODE_DENSITY="4096"

# colors must be used as follows:
#       echo -ne (No Newline at the end, allow Escape sequences translation)
COLOR1="\033[0m\033[37;44m"   # normal white  on blue 
COLOR2="\033[1;6m\033[36;44m" # bright cyan on blue 
COLOR3="\033[1;6m\033[31;40m" # bright red on black 
COLOR_RESET="\033[0m" 


# ---- Enter the main loop which you exit only when QUIT is pressed -----
while [ 0 ]; do

 ##########################################
 # ---------  MAIN WINDOW ----------------#
 ###########--------------#################
dialog \
	--title "Linux Floppy Formatting" \
	--menu "\n\
This program will format a floppy and will create \n\
an ext2 filesystem on it so that it will look as any \n\
other linux drive (same long filenames, links, etc.) \n\
                                                     \n\
Current configuration: DEVICE: $DEVICE_FILE SIZE: $DISK_SIZE KB  \n\
BLOCK SIZE: $BLOCK_SIZE bytes, INODE DENSITY: 1 per $INODE_DENSITY bytes" \
22 70 9 \
  "START" 	  "Start formatting $DEVICE_FILE $DISK_SIZE KB" \
  "QUIT" 	  "Exit"                          \
  "EXPLAIN" 	  "Read \"README\" file" \
  "DISK SIZE"	  "Set disk size                      $DISK_SIZE KB " \
  "DEVICE FILE"   "Linux device file for this drive   $DEVICE_FILE  " \
  "BLOCK SIZE" 	  "Set the block size                 $BLOCK_SIZE bytes " \
  "INODE DENSITY" "Set the inode density              1/$INODE_DENSITY bytes" \
  "CHECK" 	  "Check for bad clustes              $CHECK      " \
  "VERBOSE" 	  "Verbose operation                  $VERBOSE    " \
  2> /tmp/ffloppy.temp
 
  if [ $? = 1 -o $? = 255 ]; then
     rm -f /tmp/ffloppy.temp
     # reset just restores the terminal if it got messy
     reset
     exit
  fi
 
  MAINSELECT="`cat /tmp/ffloppy.temp`"
  rm /tmp/ffloppy.temp

 # Start checking what to do. Some modules may reset MAINSELECT to run the
 # next item in line.
 # ------------------------------------------------------------------------
 # PARAMETER SETTING WINDOWS
 # ------------------------------------------------------------------------

 if [ "$MAINSELECT" = "EXPLAIN" ]; then
    #echo "--- EXPLAIN was pressed ---"
    if [ -f $HELP_FILE ]; then
       dialog \
	    --title "Using Floppies Help" \
	    --textbox "$HELP_FILE" \
	    24 80
       continue
    else
	dialog \
	   --title "Using Floppies Help" \
	   --msgbox "I am sorry, I cannot find \
the README file. I can find it only when you run me out of that dirrectory \
or if you have set the pointer to it on top of fformat script." \
	   15 70
    fi
 fi
 
if [ "$MAINSELECT" = "DISK SIZE" ] ; then
    # echo " --- DISK SIZE was Pressed --- "
    # --------------------------------------------------------
    # SELECT DISK SIZE DIALOG
    # --------------------------------------------------------
    # It prompts the user for a value. Then that value is store
    # in file /tmp/Fdrive_size, from where other programs may
    dialog  --title "SELECT DRIVE SIZE" \
	--menu "\n What is your disk size? \n " 13 40 2 \
	"1440" "KB" \
	"720"  "KB" \
        2> /tmp/Fdisk_size

	if [ $? = 1 -o $? = 255 ]; then
	   rm -f /tmp/Fdisk_size 
	   # exit
	fi
	
	DISK_SIZE=`cat /tmp/Fdisk_size`
	rm -f /tmp/Fdisk_size
	# echo DISK_SIZE is set to $DISK_SIZE
        # echo "continuing"
        # exit
 fi

 if [ "$MAINSELECT" = "DEVICE FILE" ]; then
    # echo "--- DEVICE FILE was pressed ---" 
    # -----------------------------------
    # SELECT DEVICE FILE DIALOG
    # -----------------------------------
	dialog 	--title "SELECT FLOPPY DEVICE FILE" \
	        --menu "\n Select device file that \ncorresponds to your drive." \
		12 50 2 \
	"/dev/fd0" 	"Generic floppy in drive A:" \
	"/dev/fd1" 	"Generic floppy in drive B:" \
	2> /tmp/Fdevice_file
	# "/dev/fd0H1440" "1.44M drive a:" \
	# "/dev/fd1H1440" "1.44M drive b:" \
	# "/dev/fd0h1200" "1.2M drive a:" \
	# "/dev/fd1h1200" "1.2M drive b:" \

	if [ $? = 1 -o $? = 255 ]; then
 	    rm -f /tmp/Fdevice_file 
 	    # exit	
	fi
	
	DEVICE_FILE="`cat /tmp/Fdevice_file`"
	rm -f /tmp/Fdevice_file
	# echo "DEVICE_FILE is set to $DEVICE_FILE"
        # exit
fi

 if [ "$MAINSELECT" = "BLOCK SIZE" ]; then
    # echo "--- BLOCK SIZE was pressed ---"
    # ------------------------------------------
    # SELECT BLOCK SIZE DIALOG
    # -------------------------------------------
	dialog \
	--title "SELECT BLOCK SIZE" \
	--menu "\n\
Smaller block size means less wasted space   \n\
		                 (and longer file reads). \n\
Larger  block size means more wasted space   \n\
    		                 (and faster file reads). \n\
Unix standard is to use 1024 bytes which is \n\
a good compromise between space and performance." \
	18 65 3 \
	"512"  "Better disk space utilization" \
	"1024" "Standard for Unix systems" \
	"2048" "Faster file access" \
	2> /tmp/Fblock_size
    
	BLOCK_SIZE="`cat /tmp/Fblock_size`"
	rm -f /tmp/Fblock_size

	if [ ! "$BLOCK_SIZE" = "2048" -a ! "$BLOCK_SIZE" = "1024" ]; then
   	    BLOCK_SIZE=512
	fi
   
	#echo "BLOCK_SIZE:  $BLOCK_SIZE bytes."
	# exit
 fi

 if [ "$MAINSELECT" = "INODE DENSITY" ]; then
    	#echo "--- INODE DENSITY was pressed ---"
	# -----------------------------------------
	# SELECT INODE DENSITY DIALOG
        # -----------------------------------------  
	dialog \
	--title "SELECT INODE DENSITY" \
	--menu "\
Ext2 file system defaults to one inode per 4096 bytes \
of drive space. EACH FILE requires ONE INODE. Therefore, \
if you're going to have many small files on your drive, \
you need more inodes (--> choose smaller value -- '2048' or '1024'). \
However, more inodes also means slower file access. \
If your files are not going to be small, then just hit return \
to accept the default of 4096 bytes." \
	18 65 3 \
	"4096" 	"1 inode per 4096 bytes. (default)" \
	"2048" 	"1 inode per 2048 bytes." \
	"1024" 	"1 inode per 1024 bytes." \
	2> /tmp/Finode_density
    
	INODE_DENSITY="`cat /tmp/Finode_density`"
	rm -f /tmp/Finode_density

	if [ ! "$INODE_DENSITY" = "2048" -a ! "$INODE_DENSITY" = "1024" ]; then
   	    INODE_DENSITY=4096
	fi
   
	#echo "Inode density: 1 inode per $INODE_DENSITY bytes."
	# exit
 fi

 if [ "$MAINSELECT" = "CHECK" ]; then
    # echo "--- CHECK was pressed ---"
    # --------------------------------------------------------
    # CHECK FOR BAD CLUSTERS DIALOG
    # --------------------------------------------------------
    dialog --title "CHECK FOR BAD CLUSTERS" \
	--menu "\n Set checking for bad clusters \n " 13 40 2 \
	"ON" 	"" \
	"OFF" 	"" \
	2> /tmp/Fcheck_clusters

	if [ $? = 1 -o $? = 255 ]; then
	   rm -f /tmp/Fcheck_clusters 
	#  exit
	fi
	
	CHECK=`cat /tmp/Fcheck_clusters`
	rm -f /tmp/Fcheck_clusters
	# echo CHECK is set to $CHECK
        # echo "continuing"
 	# exit
 fi

 if [ "$MAINSELECT" = "VERBOSE" ]; then
    # echo "--- VERBOSE was pressed ---"
    # --------------------------------------------------------
    # VERBOSE MODE DIALOG
    # --------------------------------------------------------
    dialog --title "VERBOSE MODE" \
	--menu "\n Verbose mode \n " 13 40 2 \
	"ON" 	"" \
	"OFF" 	"" \
	2> /tmp/Fverbose_mode

	if [ $? = 1 -o $? = 255 ]; then
	   rm -f /tmp/Fverbose_mode 
	# exit
	fi
	
	VERBOSE=`cat /tmp/Fverbose_mode`
	rm -f /tmp/Fverbose_mode
	# echo VERBOSE is set to $VERBOSE
        # echo "continuing"
 	# exit
 fi

 #------------------------------------
 # ACTION PART OF THE PROGRAM
 #------------------------------------
 if [ "$MAINSELECT" = "START" ]; then
    # echo "--- START was pressed ---"
    # echo "Entering the main Part of the program"
    # echo "Starting formatting"
    if [ "$VERBOSE" = "ON" ]; then
	VERBOSE_STRING=" -v "	  
    fi
    if [ "$CHECK" = "ON" ]; then
	CHECK_STRING=" -c "	  
    fi

    dialog \
	--title "FORMATTING" \
	--infobox "\
                                    \n\
              Device:  $DEVICE_FILE \n\
                Size:  $DISK_SIZE KB \n\
     Filesystem type:  ext2          \n\
          Block size:  $BLOCK_SIZE bytes\n\
       Inode density:  1/$INODE_DENSITY bytes\n\
Bad cluster checking:  $CHECK "       \
	10 45

	echo -e $COLOR1		# set background to blue
	# ======== /dev/fd0 disks ========================
	if [ "$DEVICE_FILE" = "/dev/fd0" ]; then
	    umount /dev/fd0 1> /dev/null 2> /dev/null
	    if [ "$DISK_SIZE" = "1440" ]; then
		fdformat /dev/fd0H1440
		echo -ne $COLOR2 
		echo "=========== Creating ext2 filesystem =============" 
		echo -ne $COLOR1
		mke2fs $CHECK_STRING -i $INODE_DENSITY $VERBOSE_STRING /dev/fd0 1440
		echo -ne $COLOR2 
		echo "===================  DONE  ======================="
		echo -ne $COLOR1 
	    fi
	    if [ "$DISK_SIZE" = "720" ]; then
		fdformat /dev/fd0D720
		echo -ne $COLOR2 
		echo "=========== Creating ext2 filesystem ============="
		echo -ne $COLOR1 
		mke2fs $CHECK_STRING -i $INODE_DENSITY $VERBOSE_STRING /dev/fd0 720
		echo -ne $COLOR2 
		echo "===================  DONE  ======================="
		echo -ne $COLOR1 
	    fi
	fi
	# ======== /dev/fd1 disks ========================
	if [ "$DEVICE_FILE" = "/dev/fd1" ]; then
            umount /dev/fd1 1> /dev/null 2> /dev/null
	    if [ "$DISK_SIZE" = "1440" ]; then
		fdformat /dev/fd1H1440
		echo -ne $COLOR2 
		echo "=========== Creating ext2 filesystem ============="
		echo -ne $COLOR1 
		mke2fs $CHECK_STRING -i $INODE_DENSITY $VERBOSE_STRING /dev/fd1 1440
		echo -ne $COLOR2 
		echo "===================  DONE  ======================="
		echo -ne $COLOR1 
	    fi
	    if [ "$DISK_SIZE" = "720" ]; then
		fdformat /dev/fd1D720
		echo -ne $COLOR2 
		echo "=========== Creating ext2 filesystem ============="
		echo -ne $COLOR1 
		mke2fs $CHECK_STRING -i $INODE_DENSITY $VERBOSE_STRING /dev/fd1 720
		echo -ne $COLOR2 
		echo "===================  DONE  ======================="
		echo -ne $COLOR1 
	    fi
	fi
    	sleep 1
	echo -ne $COLOR_RESET	# Reset colors to normal (black)
	# -------------------
	# FORMAT ANOTHER? BOX
	# -------------------
	dialog 	--title "FORMATTING"\
		--yesno "\n  Format another?   \n" 8 25

	if [ $? = 1 -o $? = 255 ]; then
	    reset
 	    exit
	fi
fi


if [ "$MAINSELECT" = "QUIT" ]; then
   reset	# restore the monitor properties
   exit
fi


done 
# ------- end of main loop -----------------
 
   



