#!/bin/sh
#Format floppy disks
#Copyright (c) Barry Kauler 2004 www.goosee.com/puppy
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)

. /usr/bin/localization 


zapfloppy()
{
# Puppy will only allow 1440, 1680K and 1760K capacities.
ERR0=1
while [ ! $ERR0 -eq 0 ];do
xmessage -bg "#c0ffff" -center -title "$(eval_gettext "Please wait...")" -buttons "" "$(eval_gettext "Low-level formatting disk with \$1 Kbyte capacity")" &
 fdformat /dev/fd0u$1
 ERR0=$?
 killall xmessage
 if [ ! $ERR0 -eq 0 ];then
  xmessage -bg "#ffe0e0" -name "loformat" -title "$(eval_gettext "Puppy Lo-level Formatter")" -center \
  -buttons "$(eval_gettext "Try again")":20,"$(eval_gettext "QUIT")":10 -file -<<XMSG
ERROR low-level formatting disk.
Is the write-protect tab closed?
XMSG

  AN0=$?
  if [ $AN0 -eq 10 ];then
   ERR0=0
  fi
  if [ $AN0 -eq 0 ];then
   ERR0=0
  fi
  if [ $AN0 -eq 1 ];then
   ERR0=0
  fi
 else
  INTROMSG="`
echo "$(eval_gettext "SUCCESS!")"
echo -e "$(eval_gettext "Now you should press the \"Msdos/vfat filesystem\" button.")"
`"
 fi
done
}

fsfloppy()
{
echo "Creating msdos filesystem on the disk..."
ERR1=1
while [ ! $ERR1 -eq 0 ];do
xmessage -bg "#c0ffff" -center -title "$(eval_gettext "Please wait...")" -buttons "" "$(eval_gettext "Creating msdos/vfat filesystem on floppy disk")" &
 mkfs.msdos -c /dev/fd0u$1
 #mformat -f $1 a:
 #mbadblocks a:
 ERR1=$?
 killall xmessage
 if [ ! $ERR1 -eq 0 ];then
  xmessage -bg "#ffe0e0" -name "msdosvfat" -title "$(eval_gettext "Floppy msdos/vfat filesystem")" -center \
  -buttons "$(eval_gettext "Try again")":20,"$(eval_gettext "QUIT")":10 -file -<<XMSG
ERROR creating msdos/vfat filesystem on the floppy disk.
Is the write-protect tab closed?
XMSG

  AN0=$?
  if [ $AN0 -eq 10 ];then
   ERR1=0
  fi
  if [ $AN0 -eq 0 ];then
   ERR1=0
  fi
  if [ $AN0 -eq 1 ];then
   ERR1=0
  fi
 else
  INTROMSG="`
echo "$(eval_gettext "SUCCESS!")"
echo "$(eval_gettext "The floppy disk is now ready to be used.")"
echo "$(eval_gettext "Use the Puppy Drive Mounter to mount it.")"
echo "$(eval_gettext "(NOTE: if you use the MToolsFM floppy file")"
echo "$(eval_gettext " manager, the floppy drive is accessed directly,")"
echo "$(eval_gettext " so do NOT use the Puppy Drive Mounter)")"
echo "$(eval_gettext "First though, press EXIT to get out of here...")"
`"
 fi
done
sync
echo "...done."
echo " "
}

INTROMSG="`
echo "$(eval_gettext "WELCOME!")"
echo "$(eval_gettext "My Puppy Floppy Formatter only formats floppies with")"
echo "$(eval_gettext "1440 Kbyte capacity and with the msdos/vfat filesystem,")"
echo "$(eval_gettext "for interchangeability with Windows.")"
echo " "
echo "$(eval_gettext "You only need to lo-level format if the disk is formatted")"
echo "$(eval_gettext "with some other capacity, or it is corrupted. You do not")"
echo "$(eval_gettext "have to lo-level format a new disk, but may do so to")"
echo "$(eval_gettext "check its integrity.")"
echo "$(eval_gettext "A disk is NOT usable if it is only lo-level formatted: it")"
echo "$(eval_gettext "also must have a filesystem, so this must always be the")"
echo "$(eval_gettext "second step.")"
echo "$(eval_gettext "Doing step-2 only, that is, creating a filesystem on a")"
echo "$(eval_gettext "disk, is also a method for wiping any existing files.")"
`"

#big loop...
while :; do

MNTDMSG=" "
mount | grep "/dev/fd0" > /dev/null 2>&1
if [ $? -eq 0 ];then #=0 if string found
 CURRENTMNT="`mount | grep "/dev/fd0" | cut -f 3 -d ' '`"
 #this tells Rox to close any window with this directory and subdirectories open...
 rox -D "$CURRENTMNT"
 sync
 umount "$CURRENTMNT" #/mnt/floppy
 if [ ! $? -eq 0 ];then
  MNTDMSG="`
echo " "
echo "$(eval_gettext "Puppy found a floppy disk already mounted in the drive, but")"
echo "$(eval_gettext "is not able to unmount it. The disk must be unmounted now.")"
echo "$(eval_gettext "Please use the Puppy Drive Mounter (in the File Manager menu)")"
echo "$(eval_gettext "to unmount the floppy disk. DO THIS FIRST!")"
echo " "
`"
 else
  MNTDMSG="`
echo " "
echo "$(eval_gettext "Puppy found that the floppy disk was mounted, but has now")"
echo "$(eval_gettext "unmounted it. Now ok to format disk.")"
echo " "
`"
 fi
fi

xmessage -bg "#e0ffe0" -name "pformat" -title "$(eval_gettext "Puppy Floppy Formatter")" -center \
-buttons "$(eval_gettext "Lo-level Format")":20,"$(eval_gettext "Msdos/vfat filesystem")":30,"$(eval_gettext "EXIT")":10 -file -<<XMSG
$INTROMSG
$MNTDMSG
Press a button:
XMSG

ANS=$?

if [ $ANS -eq 0 ]; then
 break
fi
if [ $ANS -eq 1 ]; then
 break
fi
if [ $ANS -eq 10 ]; then
 break
fi

if [ $ANS -eq 20 ];then #format
 zapfloppy 1440
fi

if [ $ANS -eq 30 ];then #vfat
 fsfloppy 1440
fi

done
