#!/bin/bash
#PupSnap Image Hosting Module
#Upload images to www.imgur.com image hosting service
#Clipboard code by Stu91
#20140621 gettext (vicmz)

export TEXTDOMAIN=pupsnap2
export OUTPUT_CHARSET=UTF-8

WORKDIR="/usr/local/PupSnap"

[ ! "`which curl`" ] && Xdialog --title Alert --beep --msgbox "\n  "$(gettext "The 'curl client' is required for uploading")"  \n  $(gettext 'images but is not available in this distro!')  \n" 0 0 && exit 0
[ "$IMAGE_UPLOAD" = "" -o "$IMAGE_UPLOAD" = "No file selected" ] && Xdialog --title Alert --beep --msgbox "\n    $(gettext 'No file selected!')    \n" 0 0 && exit 0
IMAGE_FILE=`cat $WORKDIR/imgur-file`
[ ! -s "$IMAGE_FILE" ] && Xdialog --title Alert --beep --msgbox "\n    $(gettext 'The selected image file is not available!')    \n" 0 0 && exit 0

#define gtkdialog:
for g in gtk2dialog gtkdialog4 gtkdialog; do [ "`which $g 2>/dev/null`" ] && { GTKDIALOG="$g"; break; } done

#Upload splash:
echo 0 > /tmp/imagehost-upload
export UPLOAD_IMG=`echo "${IMAGE_UPLOAD##*/}"`
export IMAGEHOST_DIALOG='
<window title="PupSnap" icon-name="pupsnap" resizable="false" decorated="true">'"
<vbox>
 <pixmap><input file>$WORKDIR/icons/pupsnap.svg</input><height>98</height><width>125</width></pixmap>
 <text use-markup=\"true\"><label>\"<b><span size='"'medium'"'>             $(gettext 'Uploading Image...')          </span></b>\"</label></text>"'
 <text><label>'"$UPLOAD_IMG"'</label></text>
 <progressbar visible="false">
  <input>while [ "$M" != "100" ]; do M=`cat /tmp/imagehost-upload`; echo $M; usleep 500000; done</input>
  <action type="exit">Ready</action>
 </progressbar>
</vbox></window>'
$GTKDIALOG --geometry +150+150 -p IMAGEHOST_DIALOG &

# IMPORTANT: The following API key is registered with imgur.com for PupSnap. Please *do not copy* the key for use in other applications.
PUPSNAP_API="Authorization: Client-ID aac46ee4f6dae6f"

#upload image and confirm imgur response:
response=$(curl -k -H "$PUPSNAP_API" -H "Expect: " -F "image=@$IMAGE_UPLOAD" https://api.imgur.com/3/upload.xml 2>/dev/null)
if [ $? -ne 0 ]; then
	Xdialog --title Alert --beep --msgbox "\n    $(gettext 'Failed connection to imgur.com!') \n\n    $(gettext 'Are you or the Imgur service currently offline?')    \n" 0 0 && echo 100 > /tmp/imagehost-upload && exit 0
fi

overCapacity=$(echo $response | grep "Imgur is over capacity")
if [ "$overCapacity" ]; then
	Xdialog --title Alert --beep --msgbox "\n    $(gettext 'imgur.com is currently over capacity!')    \n\n    $(gettext 'Please retry later.')    \n" 0 0 && echo 100 > /tmp/imagehost-upload && exit 0
fi

uploadSUCCESS=$(echo $response | grep "status=\"200\"")
if [ "$uploadSUCCESS" = "" ]; then
	imgurERRORCODE=$(echo $response | sed -r 's/.*<status>(.*)<\/status>.*/\1/')
	Xdialog --title Alert --beep --msgbox "\n    $(gettext 'Imgur error code:')    \n\n    $imgurERRORCODE    \n\n    http://api.imgur.com/errorhandling    \n" 0 0 && echo 100 > /tmp/imagehost-upload && exit 0
fi

#parse imgur response:
URL=$(echo $response | sed -r 's/.*<link>(.*)<\/link>.*/\1/')
deleteHash=$(echo $response | sed -r 's/.*<deletehash>(.*)<\/deletehash>.*/\1/')
deleteURL=$(echo http://imgur.com/delete/$deleteHash)
smallTHUMB=$(echo $URL | sed 's/\(.*\)\./\1s\./')
largeTHUMB=$(echo $URL | sed 's/\(.*\)\./\1l\./')

#send imgur response to GUI:
echo "$URL" > /tmp/PS-uploadlink
echo "defaultbrowser $URL" > /tmp/PS-browse_uploadlink
chmod 755 /tmp/PS-browse_uploadlink

echo "$smallTHUMB" > /tmp/PS-smallthumb
echo "defaultbrowser $smallTHUMB" > /tmp/PS-browse_smallthumb
chmod 755 /tmp/PS-browse_smallthumb

echo "$largeTHUMB" > /tmp/PS-largethumb
echo "defaultbrowser $largeTHUMB" > /tmp/PS-browse_largethumb
chmod 755 /tmp/PS-browse_largethumb

echo "$deleteURL" > /tmp/PS-deletelink
echo "defaultbrowser $deleteURL" > /tmp/PS-browse_deletelink
chmod 755 /tmp/PS-browse_deletelink

#append imgur response to activity log:
CURRENT_TIME=`date +%F_%R:%S`
echo >> $WORKDIR/imgur.log
echo "$(gettext 'Upload Date:') $CURRENT_TIME" >> $WORKDIR/imgur.log
echo "$(gettext 'Local Image:') $IMAGE_UPLOAD" >> $WORKDIR/imgur.log
echo "$(gettext 'Online Image:') $URL" >> $WORKDIR/imgur.log
echo "$(gettext 'Small Thumb:') $smallTHUMB" >> $WORKDIR/imgur.log
echo "$(gettext 'Large Thumb:') $largeTHUMB" >> $WORKDIR/imgur.log
echo "$(gettext 'Delete Link:') $deleteURL" >> $WORKDIR/imgur.log

#create online-image-string for clipboard:
CLIP="$CLIP$URL"

# send online-image URL to clipboard if xsel or xclip are available:
if [ $DISPLAY ]; then
	{ type xsel >/dev/null 2>/dev/null && echo -n $CLIP | xsel; } \
		|| { type xclip >/dev/null 2>/dev/null && echo -n $CLIP | xclip; } \
		|| echo "$(gettext 'Image not copied to the clipboard: no xsel or xclip')" >&2
else
	echo "$(gettext 'Image not copied to the clipboard: no') \$DISPLAY" >&2
fi

echo 100 > /tmp/imagehost-upload

exit 0
