#!/bin/sh
#
#Name:floppimageycreator
#
#Creates Floppy Images Files, that can be moutet as root, or by  puting to fstab, by any user.
#
#Permissions: To use you need sometime root rights, for fstab, the user has to be in group plugdev,
#this is a standard group in Ubuntu for users.
#
#Version: May 2010
#Testet in Ubuntu 9.1
#
#Requierd Packages: dosfstools
#  apt-get install dosfstools
#
#copy this file to /usr/local/bin/floppimageycreator
#chmod +xs /usr/local/bin/floppyimagecreator


ver="(v.May 2010)"

#where goes the Files?
store_folder=/opt"/Floppy_images"


while [ "$answ" != 6 ];do
clear

echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo
echo "			1.	Create Floppy Files"
echo "			2.	Mount the Images"
echo "			3.	Umount the Images"
echo "			4.	Remove all Files"
echo "			5.	Put the Images in fstab"
echo "			6.     	Abort/Quit"
echo
echo " The Files are stored in ""$store_folder"". In /media are new Floppy Folder as"
echo " mount Points. By Remove ALL, /media points and files will removed, fstab"
echo " cleand out."
echo " This Floppy files you can use for different things you like, maybe also for"
echo " Vmware or VirtualBox, Qemu etc.."
echo
echo " Tipp: If you put it to fstab, all users can use these floppy images"
echo
echo
echo "		    !!   Sometimes you need root password. !!"
echo
echo -n "                                                   What you want? : "
read a

answ="$a"

case "$answ" in


1)
#Create the Floppy Images
change="0"
sudo mkdir -p $store_folder
sudo chown root.plugdev $store_folder
sudo chmod 775 $store_folder

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "			   You want a 1.44MByte Floppy?"
echo
echo -n "                                                   You want? y/n: "

read b
if [ "$b" = "y" ];then 
 dd if=/dev/zero of=$store_folder/1_Floppy1440.img bs=1024 count=1440
 change="1"
fi



clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo
echo "              	  You want another 1.44MByte Floppy?"
echo
echo -n "                                                   You want? y/n: "

read b
if [ "$b" = "y" ];then
 dd if=/dev/zero of=$store_folder/2_Floppy1440.img bs=1024 count=1440
 change="1"
fi


clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "                 	   You want a 2.88MByte Floppy?"
echo
echo -n "                                                   You want? y/n: "

read b
if [ "$b" = "y" ];then
 dd if=/dev/zero of=$store_folder/1_Floppy2880.img bs=1024 count=2880
 change="1"
fi


clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "                 	   You want another 2.88MByte Floppy?"
echo
echo -n "                                                   You want? y/n: "

read b
if [ "$b" = "y" ];then
 dd if=/dev/zero of=$store_folder/2_Floppy2880.img bs=1024 count=2880
 change="1"
fi

#if anything change, i format all
if [ "$change" -ge "1" ];then
for file in  $store_folder/*.*; do mkdosfs -F 12 $file; done
fi
#put the rights to the files
sudo chown -R .plugdev $store_folder;sudo chmod -R 774 $store_folder
#create the mointpoints
for file in  $store_folder/*; do 
mpoint=`basename "$file" | cut -d "." -f 1`
sudo mkdir -p /media/"$mpoint"
done
;;

2)
#Mounting the Floppy Images
for file in  $store_folder/*; do 
mpoint=`basename "$file" | cut -d "." -f 1`
sudo mount -o loop "$file"  /media/"$mpoint"
done

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "                	   All Floppy Images are mounted!"
sleep 3

;;

3)
#Umount Floppy Images
sudo umount /media/1_Floppy1440; sudo umount /media/2_Floppy1440; sudo umount /media/1_Floppy2880; sudo umount /media/2_Floppy2880

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "                	   All Floppy Images are unmounted!"
sleep 3

;;

4)
#Remove all Files
sudo umount /media/1_Floppy1440; sudo umount /media/2_Floppy1440; sudo umount /media/1_Floppy2880; sudo umount /media/2_Floppy2880
sudo rm -rf $store_folder;sudo rm -rf /media/*_Floppy*

#Checking for Backup fstab
if [ -f /etc/fstab.backup ];then
echo
else
sudo cp /etc/fstab /etc/fstab.backup

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "     	I made a Backup of /etc/fstab to /etc/fstab.backup"
read b
fi

#wipe the fstab entries out
sudo sh -c "sed '/_Floppy/d' </etc/fstab >/etc/fstab2"
sudo mv /etc/fstab2 /etc/fstab

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "                     	  All Files removed!"
sleep 3

;;

5)
#Put Floppy Images in fstab for users mounting, or remove the entries

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo " This will add the Lines for your Floppy to the /etc/fstab. So you can mount"
echo " it as User inside Gnome Desktop in Nautilus."
echo " Or by command  mount /media/1_Floppy1440 as normal user."
echo 
echo " It will alltimes listet as Storage Devices in Nautilus. Not to list, at the"
echo " time the entries are in fstab, you have to modifiy the hal or something"
echo " like that.But better just let it how it is. :)"
echo
echo
echo "				1.  Fill Fstab"
echo "				2.  Unfill Fstab"
echo
echo 
echo -n "                                        Nothing = Abort    What you want? : "
read b
answ_fstab="$b"


if [ "$answ_fstab" = 1 ];then

#Checking for Backup fstab
if [ -f /etc/fstab.backup ];then
echo
else
sudo cp /etc/fstab /etc/fstab.backup

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "     	I made a Backup of /etc/fstab to /etc/fstab.backup"
read b
fi

#First Blank old entries out of fstab
sudo sh -c "sed '/_Floppy/d' </etc/fstab >/etc/fstab2"
sudo mv /etc/fstab2 /etc/fstab

#Build the new Entries in fstab
for file in  $store_folder/*; do 

if [ -f "$file" ];then

#remark for the Entrie
sudo echo "#Generated by floppyimagecreator: Image_Floppy, you can remove with floppyimagecreator" | sudo tee -a /etc/fstab


#build the entries and write it
mpoint=`basename "$file" | cut -d "." -f 1`
sudo echo "$file""     /media/""$mpoint""    msdos     loop,defaults,users,noauto     0      0" | sudo tee -a /etc/fstab

fi
done

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "             	fstab fill with mount points finsih!"
sleep 3

fi


#unfill fstab
if [ "$answ_fstab" = 2 ];then

#Checking for Backup fstab
if [ -f /etc/fstab.backup ];then
echo
else
sudo cp /etc/fstab /etc/fstab.backup

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "     	I made a Backup of /etc/fstab to /etc/fstab.backup"
read b
fi

sudo sh -c "sed '/_Floppy/d' </etc/fstab >/etc/fstab2"
sudo mv /etc/fstab2 /etc/fstab

clear
echo "       MS-DOS Floppy Files Creator / Mounting / Umounting  TOOL ""$ver"
echo "=============================================================================="
echo 
echo "                  	fstab fill removed"
sleep 3

fi
;;

esac

done
clear
exit 0
