Automatisk lÄsing av skjerm
[einar-bin] / screenshot.sh
index 858797a30c48bb7cd801bd4e4ef2006b74ebdb49..2649ddf99e4359540b98b5045b8c6710d37eef85 100755 (executable)
@@ -1,35 +1,53 @@
 #!/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 
+# It assumes a lot of things, e.g. that ssh to the target host has
 # been set up with a working private key, and that xclip and
 # gnome-screenshot is installed.
 #
 # If all you have is a hammer, everything looks like a nail.
 
 # Settings
-REMOTEUSER="einar"
-REMOTEHOST="wowbagger.slaskete.net"
-REMOTEPATH="/var/www/eina.rjh.im/screenshots/"
-LOCALPATH="${HOME}/ownCloud/Screenshots/"
-URLBASE="https://eina.rjh.im/screenshots"
+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() {
-  FILENAME="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 6 | head -n 1).png"
-  gnome-screenshot -f "${LOCALPATH}/${FILENAME}" -p "$@"
-  scp -q "${LOCALPATH}/${FILENAME}" "${REMOTEUSER}@${REMOTEHOST}:${REMOTEPATH}"
-  printf "${URLBASE}/${FILENAME}"|xclip -selection p
-  printf "${URLBASE}/${FILENAME}"|xclip -selection c
-  notify-send -i applets-screenshooter "screenshot.sh" "Screenshot published to ${URLBASE}/${FILENAME}"
+  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 -i applets-screenshooter "screenshot.sh" "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 -i applets-screenshooter "screenshot.sh" "Screenshot published to ${UrlBase}/${FileName}"
+  fi
 }
 
 function show_error() {
   errcho "Please use one of: -w, --window, -a, --area, -s, --selection,"
-  errcho "                   -f, --full or no argument at all."
+  errcho "                   -f, --full, -j, --jpg or no argument at all."
   exit 1
 }
 
@@ -38,11 +56,18 @@ case ${1} in
     take_screenshot -w -b -e shadow
     ;;
   -a|--area|-s|--selection)
+    sleep 0.2
     take_screenshot -a
     ;;
   ""|-f|--full)
     take_screenshot
     ;;
+  -c|--clipboard|-p|--paste)
+    take_screenshot clipboard
+    ;;
+  -j|--jpg)
+    take_screenshot jpg
+    ;;
   *)
     show_error
     ;;