#!/bin/bash
##############  ROX app.: Click image files to mount & unmount.	by: Terry Becker	aka: SunBurnt

##############	File types = ".2fs, .3fs, .sfs, .iso, & initrd.gz".
#Translated for Russian by Valerij Kruvjalis (vkvkvk).
#121227 sfslinux@gmail.com 
rox=defaultfilemanager

 imgFile="$1"
 if [ -z "$imgFile" ] ;then exit 1 ;fi
 if [ "`dirname "$imgFile"`" = '\.' ] ;then
  imgFile="`pwd``echo "$imgFile" |sed 's/^\.//'`"
 fi
# MntPt='/mnt/'`echo "$imgFile" |sed "s#^\.##g" |sed "s#/#+#g"`
 MntPt="/mnt/`basename $imgFile`"


if [ 1 -eq 2 ] ;then	##############################################	JUMP OVER this SECTION.

 loMnts="`mount |grep '/dev/loop' |awk '{print $1}'`"
 echo "$loMnts" |while read LOOP					### test if file's mounted
 do
   loFile="`losetup-FULL $LOOP |sed 's/^.*(//' |sed 's/).*$//'`"
   if [ "$loFile" = "$imgFile" ] ;then
    echo -e "\n ### Loop File: $loFile"
    break
   fi
   i=`expr $i + 1`
 done

fi			##############################################	END of JUMP SECTION.


							### file's not mounted, mount it
# if [ $i -eq 16 ] ;then

 imgFileBASE="`basename "$imgFile"`" #BK

 if [ ! -d "$MntPt" ] ;then

 #hairywill 25 Jan 09 disabling this check, it stops user mounting identically named files
  #BK check if already mounted elsewhere...
#  for ONELOOP in `mount | grep '^/dev/loop' | cut -f 1 -d ' ' | tr '\n' ' '`
#  do
#   MNTDIMG="`losetup-FULL $ONELOOP | cut -f 2 -d '(' | cut -f 1 -d ')'`"
#    IMGBASE="`basename "$MNTDIMG"`"
    #echo "IMGBASE=$IMGBASE imgFileBASE=$imgFileBASE" #TEST
    #not perfect, but paths may be symlinks...
#      if [ "$IMGBASE" = "$imgFileBASE" ];then #if [ "$MNTDIMG" = "$imgFile" ];then
#      xmessage "Sorry $imgFile is already mounted and in-use by Puppy"
#      exit
#    fi
#  done
   
  Ext=`echo "$imgFile" |sed 's/^.*\.//'`	# get file type from extention
  if [ "$Ext" = '2fs' ] ;then
   Type='ext2'
  elif [ "$Ext" = '3fs' ] ;then
   Type='ext3'
  elif [ "$Ext" = '4fs' ] ;then
   Type='ext4'
  elif [ "$Ext" = 'sfs' ] ;then
   Type='squashfs'
  elif [ "$Ext" = 'pfs' ] ;then
   Type='squashfs'
  elif [ "$Ext" = 'iso' ] ;then
   Type='iso9660'
  # BK
  #elif [ "$imgFile" = 'initrd.gz' ] ;then
  # Type='ext2'
  # gunzip $imgFile				# handle gzip image file
  # imgFile="echo "$imgFile" |sed 's/.gz$//'"
  fi
  
  
#  echo -e "\nimgFile = $imgFile\t\tExt  = $Ext"
#  echo -e "MntPt   = $MntPt\t\tType = $Type\n"
  echo -e "\n ### Mount: $imgFile\n"
  mkdir -p "$MntPt"
  mount -t $Type -o loop "$imgFile" "$MntPt"
  Err=$?
  if [ $Err -eq 0 ] ;then
   $rox -d "$MntPt" # mount good, run rox filer
#   yaf-splash -timeout 6 -font "8x16" -outline 0 -margin 4 -bg green -text "УСПЕШНО! Щёлкните по названию файла $imgFileBASE ещё раз для размонтирования его" & #BK
   notify-send "УСПЕШНО! Для размонтирования щёлкните" "по названию файла $imgFileBASE ещё раз" -i info & #BK
  fi	

 else							### file's mounted, unmount it
  $rox -D "$MntPt" #BK
  umount "$MntPt"
  Err=$?
[ $Err -eq 0 ] && notify-send "`basename $MntPt`" "размонтирован" -i info &
  rmdir "$MntPt"
  if [ "basename "$imgFile"" = 'initrd' ] ;then gzip -9 "$imgFile" ;fi
#  yaf-splash -timeout 3 -font "8x16" -outline 0 -margin 4 -bg orange -text "Размонтирование $imgFileBASE" #BK
  #Xdialog --title " Notice..." --no-button \
  #        --infobox "
  #UnMounting: $imgFile  
  #" 0 0 2000
  echo -e "\n ### UnMount: $imgFile\n"
 fi
 if [ $Err -gt 0 ] ;then			# ERROR: mount/unmount
  xmessage -title " ОШИБКА..." "  Сбой при монтировании или размонтировании.  "
 fi
