update publish
authorJoerg Jaspert <joerg@debian.org>
Sun, 5 May 2013 16:48:46 +0000 (18:48 +0200)
committerJoerg Jaspert <joerg@debian.org>
Sun, 5 May 2013 16:48:46 +0000 (18:48 +0200)
bin/publish

index 43d5b43..4d4a4f6 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 # Copyright (c) 2009 Florian Reitmeir
-# Copyright (c) 2009 Peter Palfrader
+# Copyright (c) 2009,2011,2012 Peter Palfrader
 #
 # Permission is hereby granted, free of charge, to any person
 # obtaining a copy of this software and associated documentation
@@ -40,7 +40,8 @@
 # automagically).
 #
 # The random location chosen is printed to stdout when the script finishes,
-# and is copied to the xclipboard if the xclip(1) utility is installed·
+# and is copied to the xclipboard if the xclip(1) or xsel(1) utility is
+# installed.
 #
 # base_http and base_rsync can be overriden in a ~/.publish.cfg shell snippet.
 
@@ -50,6 +51,16 @@ base_rsync=marvin.example.com:/var/www/www.example.com/htdocs/pub
 history_file=~/.publish.history
 history_lines=1000
 
+ttl=180
+
+date_format='%Y-%m-%d-'
+rsync_args="--compress --times --chmod=u=rwX,go=rX"
+
+rsync_pre_invoke() { true ;}
+rsync_post_invoke() { true ;}
+publish_pre_invoke() { true ;}
+publish_post_invoke() { true ;}
+
 [ -e ~/.publish.cfg ] && . ~/.publish.cfg
 
 usage()
@@ -60,15 +71,23 @@ usage: $0 [<src> [<src> ...]]
 copy the file <src> to a server an report the URL.
 
 OPTIONS:
-   -n      no-do.  Just print what would have been done
-   -s      Show the history
-   -x      Publish the contents of the xclipboard
-   -h      Show this message
+   -8        Add a AddDefaultCharset UTF-8 .htaccess file.
+   -H        Show the history.
+   -s FN     When reading data from stdin, use FN as filename to be published.
+   -S        Make a screenshot of one window and publish.
+   -h        Show this message.
+   -n        no-do.  Just print what would have been done.
+   -q        Produce a QR code.
+   -r        Add --relative option to rsync so that path names of the given
+             files are preserved at the remote host.
+   -t days   time to live in days
+   -T tag    directory name on the server (use this to re-publish under that name)
+   -x        Publish the contents of the xclipboard.
 EOF
 }
 
 uri_encode() {
-       perl -MURI::Escape -ne 'print uri_escape($_)'
+       perl -MURI::Escape -F/ -ape '$_ = join "/", map {uri_escape($_)} grep {!/^\.?$/} @F'
 }
 
 get_random() {
@@ -88,23 +107,41 @@ history_show() {
     cat "$history_file"
 }
 
+setup_tmpdir() {
+    if [ -z "$tmpdir" ]; then
+        tmpdir=`mktemp -d`
+        trap "rm -rf '$tmpdir'" EXIT
+    fi
+}
+
+
 NODO=""
 inputxclip=0
+do_screenshot=0
+make_qrcode=0
+add_default_cs=0
+relative=""
 
-while getopts "hnsx" OPTION
+while getopts "rhnq8HSxs:t:T:" OPTION
 do
      case $OPTION in
          h)
              usage
              exit
              ;;
+         q)
+             make_qrcode=1
+             ;;
          n)
              NODO="echo"
              ;;
          x)
              inputxclip=1
              ;;
-         s)
+         8)
+             add_default_cs=1
+             ;;
+         H)
              if [ -r "$history_file" ]; then
                 history_show
                 exit 0
@@ -113,6 +150,21 @@ do
                 exit 3
              fi
              ;;
+         S)
+             do_screenshot=1
+             ;;
+         s)
+             name_stdin="$OPTARG"
+             ;;
+         r)
+             relative="--relative"
+             ;;
+         t)
+             ttl="$OPTARG"
+             ;;
+         T)
+             d_server="$OPTARG"
+             ;;
          *)
              usage >&2
              exit 1
@@ -121,13 +173,27 @@ do
 done
 shift $(($OPTIND - 1))
 
+
+tmpdir=""
+
+if [[ "$do_screenshot" -gt 0 ]]; then
+       setup_tmpdir
+       img="$tmpdir/screenshot.png"
+       import "$img"
+       chmod a+r "$img"
+       set dummy "$img" "$@"
+       shift
+fi
+
 if [[ "$#" = 0 ]]; then
