#!/bin/bash
# GUI Mount / Unmount file for PuppyRus (supported filesystems: "Ext2", "Ext3", "Ext4", "SquashFS", "ISO") by Zay, 2012.
v=140322 #sfs
# Depends: mountfile, umountfile.
ulocfile="/usr/share/locale/"`echo $LANG | sed "s/\(..\).*/\1/"`"/LC_MESSAGES/mount_file.mo"
[ -f "${ulocfile}" ] && . "${ulocfile}" || . "/usr/share/locale/en/LC_MESSAGES/mount_file.mo"

m="выберите файл .*fs .dat .iso"
if [ "$1" ] ;then
    [ "$1" = "%F" ] || dir="$1" 
else dir="$m"
fi

G(){
MAIN_DIALOG='	
<window title="'`basename $0`' v.'$v'"  window-position="1" icon-name="gtk-add">
 <vbox>
   <frame Монтирование:                                                 ->
   <hbox>
    <entry activates_default="true" fs-action="file" fs-folder="'`pwd`'"
	fs-filters-mime="application/pfs|application/x-squashfs-image|application/x-ext2-image|application/x-ext3-image|application/x-ext4-image|application/x-cd-image">
     <variable>d</variable>
	 <input>echo "'$dir'"</input>
    </entry>

    <button>
     <input file icon="gtk-file"></input>
     <action type="fileselect">d</action>
     <action>EXIT:mk</action>
    </button>
   </hbox>

   </frame>
    <hbox>

#   <button tooltip-text="WWW справка">
#    <label>Справка</label>
#    <input file icon="gtk-help"></input>
#    <action>defaultbrowser "http://wiki.puppyrus.org/puppyrus/pr218/pfs" &</action>
#   </button>

       <button cancel></button>
#       <button ok></button>
    </hbox>
 </vbox>
</window>'

export GUI="`echo "$MAIN_DIALOG" | sed -e 's/^#.*//'`"
ret="`gtkdialog --program=GUI --center`"

eval "$ret"
#echo $ret ;exit
[ "$EXIT" = "Cancel" ] && exit
[ "$EXIT" = "abort" ] && exit
[ "$d" = "$m" ] || [ ! "$d" ] && (ntf -e "Ошибка" "Модуль не выбран" &) && ($0 &) && exit || SFS="$d"
}

if [ "$1" ] ;then
    [ "$1" = "%F" ] && G || SFS="$1" 
else G
fi



#[ ! -f "$1" ] && exit 1
filemnt="$(realpath "$SFS")"
mntpoint=/mnt/$(echo "${filemnt}" | tr ' ' '_' | sed "s#^\.##g" | sed "s#/#+#g")
if [ ! -d "${mntpoint}" ]; then

  # Check mount:
  [ "${filemnt:0:8}" != "/initrd/" ] && checkname="${filemnt}" || checkname="${filemnt:7}"
  [ -z "$(which losetup-FULL)" ] && closetpup="losetup" || closetpup="losetup-FULL"
  useloop="$(${closetpup} -a | grep -F "${checkname}" | cut -d ':' -f1)"
  if [ "${useloop}" != "" ]; then
    mpoint="$(df "${useloop}" | grep -F "${useloop}" | cut -d '%' -f2 | cut -d ' ' -f2)"
    if [ -d "${mpoint}" ]; then
      defaultfilemanager "${mpoint}"
      (ntf -i "${Loc_FILEISMOUNT1} $(basename "$SFS")" "${Loc_FILEISMOUNT2}" &) || \
      yaf-splash -timeout 6 -font "8x16" -outline 0 -margin 4 -bg orange -text "${Loc_FILEISMOUNT1} $(basename "$SFS") ${Loc_FILEISMOUNT2}"
      exit
    else
      (ntf -i "${Loc_FILEISMOUNT1} $(basename "$SFS")" "${Loc_FILEISMOUNT2}" &) || \
      yaf-splash -timeout 10 -font "8x16" -outline 0 -margin 4 -bg red -text "${Loc_FILEISMOUNT1} $(basename "$SFS") ${Loc_FILEISMOUNT2}"
    fi 
  fi

  # Check kernel version (SquashFS):
  dt="$(disktype "${filemnt}" 2>/dev/null)"
  if [ "$(echo "${dt}" | grep -F 'squashfs')" != "" ];then
    kerneluname="$(uname -r)"
    kernel1ver="$(echo -n "${kerneluname}" | cut -f 1 -d '.' | cut -f 1 -d '-')"
    #kernel2ver="$(echo -n "${kerneluname}" | cut -f 2 -d '.' | cut -f 1 -d '-')"
    kernel3ver="$(echo -n "${kerneluname}" | cut -f 3 -d '.' | cut -f 1 -d '-')"
    kernel4ver="$(echo -n "${kerneluname}" | cut -f 4 -d '.' | cut -f 1 -d '-')"   
    sfsver="squashfs, version 4"
    if [ ${kernel1ver} -lt 3 ];then
      if [ ${kernel3ver} -lt 29 ];then
        if [ ${kernel3ver} -eq 27 ];then
          [ ${kernel4ver} -lt 47 ] && sfsver="squashfs, version 3"
	    else
	      sfsver="squashfs, version 3"
	    fi
      fi
    fi
    if [ "$(echo "${dt}" | grep -F "${sfsver}")" = "" ];then
      if [ "${sfsver}" = "squashfs, version 4" ];then
        ntf -a "${Loc_ERRORSFSOLD}" || \
        Xdialog --title "$0" --msgbox "${Loc_ERRORSFSOLD}" 6 0
      else
        ntf -a "${Loc_ERRORSFSNEW}" || \
        Xdialog --title "$0" --msgbox "${Loc_ERRORSFSNEW}" 6 0
      fi
      exit
    fi
  fi
 
  # Mount:
  `which sudo` mkdir -p "${mntpoint}"
  `which sudo` mountfile "${filemnt}" "${mntpoint}"
  if [ $? -eq 0 ] ;then
    defaultfilemanager "${mntpoint}"
    (ntf -i  "${Loc_MOUNTOK1} $(basename "$SFS")" "${Loc_MOUNTOK2}" &) || \
    yaf-splash -timeout 6 -font "8x16" -outline 0 -margin 4 -bg green -text "${Loc_MOUNTOK1} $(basename "$SFS") ${Loc_MOUNTOK2}" &
  else
    `which sudo` rmdir "${mntpoint}"
    ntf -a "${Loc_MOUNTERROR}" || \
    yaf-splash -timeout 10 -font "8x16" -outline 0 -margin 4 -bg red -text "${Loc_MOUNTERROR}"; exit 1
  fi
  
else	
   
  # Unmount:
  `which sudo` umountfile "$SFS"
  if [ $? -eq 0 ]; then
    pidof rox && rox -D "${mntpoint}"
    (ntf -i  "${Loc_UNMOUNTPROGRESS} $(basename "$SFS")"  "OK!" &) || \
    yaf-splash -timeout 3 -font "8x16" -outline 0 -margin 4 -bg orange -text "${Loc_UNMOUNTPROGRESS} $(basename "$SFS") ..."
  else
    ntf -a "${Loc_UNMOUNTFAILED} $(basename "$SFS") !" || \
    yaf-splash -timeout 14 -font "8x16" -outline 0 -margin 4 -bg red -text "${Loc_UNMOUNTFAILED} $(basename "$SFS") !"; exit 1
  fi

fi

exit 0
