#!/bin/bash
#set -x
#debiandog
#150411 sfs , moded by Fred (fred181) for DebianDog, with better support for special mountpoints (besides /media/* or /mnt/*) and difference in boot methods.
#Without progressbar 'color fill' otherwise it crashes sometimes, idea from Toni (saintless). 
# Dependency for probepart removed, using disktype instead (much faster).
#170705 sfs ref +exit button
###########################################
 
# Uses thunar as filemanager (for openbox version) but only if both thunar AND openbox are installed, to use xfe instead of rox on Jwm version replace "rox -d" with xfe on the next line.
#[[ `which thunar` && `which openbox` ]] && FLMAN=thunar || FLMAN=rox || FLMAN=defaultfilemanager
FLMAN=defaultfilemanager ###sfs 4 pra
media=mnt
MOPT="-o umask=000"
#which gsu && gsu=gsu || gsu=sudo
if [ -z `which gsu` ]; then
[ "`whoami`" != "root" ] && exec gksu ${0} "$@"
else
[ "`whoami`" != "root" ] && exec gsu ${0} "$@"
fi

export TEXTDOMAINDIR=/usr/local/share/locale
export TEXTDOMAIN=mount-wizard
export OUTPUT_CHARSET=UTF-8

# Moded for DebianDog version of Iguleder's mount-wizard script.


while true
do
_choose_action() {
   i=0
echo '<window title="'$(gettext 'Mounting Wizard')'" icon-name="gtk-harddisk">
<vbox vscrollbar-policy="1">
'
echo '<hbox space-expand="true">'
   for partition in $1
   do

if [ -f /mnt/live/tmp/modules ]; then
HDRV=`cat /mnt/live/etc/homedrv | cut -c6-9`
BACKDRVEXIT="$(df -h | grep -P '/mnt/live/memory/images/changes-exit' | cut -c6-9)"
BACKDRVCH="$(df -h | grep -P '/mnt/live/memory/changes' | cut -c6-9)"
if [ -n "$BACKDRVEXIT" ]; then 
BACKDRV="$BACKDRVEXIT"
elif [ -n "$BACKDRVCH" ]; then
BACKDRV="$BACKDRVCH"
fi
    if [ "$BACKDRV" != "$partition" ]; then
    BACKDRV="$(cat /mnt/live/tmp/modules | grep 'changes' | grep -o "$partition")"
	fi

else
if grep -q /lib/live/mount /proc/mounts; then # live-boot v3
if grep -q 'persistence' /proc/cmdline; then
HDRV="$(basename "$(readlink /live/image)")"
else
HDRV="$(df -h | grep -P /lib/live/mount/medium | cut -c6-9)"
fi
if [ "$(losetup -a | grep '/live/persistence')" ];then
BACKDRV="$(losetup -a | grep '/live/persistence' | grep -o "$partition")"
else
BACKDRV="$(basename "$(readlink /live/cow)")"
fi
else # live-boot v2
HDRV="$(df -h | grep -P /live/image | cut -c6-9)"
if [ "$(df -h | grep -P '(?=.*live-rw)(?=.*backing)')" ]; then
BACKDRV="$(df -h | grep -P '(?=.*live-rw)(?=.*backing)' | cut -c6-9)"
else
BACKDRV="$(df -h | grep -P /live/cow | cut -c6-9)"
fi
fi
fi

      fst="`blkid -o value -s TYPE "/dev/$partition"`"
      info=`disktype "/dev/$partition"`
      all=`echo "$info" |sed '/, size /!d;s,^.*size ,,;s, (.*$,,;s, ,,g;s,iB,,g'`
      pr="`df -h | grep -v 'loop\|changes\|live-rw-backing' |awk '/'$partition'/ {print $5}'`"
fr="`df -h | grep -v 'loop\|changes\|live-rw-backing' |awk '/'$partition'/ {print $4" free"}' | head -1`"
partition1="$all`df -h | grep -v 'loop\|changes\|live-rw-backing' |awk '/'$partition'/ {print "/"$4" free "$6}' | head -1`"
mntpoint="$(echo "$partition1" |awk '{print $3}' | head -1)"

if [ "$all" = "$partition1" ];then
  mkdir -p /$media/$partition
  [ "$fst" = "vfat" ] && MOPT1="$MOPT" || MOPT1=
  mount /dev/$partition /$media/$partition $MOPT1 > /dev/null 2>&1
  if [ $? -eq 0 ];then
      partition1="$all`df -h |awk '/'$partition'/ {print "/"$4" free"}'`"
   umount /dev/$partition && rmdir /$media/$partition
  fi
fi
      [ 0 -eq $i ] && echo "   <vbox>"

      case "$partition" in
         *fd*)
            type="gtk-floppy"
            ;;
         *mmc*)             type="media-memory-sd-symbolic"             ;;
         #*sda*)            type="drive24"            ;;
         *sr*)
            type="gtk-cdrom"
            ;;
         #*sd*)            type="gtk-cdrom"            ;;
         *)
            type="gtk-harddisk"
            ;;
      esac

      echo "<frame $partition: $fst $partition1>
 <hbox>
