#!/bin/sh
#(c) Copyright Barry Kauler, Oct. 2011, bkhome.org
#2011 license GPL3 (see /usr/share/doc/legal)
#called via menu or autodetected when camera plugged in (via /usr/sbin/pupautodetect).
#usbutils optionally used if installed, lsusb accesses usb.ids to obtain device description.
#pupcamera is not a separate application, it is builtin to Woof so is in all builds
# -- it detects if gphoto (and deps) not installed and informs the user.
#111010 first release.
#111015 detect if gphotofs not installed.
#120202 BK: internationalized.
#130208 rodin.s: missing translation.
#130326 note: DEVICE variable no longer in 3.5+ kernel, as /proc/bus/usb removed.
#131128 zigbert: gui (gtkdialog) improvements.
#140128 01micko, bugfix "Open" button and tame 88-puppy-autodetect.rules

PIDOFMTP=`pidof mtpdevice`
[ "$PIDOFMTP" ] && exit # exits if mtpdevice process starts to tame udev rule 88-puppy-autodetect.rules

export TEXTDOMAIN=pupcamera
export OUTPUT_CHARSET=UTF-8

mkdir -p /mnt/camera1
mkdir -p /mnt/camera2
umount /mnt/camera1 2>/dev/null #precaution.
umount /mnt/camera2 2>/dev/null #precaution.

#as this app can be run from both menu and auto-detect plugging-in, kill any previous...
ALLPS="`ps`"
OLDPIDS="`echo "$ALLPS" | grep 'gtkdialog' | grep 'PUPCAMERA_DLG' | tr -s ' ' | sed -e 's%^ %%' | cut -f 1 -d ' ' | tr '\n' ' '`"
kill $OLDPIDS 2>/dev/null

#find BUSNUM, DEVNUM...
BUSNUM=""; DEVNUM=""; PRODUCTDESC=""
if [ "$DEVPATH" ];then
 [ -e /sys${DEVPATH}/product ] && PRODUCTDESC="`cat /sys${DEVPATH}/product`"
 if [ "$DEVTYPE" = "usb_interface" ];then
  UPDEVPATH=$DEVPATH
  while [ "$UPDEVPATH" != "/" ];do
   UPDEVPATH="`dirname $UPDEVPATH`"
   case $UPDEVPATH in
    *usb*) #this is to avoid going too high.
     [ ! -e /sys${UPDEVPATH}/busnum ] && continue
     [ ! -e /sys${UPDEVPATH}/devnum ] && continue
     BUSNUM="00`cat /sys${UPDEVPATH}/busnum`"
     DEVNUM="00`cat /sys${UPDEVPATH}/devnum`"
     BUSNUM="`echo -n "$BUSNUM" | rev | cut -c 1,2,3 | rev`" #must be in 3-digit format, ex: 002
     DEVNUM="`echo -n "$DEVNUM" | rev | cut -c 1,2,3 | rev`" #must be in 3-digit format, ex: 012
     [ ! "$PRODUCTDESC" ] && [ -e /sys${UPDEVPATH}/product ] && PRODUCTDESC="`cat /sys${UPDEVPATH}/product`"
     break
    ;;
    *)
     break
    ;;
   esac
  done
 fi
fi

echo -n "" > /tmp/pupcamera_ports
CAMERAINFO=""
if [ "$BUSNUM" ];then
 
 #get product description from lsusb (usbutils pkg)...
 VENDORINSERT=""
 if which lsusb;then
  if [ "`lsusb --help 2>&1 | grep -i 'busybox'`" = "" ];then #want the full lsusb
   VENDORDESC="`lsusb -s ${BUSNUM}:${DEVNUM} | tr -s ' ' | cut -f 3-9 -d ':' | cut -f 2-99 -d ' '`"
   if [ "$VENDORDESC" ];then
    VENDORINSERT="$(gettext 'The camera has identified itself as:')
 <b>${VENDORDESC}</b>
"
   fi
  fi
 fi
 
 CAMERAINFO="$(gettext 'A camera is detected connected via USB.')
$(gettext 'The Linux kernel has identified it as:')
 $(gettext 'Type:') <b>${PRODUCTDESC}</b>
 $(gettext 'Connected on port: usb:')${BUSNUM},${DEVNUM}
${VENDORINSERT}"
 echo "usb:${BUSNUM},${DEVNUM}" >> /tmp/pupcamera_ports
fi

PROBEINFO=""
if [ "`which gphoto2`" ];then
 GPHOTOPROBE="`gphoto2 --auto-detect | tr -s ' ' | tail -n +3`"
 if [ "$GPHOTOPROBE" ];then
  PROBEINFO="$(gettext "A probe by the 'gphoto2' utility has detected a camera, so it is likely that this camera will be accessable. Results of probe:")
