]> git.donarmstrong.com Git - bin.git/blob - publish
add reset usb bus command
[bin.git] / publish
1 #!/bin/bash
2
3 # Copyright (c) 2009 Florian Reitmeir
4 # Copyright (c) 2009 Peter Palfrader
5 #
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
13 # conditions:
14 #
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
17 #
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.
26
27 # Author: Florian Reitmeir
28 # E-mail: florian@reitmeir.org
29
30 # Publishes files on a webserver.
31 #
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
35 # indexing disabled.
36 #
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
40 # automagically).
41 #
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·
44 #
45 # base_http and base_rsync can be overriden in a ~/.publish.cfg shell snippet.
46
47 base_http=http://www.example.com/pub
48 base_rsync=marvin.example.com:/var/www/www.example.com/htdocs/pub
49
50 history_file=~/.publish.history
51 history_lines=1000
52
53 date_format='%Y-%m-%d-'
54
55 rsync_pre_invoke() { true ;}
56 rsync_post_invoke() { true ;}
57 publish_pre_invoke() { true ;}
58 publish_post_invoke() { true ;}
59
60 [ -e ~/.publish.cfg ] && . ~/.publish.cfg
61
62 usage()
63 {
64 cat << EOF
65 usage: $0 [<src> [<src> ...]]
66
67 copy the file <src> to a server an report the URL.
68
69 OPTIONS:
70    -n      no-do.  Just print what would have been done
71    -s      Show the history
72    -x      Publish the contents of the xclipboard
73    -h      Show this message
74 EOF
75 }
76
77 uri_encode() {
78         perl -MURI::Escape -ne 'print uri_escape($_)'
79 }
80
81 get_random() {
82         head -c 8 /dev/urandom | base64 | tr '/+' 'xx' | tr -d '='
83 }
84
85 history_append() {
86     time=$(date --utc --rfc-3339='seconds')
87     echo $time $1 >>"$history_file"
88
89     history_tmp=$( tempfile )
90     cat "$history_file" | tail --lines="$history_lines" >"$history_tmp"
91     mv "$history_tmp" "$history_file"
92 }
93
94 history_show() {
95     cat "$history_file"
96 }
97
98 NODO=""
99 inputxclip=0
100
101 while getopts "hnsx" OPTION
102 do
103      case $OPTION in
104          h)
105              usage
106              exit
107              ;;
108          n)
109              NODO="echo"
110              ;;
111          x)
112              inputxclip=1
113              ;;
114          s)
115              if [ -r "$history_file" ]; then
116                 history_show
117                 exit 0
118              else
119                 echo "history file: '$history_file' not found"
120                 exit 3
121              fi
122              ;;
123          *)
124              usage >&2
125              exit 1
126              ;;
127      esac
128 done
129 shift $(($OPTIND - 1))
130
131 if [[ "$#" = 0 ]]; then
132         stdin=`tempfile`
133         trap "rm -f '$stdin'" EXIT
134         chmod a+r "$stdin"
135
136         if [ "$inputxclip" != 1 ] ; then
137                 echo "No files given on the command line, using stdin" >&2
138                 cat > "$stdin"
139         else
140                 if which xclip >/dev/null 2>&1; then
141                         echo "Publishing x clipboard:" >&2
142                         xclip -o > "$stdin"
143                         cat "$stdin" | sed -e 's#^#| #'
144                         echo
145                         echo "EOF"
146                 else
147                         echo "xclip not installed?" >&2
148                         exit 1
149                 fi
150         fi
151         set dummy "$stdin"
152         shift
153 elif [ "$inputxclip" = 1 ] ; then
154         echo "Ignoring -x because there are files on the command line" >&2
155 fi
156
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"
161
162 d_server_http_base=$( echo -n "$d_server" | uri_encode )
163
164 if [ "$#" = 1 ]; then
165         only_one=1
166 else
167         only_one=0
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
171         fi
172 fi
173
174
175 publish_pre_invoke
176
177 while [ "$#" -gt 0 ]; do
178         file="$1"
179         shift
180
181         uri="$base_http/$d_server_http_base/$( echo -n "`basename "$file"`" | uri_encode )"
182
183         trail=""
184         if [ "$only_one" = 1 ]; then
185                 if [ -d "$file" ]; then
186                         trail="/"
187                         uri="$base_http/$d_server_http_base/"
188                 fi
189                 if which xclip >/dev/null 2>&1; then
190                         echo -n "$uri" | xclip
191                 fi
192         fi
193
194         echo "$uri"
195         history_append "$uri"
196
197         rsync_pre_invoke
198         $NODO rsync $rsync_args "$file$trail" $base_rsync"/$d_server/"
199         rsync_post_invoke
200 done
201
202 publish_post_invoke