3 # Copyright (c) 2009 Florian Reitmeir
4 # Copyright (c) 2009 Peter Palfrader
6 # Permission is hereby granted, free of charge, to any person
7 # obtaining a copy of this software and associated documentation
8 # files (the "Software"), to deal in the Software without
9 # restriction, including without limitation the rights to use,
10 # copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the
12 # Software is furnished to do so, subject to the following
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 # OTHER DEALINGS IN THE SOFTWARE.
27 # Author: Florian Reitmeir
28 # E-mail: florian@reitmeir.org
30 # Publishes files on a webserver.
32 # This script copies files or directories as given on the command line,
33 # or stdin or the xclipboard, to a remote location using rsync. That
34 # remote directory should be exposed online and ideally be o-r or have
37 # The directory names chosen by this script are random(ish) but still
38 # contain a date string so one know how old things are when looking at them
39 # (Or one can just have cron clean out stuff older than a week or a month
42 # The random location chosen is printed to stdout when the script finishes,
43 # and is copied to the xclipboard if the xclip(1) utility is installed·
45 # base_http and base_rsync can be overriden in a ~/.publish.cfg shell snippet.
47 base_http=http://www.example.com/pub
48 base_rsync=marvin.example.com:/var/www/www.example.com/htdocs/pub
50 history_file=~/.publish.history
53 date_format='%Y-%m-%d-'
55 rsync_pre_invoke() { true ;}
56 rsync_post_invoke() { true ;}
57 publish_pre_invoke() { true ;}
58 publish_post_invoke() { true ;}
60 [ -e ~/.publish.cfg ] && . ~/.publish.cfg
65 usage: $0 [<src> [<src> ...]]
67 copy the file <src> to a server an report the URL.
70 -n no-do. Just print what would have been done
72 -x Publish the contents of the xclipboard
78 perl -MURI::Escape -ne 'print uri_escape($_)'
82 head -c 8 /dev/urandom | base64 | tr '/+' 'xx' | tr -d '='
86 time=$(date --utc --rfc-3339='seconds')
87 echo $time $1 >>"$history_file"
89 history_tmp=$( tempfile )
90 cat "$history_file" | tail --lines="$history_lines" >"$history_tmp"
91 mv "$history_tmp" "$history_file"
101 while getopts "hnsx" OPTION
115 if [ -r "$history_file" ]; then
119 echo "history file: '$history_file' not found"
129 shift $(($OPTIND - 1))
131 if [[ "$#" = 0 ]]; then
133 trap "rm -f '$stdin'" EXIT
136 if [ "$inputxclip" != 1 ] ; then
137 echo "No files given on the command line, using stdin" >&2
140 if which xclip >/dev/null 2>&1; then
141 echo "Publishing x clipboard:" >&2
143 cat "$stdin" | sed -e 's#^#| #'
147 echo "xclip not installed?" >&2
153 elif [ "$inputxclip" = 1 ] ; then
154 echo "Ignoring -x because there are files on the command line" >&2
157 d_date=$(date +"$date_format")
158 d_random=$(get_random)
159 d_server="$d_date$d_random"
160 rsync_args="--partial --recursive --compress --times"
162 d_server_http_base=$( echo -n "$d_server" | uri_encode )
164 if [ "$#" = 1 ]; then
168 echo "$base_http/$d_server_http_base/"
169 if which xclip >/dev/null 2>&1; then
170 echo -n "$base_http/$d_server_http_base/" | xclip
177 while [ "$#" -gt 0 ]; do
181 uri="$base_http/$d_server_http_base/$( echo -n "`basename "$file"`" | uri_encode )"
184 if [ "$only_one" = 1 ]; then
185 if [ -d "$file" ]; then
187 uri="$base_http/$d_server_http_base/"
189 if which xclip >/dev/null 2>&1; then
190 echo -n "$uri" | xclip
195 history_append "$uri"
198 $NODO rsync $rsync_args "$file$trail" $base_rsync"/$d_server/"