#!/usr/bin/env bash

if [ -z "$1" ]
then
    echo "Usage: $0 <glob_path>"
    exit 1
fi

if [ -z "$IMGUR_API_KEY" ]
then
    echo "To use screenshots uploader, it is essential to set up a variable with imgur api key"
    echo "Run:"
    echo "$ export IMGUR_API_KEY=\"your api key\""
    exit 1
fi

echo "Uploading screenshots..."

for image in $1
do
    if [ ! -e "$image" ]
    then
        break
    fi

    url=`curl -s -# -F "image"=@"$image" -F "key"="$IMGUR_API_KEY" imgur.com/api/upload.xml \
     | grep -Eo '<[a-z_]+>http[^<]+' \
     | sed 's/^<.\|_./\U&/g;s/_/ /;s/<\(.*\)>/\x1B[0;34m\1:\x1B[0m /' \
     | grep Original \
     | awk '{ print $3 }'`
    echo "$image - $url"
done
