summaryrefslogtreecommitdiff
path: root/publish
blob: 7a7d68bb933db742c3bb35ea0eb0b02d1a2d6a2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash

# Copyright (c) 2009 Florian Reitmeir
# Copyright (c) 2009 Peter Palfrader
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# Author: Florian Reitmeir
# E-mail: florian@reitmeir.org

base_http=http://www.example.com/pub
base_rsync=marvin.example.com:/var/www/www.example.com/htdocs/pub

history_file=~/.publish.history
history_lines=1000

[ -e ~/.publish.cfg ] && . ~/.publish.cfg

usage()
{
cat << EOF
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
EOF
}

uri_encode() {
	perl -MURI::Escape -ne 'print uri_escape($_)'
}

get_random() {
	head -c 8 /dev/urandom | base64 | tr '/+' 'xx' | tr -d '='
}

history_append() {
    time=$(date --utc --rfc-3339='seconds')
    echo $time $1 >>"$history_file"

    history_tmp=$( tempfile )
    cat "$history_file" | tail --lines="$history_lines" >"$history_tmp"
    mv "$history_tmp" "$history_file"
}

history_show() {
    cat "$history_file"
}

NODO=""
inputxclip=0

while getopts "hnsx" OPTION
do
     case $OPTION in
         h)
             usage
             exit
             ;;
         n)
             NODO="echo"
             ;;
         x)
             inputxclip=1
             ;;
         s)
             if [ -r "$history_file" ]; then
                history_show
                exit 0
             else
                echo "history file: '$history_file' not found"
                exit 3
             fi
             ;;
         *)
             usage >&2
             exit 1
             ;;
     esac
done
shift $(($OPTIND - 1))

if [[ "$#" = 0 ]]; then
	stdin=`tempfile`
	trap "rm -f '$stdin'" EXIT
	chmod a+r "$stdin"

	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
			echo "Publishing x clipboard:" >&2
			xclip -o > "$stdin"
			cat "$stdin" | sed -e 's#^#| #'
			echo
			echo "EOF"
		else
			echo "xclip not installed?" >&2
			exit 1
		fi
	fi
	set dummy "$stdin"
	shift
elif [ "$inputxclip" = 1 ] ; then
	echo "Ignoring -x because there are files on the command line" >&2
fi

d_date=$(date +'%Y-%m-%d')
d_random=$(get_random)
d_server="$d_date-$d_random"
rsync_args="--partial --recursive --compress"

d_server_http_base=$( echo -n "$d_server" | uri_encode )

if [ "$#" = 1 ]; then
	only_one=1
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
	fi
fi


while [ "$#" -gt 0 ]; do
	file="$1"
	shift

	uri="$base_http/$d_server_http_base/$( echo -n "`basename $file`" | uri_encode )"
	echo "$uri"
	history_append "$uri"

	trail=""
	if [ "$only_one" = 1 ]; then
		if [ -d "$file" ]; then
			trail="/"
		fi
		echo -n "$uri" | xclip
	fi


	$NODO rsync $rsync_args "$file$trail" $base_rsync"/$d_server/"
done