"

[ "`df |grep $partition`" ] && echo "
<button tooltip-text=\"$(gettext 'Browse')\">
   <input file icon=\"gtk-directory\"></input>
   <action>$FLMAN $mntpoint</action>
   <action>EXIT=ok</action>
</button>
"
echo "
			<progressbar>
				<label>$partition $pr $fr</label>
#				<input>echo "$pr"</input>
#				<action function=\"exit\">Ready</action>
			</progressbar>
" |egrep -v '^#'

      mountpoint -q "$mntpoint" > /dev/null 2>&1
      if [ 0 -eq $? ]
      then
#echo "type=$type" >>1111 ;type=gtk-harddisk
            echo "
   <pixmap><height>32</height><width>32</width><input file stock=\"$type\"></input></pixmap>
"

if [ "$partition" = "$HDRV" ] || [ "$partition" = "$BACKDRV" ]; then
export partition=$partitions
echo "
<button sensitive=\"false\" width-request=\"120\">
<label>$(gettext 'Unmount')</label>
   <input file stock=\"gtk-connect\"></input>
"
else
echo "
<button width-request=\"120\">
<label>$(gettext 'Unmount')</label>
   <input file stock=\"gtk-disconnect\"></input>
"
fi
echo "
      <action>echo $partition >/tmp/mount-wizard</action>
      <action>EXIT:U</action>
</button>
"
      else
         echo "
   <pixmap><height>32</height><width>32</width><input file icon=\"$type\"></input></pixmap>
<button width-request=\"120\">
   <width>128</width>
   <label>$(gettext 'Mount')</label>
   <input file stock=\"gtk-connect\"></input>
      <action>echo $partition >/tmp/mount-wizard</action>
      <action>EXIT:M</action>
</button>
"
      fi

      echo "
   </hbox>
</frame>"

      i=$((1 + $i))
      if [ 10 -eq $i ]
      then
         echo "</vbox>"
         i=0;
      fi
   done

   [ 0 -ne $i ] && echo "</vbox>"
echo "</hbox>"
echo "
<hbox>
<text><label>$(gettext 'Any grey Unmount button is boot or save partition.')</label></text>
<button tooltip-text=\"Мониторинг в трее\">
   <input file icon=\"desktop_tray\"></input>
   <action>traymnt</action>
</button>

<button tooltip-text=\"Обновить\">
   <input file stock=\"gtk-refresh\"></input>
   <action>EXIT:Cancel</action>
</button>
<button tooltip-text=\"Выход\">
   <input file stock=\"gtk-quit\"></input>
   <action>EXIT:abort</action>
</button>
</hbox>
"
echo "</vbox>"
echo "</window>"
}

# list all partitions
partitions=""
         partitions="$(blkid -o list | grep /dev | grep -E -v "loop|swap|crypt|squashfs" | sort | cut -d" " -f1 | sed 's|/dev/||g')"

# let the user choose a partition
eval $(_choose_action "$partitions" | gtkdialog -sc )
partition=`cat /tmp/mount-wizard`
case "$EXIT" in
	M)
	    fst="`blkid -o value -s TYPE "/dev/$partition"`"
    	    [ "$fst" = "vfat" ] && MOPT1="$MOPT" || MOPT1=
	    #ntf -i "fst $fst $MOPT1" &
	    mkdir /$media/$partition ; wt "mount /dev/$partition /$media/$partition $MOPT1"
		exec "$0"
		;;
	U)
	    #wt "umount /dev/$partition /$media/$partition" && rmdir /$media/$partition
	    wt "umount /dev/$partition" && rmdir /$media/$partition
		exec "$0"
		;;
esac
[ "abort" = "$EXIT" ] && exit 1
done