-       stdin=`tempfile`
-       trap "rm -f '$stdin'" EXIT
-       chmod a+r "$stdin"
+       setup_tmpdir
+       if [ "${name_stdin%-}" != "" ]; then
+               stdin="$tmpdir/$(basename "$name_stdin")"
+       else
+               stdin="$tmpdir/stdin"
+       fi
 
        if [ "$inputxclip" != 1 ] ; then
-               echo "No files given on the command line, using stdin" >&2
                cat > "$stdin"
        else
                if which xclip >/dev/null 2>&1; then
@@ -136,8 +202,14 @@ if [[ "$#" = 0 ]]; then
                        cat "$stdin" | sed -e 's#^#| #'
                        echo
                        echo "EOF"
+               elif which xsel >/dev/null 2>&1; then
+                       echo "Publishing x clipboard:" >&2
+                       xsel -o > "$stdin"
+                       cat "$stdin" | sed -e 's#^#| #'
+                       echo
+                       echo "EOF"
                else
-                       echo "xclip not installed?" >&2
+                       echo "Neither xclip nor xsel installed?" >&2
                        exit 1
                fi
        fi
@@ -147,43 +219,87 @@ elif [ "$inputxclip" = 1 ] ; then
        echo "Ignoring -x because there are files on the command line" >&2
 fi
 
-d_date=$(date +'%Y-%m-%d')
+d_date=$(date +"$date_format")
 d_random=$(get_random)
-d_server="$d_date-$d_random"
-rsync_args="--partial --recursive --compress"
+d_server="${d_server:-$d_date$d_random}"
 
 d_server_http_base=$( echo -n "$d_server" | uri_encode )
+base_uri="$base_http/$d_server_http_base/"
+main_uri="$base_uri"
 
+one_dir_only=""
 if [ "$#" = 1 ]; then
-       only_one=1
+       if [ -d "$1" ]; then
+               set dummy "$1/"
+               shift
+               one_dir_only=1
+       else
+               main_uri="$base_uri$( echo -n "$(basename "$1")" | uri_encode )"
+       fi
 else
-       only_one=0
-       echo "$base_http/$d_server_http_base/"
-       if which xclip >/dev/null 2>&1; then
-               echo -n "$base_http/$d_server_http_base/" | xclip
+       echo "$main_uri"
+fi
+
+
+publish_pre_invoke
+
+if [ "$make_qrcode" -gt 0 ] ; then
+       if command -v qrencode >/dev/null 2>&1; then
+               setup_tmpdir
+               img="$tmpdir/.qr.png"
+               echo -n "$main_uri" | qrencode -s 5 -l H -o "$img"
+               echo "$base_uri"".qr.png"
+               main_uri="$base_uri"".qr.png"
+               set dummy "$img" "$@"
+               shift
+       else
+               echo >&2 "Warning: qrencode not found."
        fi
 fi
 
+if [ "$add_default_cs" -gt 0 ] ; then
+       setup_tmpdir
+       echo 'AddDefaultCharset UTF-8' > "$tmpdir/.htaccess"
+       set dummy "$tmpdir/.htaccess" "$@"
+       shift
+fi
+
+if command -v xclip >/dev/null 2>&1; then
+       echo -n "$main_uri" | xclip
+elif command -v xsel >/dev/null 2>&1; then
+       echo -n "$main_uri" | xsel
+fi
+
+
+if [ -n "${ttl}" ]; then
+       setup_tmpdir
+       echo "${ttl}" > "$tmpdir/.publish.ttl"
+       set dummy "$tmpdir/.publish.ttl" "$@"
+       shift
+fi
 
 while [ "$#" -gt 0 ]; do
        file="$1"
        shift
 
-       uri="$base_http/$d_server_http_base/$( echo -n "`basename "$file"`" | uri_encode )"
-
-       trail=""
-       if [ "$only_one" = 1 ]; then
-               if [ -d "$file" ]; then
-                       trail="/"
-                       uri="$base_http/$d_server_http_base/"
-               fi
-               if which xclip >/dev/null 2>&1; then
-                       echo -n "$uri" | xclip
+       if [ "${file%/.publish.ttl}" == "$file" ]; then
+               if [ -z "$relative" ]; then
+                       if [ -z "$one_dir_only" ]; then
+                               uri="$base_uri""$( echo -n "`basename "$file"`" | uri_encode )"
+                       else
+                               uri="$base_uri"
+                       fi
+               else
+                       uri="$base_uri""$( echo -n $file | uri_encode )"
                fi
+               echo "$uri"
+               history_append "$uri"
        fi
 
-       echo "$uri"
-       history_append "$uri"
-
-       $NODO rsync $rsync_args "$file$trail" $base_rsync"/$d_server/"
+       rsync_pre_invoke
+       $NODO rsync --recursive $rsync_args $relative "$file" $base_rsync"/$d_server/"
+       rsync_post_invoke
 done
+
+
+publish_post_invoke