#!/bin/bash

# screeny 2.0 - mail AT patrick-nagel DOT net

# COMPATIBLE WITH KDE >=4.4 ONLY
# for a KDE 3.x compatible version, please see
# http://patrick-nagel.net/scripts/screeny/old/screeny.sh

# 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 ###

# server's hostname/ip
HOST="your.server.com"

# default: 22
SSHPORT="22"

# remote user's name
USER="serveruser"

# directory on server
DIR="/var/www/whatever/htdocs/screenshots"

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

# JPEG quality in percent
JPGQUALITY="85"

# 1 = use TinyURL service
USETINYURL="1"

# See 'man strftime' for possible variables. '%s' is timestamp.
FILENAMESTRING="%s"


function take_screenshot() {

        echo -n "Taking screenshot... "
        NAME="$(scrot $scrotopts "${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}" && \
        ssh -p "${SSHPORT}" "${USER}@${HOST}" mkdir -p ${DIR} && \
        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 "<b>http://tinyurl.com" | sed -e 's|.*>http|http|' -e 's|</b>.*||')" && \
                SHORTURL2="$(curl -s "http://tinyurl.com/create.php?url=${LONGURL2}" |\
                grep "<b>http://tinyurl.com" | sed -e 's|.*>http|http|' -e 's|</b>.*||')" && \
                echo "done."
        }
        else
        {
                echo -n "Creating URLs... "
                SHORTURL1="${LONGURL1}"
                SHORTURL2="${LONGURL2}"
                echo "done."
        }
        fi && \

        return 0;

}

function put_into_clipboard() {

        specific_clipboard && \
        echo "done." && \
        return 0;

}

function notify_user() {

        specific_notify && \
        echo "done." && \
        return 0;

}

function specific_clipboard () {
        echo -n "Putting text into klipper... "
        dbus-send --session --dest=org.kde.klipper --type=method_call /klipper \
                        org.kde.klipper.klipper.setClipboardContents \
                        string:"Screenshot: ${SHORTURL1} (JPEG version) / ${SHORTURL2} (PNG version)"
}

function specific_notify () {
        echo -n "Notifying user through kdialog... "
        kdialog --passivepopup "Screenshot uploaded to "${HOST}:${SSHPORT}"." 5
}

local=false
for param in $@; do
        case $param in
                --select|-s) scrotopts="-s";;
                --local|-l) local=true;;
                *) echo "unknown option $param" && exit 6;;
        esac
done

take_screenshot || { echo "A problem occurred while trying to take a screenshot (see above). Exiting."; exit 1; }
$local && exit 0
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; }
