#!/bin/sh
#pupmtp written by mistfire. Interface and behavior based from pmount for Puppy 4.0
#manage mtp devices

#using ${$} which is pid of script...
MYPID=${$}

. /etc/rc.d/PUPSTATE #v3.96

[ ! -e /media ] && mkdir /media

MINIFOLDERXPM='/usr/local/lib/X11/mini-icons/mini-filemgr.xpm'

ALLPS="`ps`"
OLDPIDS="`echo "$ALLPS" | grep 'gtkdialog' | grep 'PMTPGUI' | tr -s ' ' | sed -e 's%^ %%' | cut -f 1 -d ' ' | tr '\n' ' '`"
kill $OLDPIDS 2>/dev/null

/usr/lib/gtkdialog/box_splash -close never -text "Probing MTP devices..." &
xPID=$!
sleep 3s
kill $xPID

if [ "`which simple-mtpfs`" ];then
simple-mtpfs -l | sed -e "s#\ #_#g" -e "s#^_##g" -e "s#:_#:#g"> /tmp/mtp-devices
else
/usr/lib/gtkdialog/box_ok "PupMTP" error "<b>$(gettext "The 'simple-mtpfs' application is not installed.")</b>" "$(gettext "That means PupMTP cannot access the files on a mtp device. Please run the Puppy Package Manager (see 'install' icon on desktop) to install 'simple-mtpfs' and its dependencies.")"
exit
fi

content=`cat /tmp/mtp-devices`

MTPCNT=1

for d1 in `cat /tmp/mtp-devices | sort`;
do

portmtp=`echo "$d1" | cut -f 1 -d ":"`

echo $portmtp

DEVNAME=`echo "$d1" | cut -f 2 -d ":"`
 
ONEMNTPT=`mount | grep /media/mtpdev${MTPCNT}`
 
 if [ "$ONEMNTPT" = "" ];then #not mounted.
  PARTSGUI="${PARTSGUI}<hbox><text><label>${DEVNAME} (MTP ${MTPCNT})</label></text><button><label>MOUNT</label><action>mkdir /media/mtpdev${MTPCNT}</action><action>simple-mtpfs --device $portmtp /media/mtpdev${MTPCNT}</action><action>EXIT:refresh</action></button><button><input file>${MINIFOLDERXPM}</input><label>OPEN</label><action>rox -d /media/mtpdev${MTPCNT}&</action><sensitive>false</sensitive></button></hbox>"
 else
  #<input file>${DRIVEXPM}</input>
  PARTSGUI="${PARTSGUI}<hbox><text><label>${DEVNAME} (MTP ${MTPCNT})</label></text><button><label>UNMNT</label><action>umount /media/mtpdev${MTPCNT}</action><action>rmdir /media/mtpdev${MTPCNT}</action><action>EXIT:refresh</action></button><button><input file>${MINIFOLDERXPM}</input><label>OPEN</label><action>rox -d /media/mtpdev${MTPCNT}&</action><sensitive>true</sensitive></button></hbox>"
 fi

MTPCNT=`echo $(expr $MTPCNT + 1)`

done

if [ "$PARTSGUI" == "" ]; then
PARTSGUI="<text><label>NO MTP DEVICES DETECTED</label></text>"
fi


export PMTPGUI="
<window title=\"PupMTP - '$(gettext 'Access MTP devices')'\" icon-name=\"gtk-connect\" resizable=\"false\">
 <vbox height-request=\"300\" width-request=\"500\">
  <vbox>
  `/usr/lib/gtkdialog/xml_info fixed phone_mobile.svg 40 \"<b>DO NOT REMOVE MOUNTED MEDIA</b>\" \"$(gettext 'You may have to close your file manager to close the MTP device correctly')\"`
  </vbox>
  <frame Detected MTP devices>
  <vbox scrollable=\"true\" space-expand=\"true\">
  ${PARTSGUI}
  </vbox>
  </frame>
  <hbox>
   <button><input file stock=\"gtk-refresh\"></input><label>REFRESH</label>
    <action type=\"exit\">refresh</action>
   </button>
   <button><input file stock=\"gtk-quit\"></input><label>QUIT</label></button>
  </hbox>
 </vbox>
</window>"


RETPARAMS="`gtkdialog --program=PMTPGUI --center`"

rm -f /tmp/mtp-devices

#echo "$RETPARAMS"

result=`echo $RETPARAMS | grep "refresh"`

if [ "$result" != "" ]; then
 $0
fi


###END###