#!/bin/sh

# Puush for FreeBSD
# Owner : Sunmock Yang
# www.sunmock.com
# May 2014
#
# Dependencies:
# - gnome-screenshot
# - curl
# - zenity
# - xclip
# - notify-send
#
# Licence : Beerware
#
# Instructions:
# - Add your puush API key to PUUSH_API_KEY (You can find your API key at http://puush.me/account/settings)
# - Set up keyboard shortcuts within FreeBSD
#
# 		command			description		(recommended keyboard shortcut)
#		---------------------------------------------------------------
#		puush -w		puush window	(Ctrl + Shift + 2/@)
#		puush -d		puush desktop	(Ctrl + Shift + 3/#)
#		puush -a		area puush		(Ctrl + Shift + 4/$)
#		puush -f		file upload		(Ctrl + Shift + U)
#
#
# Notes:
# - Link(s) will be copied into clipboard and appear in notifications
# - puush curl upload code borrowed from online sources

# Usage: puushFile [fileName]

. ~/.config/puush

puushFile() {
	if [ -z "$1" ]; then
		echo "No file specified"
		exit 1
	elif [ ! -f "$1" ]; then
		echo "Puush cancelled"
		exit 1
	fi

	fileURL=`curl "https://puush.me/api/up" -# -F "k=$PUUSH_API_KEY" -F "z=waifu" -F "f=@$1" | sed -E 's/^.+,(.+),.+,.+$/\1\n/ ; s/http\:/https\:/'`
	echo ${fileURL}

	if [ ! -z "$fileURL" ]; then
		#Copy link to clipboard, show notification
		printf $fileURL | xclip -selection "clipboard"
		notify-send -i "/usr/local/share/icons/puush.png" -t 2000 "puush complete!" "$fileURL"
	fi
}

helpText() {
  printf "____________ puush for FreeBSD ____________\n"
  printf "Created by Sunmock Yang using the puush api\n"
  printf "\n"
  printf "Usage:\n"
  printf "  puush [OPTIONS] [PATH]\n"
  printf "\n"
  printf "OPTIONS:\n"
  printf "  -d                 puush entire desktop\n"
  printf "  -a                 select area to puush\n"
  printf "  -w                 puush current window\n"
  printf "  -f                 puush specific file (opens file dialog)\n"
  printf "\n"
  printf "  --help,-h          show this page\n"
  printf "\n"
  printf "PATH:\n"
  printf "  PATH               optional: path of file to puush\n"
}

generateFileName() {
echo "/tmp/puush ($(date +"%Y-%m-%d at %I.%M.%S")).png";
}


if [ -z "$PUUSH_API_KEY" ]; then
  echo "Set the variable PUUSH_API_KEY in ~/.config/puush"
  echo "You can find your API key at http://puush.me/account/settings"

  notify-send -i "/usr/local/share/icons/puush.png" "Set the variable PUUSH_API_KEY in $0" "You can find your API key at http://puush.me/account/settings"

  exit 1

elif [ -z "$1" ]; then
	echo "No file entered."
	helpText
  exit 1

fi

#Get file to puush and puush it
case "$1" in
	-d)
		echo "Whole Desktop"
			fileName=$(generateFileName)
			gnome-screenshot -f "$fileName"
			puushFile "$fileName"
		;;

	-a)
		echo "Area"
			fileName=$(generateFileName)
			gnome-screenshot -a -f "$fileName"
			puushFile "$fileName"
		;;

	-w)
		echo "Window"
			fileName=$(generateFileName)
			gnome-screenshot -w -f "$fileName"
			puushFile "$fileName"
		;;

	-f)
		echo "File Upload"
			fileName=`zenity --file-selection`
			puushFile "$fileName"
		;;

	-h|--help)
		helpText
		exit 0
		;;
		
	*)
		echo "Upload $1"
			puushFile "$1"
		;;
		
esac
