#!/bin/bash
# A quick and dirty script to take a screenshot, upload it via scp
# and put a link to it in the clipboard for easy pasting to i.e. IRC.
# It assumes a lot of things, e.g. that ssh to the target host has
# been set up with a working logon with private key, and that xclip and
# gnome-screenshot is installed.
#
# If all you have is a hammer, everything looks like a nail.

# TODO:
# * Undo function
# * Check that we don't overwrite an existing file (theoretically possible, but unlikely)

# Settings
RemoteUser="einar"
RemoteHost="wowbagger.slaskete.net"
RemotePath="/var/www/eina.rjh.im/screenshots/"
LocalPath="${HOME}/Pictures/Screenshots/"
UrlBase="https://eina.rjh.im/g"

function errcho() {
  >&2 echo -e "$@"
}

function take_screenshot() {
  FileID="$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 6 | head -n 1)"
  FileName="${FileID}.png"
  if [ "$*" = "clipboard" ] || [ "$*" = "jpg" ]; then
    xclip -out -selection clipboard -t image/png > "/tmp/${FileName}" 2>/dev/null
    if [ "$(file -b --mime-type "/tmp/${FileName}")" == "image/png" ]; then
      if [ "$*" = "jpg" ]; then
        convert "/tmp/${FileName}" "/tmp/${FileID}.jpg"
        rm "/tmp/${FileName}"
        FileName="${FileID}.jpg"
      fi
      mv "/tmp/${FileName}" "${LocalPath}/"
    else
      rm "/tmp/${FileName}"
      notify-send -a "screenshot.sh" -i dialog-warning "Ooopsie" "Tried to post image from clipboard, but found no image there."
    fi
  else
    gnome-screenshot -f "${LocalPath}/${FileName}" -p "$@"
  fi
  if [ -f "${LocalPath}/${FileName}" ]; then
    scp -q "${LocalPath}/${FileName}" "${RemoteUser}@${RemoteHost}:${RemotePath}"
    echo -n "${UrlBase}/${FileName}"|xclip -selection p
    echo -n "${UrlBase}/${FileName}"|xclip -selection c
    notify-send -a "screenshot.sh" -i applets-screenshooter "Screenshot uploaded" "Screenshot published to ${UrlBase}/${FileName}"
  fi
}

function show_help() {
  errcho "Usage: screenshot.sh [option]"
  errcho "This script wraps gnome-screenshot in order to instantly upload screenshots and get back a URL for sharing to IRC or other social media"
  errcho "The default option is to take a screenshot of the whole screen."
  errcho "The following options are available:"
  errcho "-f, --full        Take screenshot of the whole screen"
  errcho "-w, --window      Take screenshot of the active window"
  errcho "-a, --area        Take screenshot of an area/selection"
  errcho "-s, --selection   Take screenshot of an area/selection"
  errcho "-c, --clipboard   Upload image from paste buffer if present"
  errcho "-p, --paste       Upload image from paste buffer if present"
  errcho "-j, --jpg, --jpeg Convert image from paste buffer to jpeg and upload"
  errcho "-h, --help        Show this help"
  exit 1
}

case ${1} in
  -w|--window)
    take_screenshot -w -b
    ;;
  -a|--area|-s|--selection)
    sleep 0.2
    take_screenshot -a
    ;;
  ""|-f|--full)
    take_screenshot
    ;;
  -c|--clipboard|-p|--paste)
    take_screenshot clipboard
    ;;
  -j|--jpg|--jpeg)
    take_screenshot jpg
    ;;
  -h|--help)
    show_help
    ;;
  *)
    errcho "ERROR: Unknown option."
    show_help
    ;;
esac

