#!/bin/sh
#pupmtp written by mistfire. manage mtp devices.
#20201205 imported from woof-ce, adapted for easyos.

#launched via udev rule in /etc/udev/rules.d/88-puppy-autodetect.rules
#see also /usr/sbin/pupautodetect and autodetect-run

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

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

which simple-mtpfs >/dev/null
if [ $? -ne 0 ];then
 /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 it's dependencies.")"
 exit 1
fi

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

simple-mtpfs -l | sed -e "s#\ #_#g" -e "s#^_##g" -e "s#:_#:#g"> /tmp/mtp-devices
sync
content=`cat /tmp/mtp-devices`
kill $xPID

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 /mnt/mtpdev${MTPCNT}`
 
 if [ "$ONEMNTPT" = "" ];then #not mounted.
  PARTSGUI="${PARTSGUI}<hbox><text><label>${DEVNAME} (MTP ${MTPCNT})</label></text><button><label>MOUNT</label><action>mkdir /mnt/mtpdev${MTPCNT}</action><action>simple-mtpfs --device $portmtp /mnt/mtpdev${MTPCNT}</action><action>EXIT:refresh</action></button><button><input file>${MINIFOLDERXPM}</input><label>OPEN</label><action>rox -d /mnt/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 /mnt/mtpdev${MTPCNT}</action><action>rmdir /mnt/mtpdev${MTPCNT}</action><action>EXIT:refresh</action></button><button><input file>${MINIFOLDERXPM}</input><label>OPEN</label><action>rox -d /mnt/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 -o "refresh"`

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

###END###
