Middagsgenerator
[einar-bin] / screenshot.sh
1 #!/bin/bash
2 # A quick and dirty script to take a screenshot, upload it via scp
3 # and put a link to it in the clipboard for easy pasting to i.e. IRC.
4 # It assumes a lot of things, e.g. that ssh to the target host has
5 # been set up with a working private key, and that xclip and
6 # gnome-screenshot is installed.
7 #
8 # If all you have is a hammer, everything looks like a nail.
9
10 # Settings
11 RemoteUser="einar"
12 RemoteHost="wowbagger.slaskete.net"
13 RemotePath="/var/www/eina.rjh.im/screenshots/"
14 LocalPath="${HOME}/ownCloud/Screenshots/"
15 UrlBase="https://eina.rjh.im/g"
16
17 function errcho() {
18 >&2 echo -e "$@"
19 }
20
21 function take_screenshot() {
22 FileName="$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 6 | head -n 1).png"
23 if [ "$@" = "clipboard" ]; then
24 xclip -out -selection clipboard -t image/png > "/tmp/${FileName}" 2>/dev/null
25 if [ "$(file -b --mime-type "/tmp/${FileName}")" == "image/png" ]; then
26 mv "/tmp/${FileName}" "${LocalPath}/"
27 else
28 rm "/tmp/${FileName}"
29 notify-send -i applets-screenshooter "screenshot.sh" "Tried to post image from clipboard, but found no image there."
30 fi
31 else
32 gnome-screenshot -f "${LocalPath}/${FileName}" -p "$@"
33 fi
34 if [ -f "${LocalPath}/${FileName}" ]; then
35 scp -q "${LocalPath}/${FileName}" "${RemoteUser}@${RemoteHost}:${RemotePath}"
36 echo -n "${UrlBase}/${FileName}"|xclip -selection p
37 echo -n "${UrlBase}/${FileName}"|xclip -selection c
38 notify-send -i applets-screenshooter "screenshot.sh" "Screenshot published to ${UrlBase}/${FileName}"
39 fi
40 }
41
42 function show_error() {
43 errcho "Please use one of: -w, --window, -a, --area, -s, --selection,"
44 errcho " -f, --full or no argument at all."
45 exit 1
46 }
47
48 case ${1} in
49 -w|--window)
50 take_screenshot -w -b -e shadow
51 ;;
52 -a|--area|-s|--selection)
53 sleep 0.2
54 take_screenshot -a
55 ;;
56 ""|-f|--full)
57 take_screenshot
58 ;;
59 -c|--clipboard|-p|--paste)
60 take_screenshot clipboard
61 ;;
62 *)
63 show_error
64 ;;
65 esac
66