#!/bin/bash

function uploadImage {

	cd "${HOME}"

default_client_id=c9a6efb3d7932fd
client_id="${IMGUR_CLIENT_ID:=$default_client_id}"

url=$(curl -s -H "Authorization: Client-ID $client_id" -H "Expect: " -F "image=$1" https://api.imgur.com/3/image.xml)
IMGVIEW=$(grep -E -o "https://i.imgur.com/[^<]*" <<< ${url[@]})

if [[ "$?" != "0" ]]; then
    zenity --error --title="Upload error" --text="\nCould not send screenshot to imgur.com" 2>/dev/null
    exit 1
fi 
echo 100  # 100 to auto-kill the progress bar

/usr/bin/firefox "$IMGVIEW"
}

if zenity --question --width="340" --height="140" --title="Screenshot Upload" \
		  --text="\nLite Control Center screenshot has been saved to:\n<b>${HOME}/liteccshot.png</b>. \
\n\nLinux Lite can now upload this screenshot to imgur.com online image hosting service for you. \
\n\nWould you like to upload it?" 2>/dev/null; then
	uploadImage "@liteccshot.png" | zenity --progress --pulsate --auto-close --auto-kill --title="Uploading Image" \
												  --no-cancel --text="\n      Sending image to imgur.com. Please wait...      " 2>/dev/null
	rm "${HOME}/liteccshot.png"
fi
exit 0