<b>${GPHOTOPROBE}</b>"
  PROBEPORTS="`echo "$GPHOTOPROBE" | sed -e 's% *$%%' | rev | cut -f 1 -d ' ' | rev`"
  echo "$PROBEPORTS" >> /tmp/pupcamera_ports
 else
  if [ -s /tmp/pupcamera_ports ];then
   PROBEINFO="<b>$(gettext "No camera is detected.")</b>
$(gettext "It means that the Gphoto software in puppy does not recognise the camera. (But, you can try, click the 'Open' button)")"
  else
   PROBEINFO="<b>$(gettext "No camera is detected.")</b>
$(gettext "It means that the Gphoto software in puppy does not recognise the camera.")"
  fi
 fi
else
 #gphoto (and deps) not installed...
 PROBEINFO="<b>$(gettext "The 'gphoto' application is not installed.")</b>
$(gettext "That means PupCamera cannot access the photos on a digital camera. Please run the Puppy Package Manager (see 'install' icon on desktop) to install 'gphotofs' and it's dependencies.")"
 echo -n "" > /tmp/pupcamera_ports #remove button.
fi

if [ "`which gphotofs`" = "" ]; then #111015
 PROBEINFO="<b>$(gettext "The 'gphotofs' application is not installed.")</b>
$(gettext "That means PupCamera cannot access the photos on a digital camera. Please run the Puppy Package Manager (see 'install' icon on desktop) to install 'gphotofs' and it's dependencies.")"
fi

func_splash(){
	/usr/lib/gtkdialog/box_splash -placement center -timeout 5 -fontsize large -text "$(gettext 'Please wait...')" &
}
export -f func_splash
if [ -s /tmp/pupcamera_ports ];then
 CAMCNT=1
 for ONEPORT in `cat /tmp/pupcamera_ports | sort -u | tr '\n' ' '`
 do
  mkdir -p /mnt/camera${CAMCNT} #"${OPENBUTTON}"
  OPENBUTTON="${OPENBUTTON}"'
  <button space-expand="false" space-fill="false">
   '"`/usr/lib/gtkdialog/xml_button-icon open`"'
   <label>'$(gettext 'Open')' '${ONEPORT}'</label>
   <action>gphotofs --port='${ONEPORT}' /mnt/camera'${CAMCNT}'</action>
   <action>func_splash</action>
   <action>rox -x /mnt/camera'${CAMCNT}' -d /mnt/camera'${CAMCNT}' & </action>
   <action>enable:BTN_CLOSE</action>
  </button>'
  CAMCNT=$(($CAMCNT + 1))
 done
 ICON=photocamera_yes.svg
else
 OPENBUTTON='<button space-expand="false" space-fill="false">'"`/usr/lib/gtkdialog/xml_button-icon open`"'<label>'$(gettext 'Open camera')'</label><sensitive>falsw</sensitive></button>'
 ICON=photocamera_no.svg
fi

[ -s /tmp/pupcamera_ports ] && MSG2="$(gettext "click 'Open' button to access photos on your camera. A ROX-Filer window will pop up, and if you are then able to see your photos, it worked.")" || MSG=""

#main gui
S='
<window title="PupCamera - '$(gettext 'Access digital camera')'" icon-name="gtk-orientation-landscape">
<vbox>
  <frame '$(gettext 'Detected camera')'>
    '"`/usr/lib/gtkdialog/xml_info scale "$ICON" 60 "${CAMERAINFO}" "${PROBEINFO}" " " "$MSG2" "$(gettext "Please click the 'Close camera' button when you have finished accessing your camera...")"`"' 
    <text height-request="20"><label>""</label></text>
    <hbox homogeneous="true">
      <hbox space-expand="false" space-fill="false">
        '${OPENBUTTON}'
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon close`"'
          <label>'$(gettext 'Close camera')'</label>
          <variable>BTN_CLOSE</variable>
          <action>rox -D /mnt/camera1</action>
          <action>umount /mnt/camera1</action>
          <action>rox -D /mnt/camera2</action>
          <action>umount /mnt/camera2</action>
          <sensitive>false</sensitive>
	    </button>
      </hbox>
    </hbox>
    <text height-request="5"><label>""</label></text>
  </frame>

  <hbox space-expand="false" space-fill="false">
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon refresh`"'
      <label>'$(gettext 'Rescan')'</label>
      <action>EXIT:rescan</action>
    </button>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
      <label>'$(gettext 'Quit')'</label>
      <action>EXIT:finished</action>
    </button>
  </hbox>
</vbox>
</window>'
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
export PupCamera="$S"

RETVALS="`gtkdialog -p PupCamera`"

umount /mnt/camera1 2>/dev/null #precaution.
umount /mnt/camera2 2>/dev/null #precaution.

[ "`echo "$RETVALS" | grep 'rescan'`" != "" ] && exec pupcamera #reenter.

###END###
