#!/bin/bash

# screeny 1.2.1 - mail AT patrick-nagel DOT net

# Dependencies:
# - On this machine: ssh, scrot, curl, KDE (klipper, kdialog)
# - On the server:   sshd, httpd, ImageMagick
# - Working ssh public key authentication for the user that will
#   do the upload (if you want to run the script non-interactively)


### Configuration ###

HOST="your.server.com"				# server's hostname/ip
SSHPORT="22"					# default: 22
USER="serveruser"				# remote user's name
DIR="/var/www/whatever/htdocs/screenshots"	# directory on server

URLHOST="http://www.your.server.com/screens/"	# base URL (with trailing slash)

JPGQUALITY="85"					# JPEG quality in percent

USETINYURL="1"					# 1 = use TinyURL service
FILENAMESTRING="%s"				# See 'man strftime' for possible
						# variables. '%s' is timestamp.

### End of configuration ###



function take_screenshot() {

	echo -n "Taking screenshot... "
	NAME="$(scrot "${FILENAMESTRING}.png" -e 'echo $n')" && \
	echo "done." && \

	return 0;

}

function upload_and_convert() {

	# Upload the png image to the specified server and
	# convert it from png to jpg after uploading.
	# Finally, delete the local image.

	echo -e "\nSummary:\n"
	echo "Filename:            ${NAME}"
	echo "Resulting URL (png): ${SHORTURL2}"
	echo "Resulting URL (jpg): ${SHORTURL1}"
	echo "Uploading to:        ${HOST}:${SSHPORT}:${DIR}"
	echo

	echo -n "Uploading... " 
	chmod a+r "${NAME}" && \
	scp -q -P "${SSHPORT}" -p "${NAME}" "${USER}@${HOST}:${DIR}${NAME}" && \
	echo "done." && \

	echo -n "Converting on server... " && \
	ssh -p "${SSHPORT}" "${USER}@${HOST}" convert -quality "${JPGQUALITY}" "${DIR}${NAME}" "${DIR}${NAMEJPG}" && \
	echo "done." && \

	echo -n "Deleting local copy... " && \
	rm -f "${NAME}" && \
	echo "done." && \

	return 0;

}

function make_url() {

	NAMEJPG="${NAME%.*}.jpg"
	LONGURL1="${URLHOST}${NAMEJPG}"
	LONGURL2="${URLHOST}${NAME}"

	if [[ "${USETINYURL}" == "1" ]]; then
	{
		# Make TinyURLs from the resulting URLs for the png and jpg images
	
		echo -n "Creating TinyURLs... "
		[ -x /usr/bin/curl ] || { echo "This script needs curl."; return 1; }
		SHORTURL1="$(curl -s "http://tinyurl.com/create.php?url=${LONGURL1}" |\
	          grep "\[.*\]" |\
	          sed -e 's|^.*\[<a href=\"http\://tinyurl\.com/|http\://tinyurl.com/|' \
	              -e 's|\".*$||')" && \
		SHORTURL2="$(curl -s "http://tinyurl.com/create.php?url=${LONGURL2}" |\
	          grep "\[.*\]" |\
	          sed -e 's|^.*\[<a href=\"http\://tinyurl\.com/|http\://tinyurl.com/|' \
	              -e 's|\".*$||')" && \
		echo "done."
	}
	else
	{
		echo -n "Creating URLs... "
		SHORTURL1="${LONGURL1}"
		SHORTURL2="${LONGURL2}"
		echo "done."
	}
	fi && \

	return 0;

}

function put_into_clipboard() {

	echo -n "Putting text into clipboard... "
	dcop klipper klipper setClipboardContents "Screenshot: ${SHORTURL1} (JPEG version) / ${SHORTURL2} (PNG version)" && \
	echo "done." && \
	return 0;

}

function notify_user() {

	echo -n "Notifying user through kdialog... "
	kdialog --passivepopup "Screenshot uploaded to "${HOST}:${SSHPORT}"." 5 && \
	echo "done." && \
	return 0;

}


# main

take_screenshot || { echo "A problem occurred while trying to take a screenshot (see above). Exiting."; exit 1; }
make_url || { echo "A problem occurred while trying to make URLs (see above). Exiting."; exit 3; }
upload_and_convert || { echo "A problem occurred while uploading/converting (see above). Exiting."; exit 2; }
put_into_clipboard || { echo "A problem occured while trying to put the URLs into the clipboard (see above). Exiting."; exit 4; }
notify_user || { echo "A problem occured while trying to notify via kdialog (see above). Exiting."; exit 5; }
