#!/bin/sh
##########  This script is a GUI for mounting IMAGE files of type:

##########  image, initrd, image.gz, initrd.gz, SAVE files, ISO, Squash, & generic images.


FILE=`Xdialog --stdout --title " File Mounter: image, .gz, .iso, 3fs, .fs, .sfs" --fselect /mnt 0 0`

if [ $? -gt 0 ]; then				# cancel button pressed
 exit 0;
fi

FileName="`basename $FILE`"
DirPath="`dirname $FILE`"
FileType='image(ext2)'
MntPt=''

if [ "$FileName" = 'initrd.gz' ]; then		# compressed initrd.gz file
 MntPt='/mnt/ird-gz'
 ImgFile="${DirPath}/initrd"
 gunzip $FILE
elif [ "$FileName" = 'image.gz' ]; then		# compressed image.gz file
 MntPt='/mnt/img-gz'
 ImgFile="${DirPath}/image"
 gunzip $FILE
elif [ "$FileName" = 'initrd' ]; then		# uncompressed initrd file
 MntPt='/mnt/ird'
elif [ "$FileName" = 'image' ]; then		# uncompressed image file
 MntPt='/mnt/img'
else
 FileType=''
 X=`echo "$FILE" | sed 's#^.*\.##'`		# get just file extention

 if [ "$X" = '3fs' ]; then				# ".ext3" type Save file
  FileType='image(ext3)'
 elif [ "$X" = 'iso' -o "$X" = 'ISO' ]; then		# ".iso" type ISO file
  FileType='ISO'
 elif [ "$X" = 'fs' -o "$X" = 'sfs' ]; then		# ".fs" or ".sfs" Squash file
  FileType='Squash'
 fi
fi

if [ -z $FileType ]; then		# If file not recognized, file Type dialog

 Xdialog --title " Pick: File Type." \
         --menu "Couldn't identify the file type.
 Pick the type of file to mount." 0 0 0 \
        "image(ext2)"	"An uncompressed ext2 image file." \
        "image(ext3)"	"An uncompressed ext3 image file." \
        "ISO"		"A ISO file for a CD-R." \
        "Squash"	"A compressed squash file." 2>/tmp/checklist.tmp.$$

 if [ $? -gt 0 ]; then			# cancel button pressed
  exit 0;
 fi
 FileType=`cat /tmp/checklist.tmp.$$`
 rm -f /tmp/checklist.tmp.$$
fi

if [ "$FileType" = 'image(ext2)' ]; then	# do ext2 image file
 TYPE='ext2'
 MntPt='/mnt/2fs'
elif [ "$FileType" = 'image(ext3)' ]; then	# do ext3 image file
 TYPE='ext3'
 MntPt='/mnt/3fs'
elif [ "$FileType" = 'ISO' ]; then		# do ISO image file
 TYPE='iso9660'
 MntPt='/mnt/iso'
elif [ "$FileType" = 'Squash' ]; then		# do S image file
 TYPE='squashfs'
 MntPt='/mnt/sqs'
fi

FSMNT=`mount`			# del. unused mount points
for i in 0 1 2 3 4 5 6 7
do
 if [ -f "${MntPt}_"$i ]; then
  if [ -z `echo "${MntPt}_"$i | grep -i "$FSMNT"` ] ; then
   rmdir ${MntPt}_$i
  fi
 fi
done 

for i in 0 1 2 3 4 5 6 7	# find unused mount point
do
 if [ ! -f "${MntPt}_"$i ]; then
  MntPt="${MntPt}_"$i
  break
 fi
done

echo ""
echo "#####   FILE  = $FILE"
echo "#####   EXT.X = $X"
echo "#####   TYPE  = $TYPE"
echo "#####   MntPt = $MntPt"
echo ""

mkdir -p $MntPt

if [ "$FileName" = "initrd.gz" -o "$FileName" = "image.gz" ]; then
 mount -t $TYPE -o loop $ImgFile $MntPt
else
 mount -t $TYPE -o loop $FILE $MntPt
fi

if [ $? -gt 0 ]; then			# error: mounting failed
 xmessage "
 ERROR: Failed to mount the file: $FILE 

 "
								# recompress ".gz" file
 if [ "$FileName" = "initrd.gz" -o "$FileName" = "image.gz" ]; then
  gzip -9 $ImgFile
 fi
 rmdir $MntPt				# delete mount point
 exit 1;
fi

rox $MntPt			# open mounted file in ROX

				# success mounting, & info.
xmessage "
  Success mounting the file:

  $FILE  

  The file is mounted on: $MntPt  


  When ready, click [okay] to unmount the file.  
  =============================================  

"

while :
do
 umount $MntPt		# unmount file

 if [ $? -gt 0 ]; then		# if file not unmounted, ERROR
  xmessage -center "
  ERROR: File was NOT UnMounted.  

  You may still be in: $MntPt 

  If so, change path out of it.  


    When ready, click [okay].
    =========================
  "
 else							# if file unmounted, exit
  break
 fi  
done
								# recompress ".gz" file
if [ "$FileName" = "initrd.gz" -o "$FileName" = "image.gz" ]; then
 gzip -9 $ImgFile
fi
rmdir $MntPt							# delete mount point

xmessage -center "
  UnMounted the file: $FILE  

"
