upload.sh: work around, uhm, "strange" dash behavior
[git/dscho.git] / upload.sh
blob2d8d47726c9967df874c245f1eeb695767acabd6
1 #!/bin/sh
3 # This is a simple script that will produce my blog on repo.or.cz
5 # The idea is to have source-<timestamp>.txt files as input, having the
6 # stories, and this script turning them into nice HTML, committing
7 # everything, and then pushing it to my repository.
9 # The blog will then be served using gitweb.
11 # To make it easier on me, if a file "new" exists, it is automatically
12 # renamed using the current timestamp.
14 # How to use:
16 # $ mkdir my-blog
17 # $ cd my-blog
18 # $ git init
20 # Then symlink or copy this file (upload.sh); you can track it or add it
21 # to .gitignore, does not matter.
23 # Add a remote "origin" (you might want to track only the appropriate branch
24 # if the repository contains other branches, too), add a background image,
25 # and then set the config variables gitweb.url, blog.title, blog.background
26 # and blog.branch appropriately.
28 # Example:
30 # $ git remote add -t blog repo.or.cz:/srv/git/git/dscho.git/
31 # $ git symbolic-ref HEAD refs/heads/blog
32 # $ cp ~/images/background.jpg ./
33 # $ git config gitweb.url http://repo.or.cz/w/git/dscho.git
34 # $ git config blog.title "Dscho's blog"
35 # $ git config blog.background background.jpg
36 # $ git config blog.branch blog
38 # Now you can start writing posts, by creating a file called "new", and
39 # calling ./upload.sh to commit the post together with the images and
40 # push all.
42 # Note that no file names may contain spaces.
44 # TODO: document the "syntax" of the source-*.txt files
47 # make sure we're in the correct working directory
48 cd "$(dirname "$0")"
50 GITWEBURL="$(git config gitweb.url)"
51 test -z "$GITWEBURL" && {
52 echo "Please set gitweb.url in the Git config first!" >&2
53 exit 1
56 get_config () {
57 value=$(git config blog.$1)
58 test -z "$value" && value=$2
59 echo $value
62 BACKGROUNDIMG=$(get_config background paper.jpg)
63 TITLE=$(get_config title "Dscho's Git log")
64 MAXENTRIES=$(get_config maxPostsPerPage 10)
65 BRANCH=$(get_config branch blog)
67 URLPREFIX="$(dirname "$GITWEBURL")"/
68 REMOTEREPOSITORY="$(basename "$GITWEBURL")"
69 case "$GITWEBURL" in
70 *'?'*) BLOBPLAIN="$REMOTEREPOSITORY;a=blob_plain";;
71 */) URLPREFIX=$GITWEBURL; BLOBPLAIN="a=blob_plain";;
72 *) BLOBPLAIN="$REMOTEREPOSITORY?a=blob_plain";;
73 esac
74 URL="$BLOBPLAIN;hb=$BRANCH;f="
75 ORIGURL=$URL
76 NEW=new
77 OUTPUT=index.html
78 RSS=blog.rss
79 TEST=test.html
80 THIS=$0
82 LC_ALL=C
83 export LC_ALL
85 move_new_entry_back () {
86 test -f source-$now.txt &&
87 mv source-$now.txt $NEW &&
88 git rm --cached -f source-$now.txt
91 die () {
92 move_new_entry_back
93 echo "$*" >&2
94 exit 1
97 strip_prefix () {
98 echo "${1#$2}"
101 chomp () {
102 strip_prefix "${1%$3}" "$2"
105 nth () {
106 # add illogical suffix
107 case "$1" in
108 *1?|*[04-9]) echo "$1th";;
109 *1) echo "$1st";;
110 *2) echo "$1nd";;
111 *3) echo "$1rd";;
112 esac
115 make_chinese_hour () {
116 case $1 in
117 23|00) echo Rat;;
118 01|02) echo Buffalo;;
119 03|04) echo Tiger;;
120 05|06) echo Rabbit;;
121 07|08) echo Dragon;;
122 09|10) echo Snake;;
123 11|12) echo Horse;;
124 13|14) echo Goat;;
125 15|16) echo Monkey;;
126 17|18) echo Rooster;;
127 19|20) echo Dog;;
128 21|22) echo Pig;;
129 esac
132 digit_to_roman () {
133 case $1 in
134 1) echo $2;;
135 2) echo $2$2;;
136 3) echo $2$2$2;;
137 4) echo $2$3;;
138 5) echo $3;;
139 6) echo $3$2;;
140 7) echo $3$2$2;;
141 8) echo $3$2$2$2;;
142 9) echo $2$4;;
143 esac
146 make_roman_number () {
147 case $1 in
148 '') ;;
149 ?) digit_to_roman $1 I V X;;
150 ??) echo $(digit_to_roman ${1%?} X L C)$(make_roman_number ${1#?});;
151 ???) echo $(digit_to_roman ${1%??} C D M)$(make_roman_number ${1#?});;
152 ????) echo $(digit_to_roman ${1%???} M)$(make_roman_number ${1#?});;
153 esac
156 make_date () {
157 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
158 $(date +%A -d @$1) \
159 $(nth $(date +%e -d @$1)) \
160 $(date +%B -d @$1) \
161 $(make_roman_number $(date +%Y -d @$1)) \
162 $(make_chinese_hour $(date +%H -d @$1))
165 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
166 markup_substitution () {
167 # sed does not know minimal match with .*, only greedy one
168 middle=
169 middleend=
170 delim=$1
171 right_no=3
172 while test ! -z "$delim"
174 right_no=$(($right_no+1))
175 test $right_no -gt 9 &&
176 die "Invalid markup pattern: $1"
177 first=$(expr "$delim" : '\(.\)')
178 delim=${delim#$first}
179 test -z "$delim" && {
180 middle="$middle\\([^$first]\\|$first[A-Za-z0-9]\\)"
181 break
183 middle="$middle\\([^$first]\\|$first"
184 middleend="$middleend\\)"
185 done
187 left="\\(^\\|[^A-Za-z0-9]\\)$1\\($middle$middleend*\\)"
188 right="$1\\($\\|[^A-Za-z0-9]\\)"
189 # work around stupid dash interpreting backslashes when expanding vars
190 bs="\\\\"
191 test "\\" = "$(echo "$bs")" || bs="\\"
192 echo "s/$left$right/${bs}1<$2>${bs}2<\/$2>$bs$right_no/g"
195 space80=' '
196 space80="$space80$space80$space80 "
197 # transform markup in stdin to HTML
198 markup () {
199 case "$*" in
200 *invert-bash*) bash_bg=white; bash_fg=black;;
201 *) bash_bg=black; bash_fg=white;;
202 esac
203 sed -e 's!^$!</p><p>!' \
204 -e "$(markup_substitution "''" i)" \
205 -e "$(markup_substitution "_" u)" \
206 -e 's!IMHO!in my humble opinion!g' \
207 -e 's!BTW!By the way,!g' \
208 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
209 -e 's!:-)!\&#x263a;!g' \
210 -e "s!\\[\\[\(Image\|SVG\):.*!$THIS handle &!e" \
211 -e 's!<bash>!<table\
212 border=1 bgcolor='$bash_bg'>\
213 <tr><td bgcolor=lightblue colspan=3>\
214 <pre>'"$space80"'</pre>\
215 </td></tr>\
216 <tr><td>\
217 <table cellspacing=5 border=0\
218 style="color:'$bash_fg';">\
219 <tr><td>\
220 <pre>!' \
221 -e 's!</bash>!</pre>\
222 </td></tr>\
223 </table>\
224 </td></tr>\
225 </table>!' \
229 # output lines containing <timestamp> <filename> <title>
230 get_blog_entries () {
231 for file in $(ls -r source-*.txt)
233 timestamp=$(chomp $file source- .txt)
234 title="$(sed 1q < $file | markup)"
235 echo "$timestamp $file $title"
236 done
239 get_last_removed_entry () {
240 git log --pretty=format: --name-only --diff-filter=D HEAD |
241 while read line
243 case "$line" in
244 source-*.txt) file=$line;;
245 '') test -z "$file" || {
246 echo "$file"
247 break
249 esac
250 done
253 box_count=0
254 begin_box () {
255 test $box_count = 0 || echo "<br>"
256 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
257 echo "<tr><th>$1</th></tr>"
258 echo "<tr><td>"
261 end_box () {
262 echo "</td></tr></table>"
263 box_count=$(($box_count+1))
266 # make HTML page
267 make_html () {
268 body_style="width:800px"
269 body_style="$body_style;background-image:url($URL$BACKGROUNDIMG)"
270 body_style="$body_style;background-repeat:repeat-y"
271 body_style="$body_style;background-attachment:scroll"
272 body_style="$body_style;padding:0px;"
273 text_style="width:610px"
274 text_style="$text_style;margin-left:120px"
275 text_style="$text_style;margin-top:50px"
276 text_style="$text_style;align:left"
277 text_style="$text_style;vertical-align:top;"
278 cat << EOF
279 <html>
280 <head>
281 <title>$TITLE</title>
282 <meta http-equiv="Content-Type"
283 content="text/html; charset=UTF-8"/>
284 </head>
285 <body style="$body_style">
286 <div style="$text_style">
287 <h1>$TITLE</h1>
289 indent='\t\t\t'
291 # make toc
292 toc_width=400px
293 toc_style="position:absolute;top:50px;left:810px;width=$toc_width"
295 echo "<div style=\"$toc_style\">"
296 begin_box "Table of contents:"
297 echo '<p><ul>'
298 get_blog_entries |
299 while read timestamp filename title
301 date="$(date +"%d %b %Y" -d @$timestamp)"
302 echo "<li><a href=#$timestamp>$date $title</a>"
303 done
304 echo '</ul></p>'
305 file=
306 last_removed_entry=$(get_last_removed_entry)
307 test -z "$last_removed_entry" || {
308 commit=$(git log --pretty=format:%H --diff-filter=AM \
309 -- $last_removed_entry |
310 head -n 1)
311 previous="$BLOBPLAIN;hb=$commit"
312 echo "<a href=$previous;f=index.html>Older posts</a>"
314 end_box
316 # RSS feed
317 rss_style="background-color:orange;text-decoration:none"
318 rss_style="$rss_style;color:white;font-family:sans-serif;"
319 echo '<br>'
320 echo '<div style="text-align:right;">'
321 echo "<a href=\"$ORIGURL$RSS\""
322 echo ' title="Subscribe to my RSS feed"'
323 echo ' class="rss" rel="nofollow"'
324 echo " style=\"$rss_style\">RSS</a>"
325 echo '</div>'
327 # About
328 test -f about.html && {
329 begin_box "About this blog:"
330 cat about.html
331 end_box
334 # Links
335 test -f links.html && {
336 begin_box "Links:"
337 cat links.html
338 end_box
341 # Google AdSense
342 test -z "$DRYRUN" && test -f google.adsense && {
343 begin_box "Google Ads:"
344 cat google.adsense
345 end_box
348 echo '</div>'
349 } | sed -s "s/^/$indent/"
352 # timestamps will not need padding to sort correctly, for some time...
353 get_blog_entries |
354 while read timestamp filename title
356 echo "<h6>$(make_date $timestamp)</h6>"
357 echo "<a name=$timestamp>"
358 echo "<h2>$title</h2>"
359 echo ""
360 echo "<p>"
361 sed 1d < $filename | markup
362 echo "</p>"
363 done |
364 sed -e "s/^./$indent&/" \
365 -e "/<pre>/,/<\/pre>/s/^$indent//"
367 cat << EOF
368 </div>
369 </body>
370 </html>
374 generate_rss () {
375 echo '<?xml version="1.0" encoding="utf-8"?>'
376 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
377 echo '<channel>'
378 echo "<title>Dscho's blog</title>"
379 echo "<link>$URLPREFIX${URL}index.html</link>"
380 self="$URLPREFIX$ORIGURL$RSS"
381 selfattribs='rel="self" type="application/rss+xml"'
382 echo "<atom:link href=\"$self\" $selfattribs/>"
383 echo '<description>A few stories told by Dscho</description>'
384 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
385 echo '<language>en-us</language>'
387 get_blog_entries |
388 while read timestamp filename title
390 # remove all tags
391 title=$(echo "$title" | sed 's/<[^>]*>//g')
392 echo '<item>'
393 echo "<title>$title</title>"
394 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
395 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
396 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
397 description="$(cat < $filename | markup invert-bash)"
398 echo "<description><![CDATA[$description]]></description>"
399 echo "</item>"
400 done
402 echo '</channel>'
403 echo '</rss>'
406 get_image_files () {
407 git ls-files |
408 grep -v '\.\(rss\|html\|gitignore\|in\|sh\|txt\|adsense\)$'
411 remove_old_entries () {
412 count=$(ls source-*.txt | wc -l)
413 test $MAXENTRIES -ge $count && return
415 for file in source-*.txt
417 test $MAXENTRIES -lt $count || break
418 git rm $file > /dev/null || return 1
419 count=$(($count-1))
420 echo $file
421 done
423 # remove no-longer referenced images
424 image_files=$(get_image_files)
425 referenced_files="$(cat source-*.txt |
426 tr ']|' '\n' |
427 sed -ne 's/\[\[\(Image\|SVG\)://p') $BACKGROUNDIMG"
428 for file in $(echo $image_files $referenced_files $referenced_files |
429 tr ' ' '\n' | sort | uniq -u)
431 git rm $file > /dev/null || return 1
432 echo $file
433 done
436 # never, ever have spaces in the file names
437 commit_new_images () {
438 files="$(remove_old_entries) $RSS $BACKGROUNDIMG" ||
439 die "Could not remove old entries"
442 generate_rss > $RSS &&
443 git add $RSS ||
444 die "Could not generate $RSS"
446 for image in $(cat source-* |
447 tr ' ]|' '\n' |
448 sed -n 's/.*\[\[\(Image\|SVG\)://p' |
449 sort |
450 uniq)
452 git add $image || die "Could not git add image $image"
453 files="$files $image"
454 done
456 git update-index --refresh &&
457 git diff-files --quiet -- $files &&
458 git diff --cached --quiet HEAD -- $files ||
459 git commit -s -m "Housekeeping on $(make_date $now)" $files
462 get_image_url () {
463 test ! -z "$DRYRUN" && echo "$1" && return
464 rev=$(git rev-list -1 HEAD -- $1)
465 test -z "$rev" && die "No revision found for $1"
466 echo "$BLOBPLAIN;hb=$rev;f=$1"
469 handle_svg_file () {
470 # for some reason, Firefox adds scrollbars, so nudge the width a bit
471 width=$(sed -ne 's/.* width="\([^"]*\).*/\1/p' -e '/<metadata/q' < "$1")
472 test -z "$width" || width=" width=$(($width+5))"
473 url=$(get_image_url "$1")
474 cat << EOF
475 <center>
476 <table border=0>
477 <tr>
478 <td align=center>
479 <embed type="image/svg+xml"
480 src="$url"$width />
481 </td>
482 </tr>
483 <tr>
484 <td align=center>
485 <a href=$url>$1</a>
486 </td>
487 </tr>
488 </table>
489 </center>
493 handle_image_file () {
494 echo "<center><img src=$(get_image_url "${1%% *}") ${1#* }></center>"
499 # parse command line option
500 case "$1" in
501 *dry*) DRYRUN=1; export DRYRUN; shift;;
502 *show*) firefox "$(pwd)"/$TEST; exit;;
503 *remote*) firefox $URLPREFIX$URL$OUTPUT; exit;;
504 handle)
505 shift
506 case "$1" in
507 "[[SVG:"*) handle_svg_file "$(chomp "$*" '\[\[SVG:' '\]\]')";;
508 "[[Image:"*) handle_image_file "$(chomp "$*" '\[\[Image:' '\]\]')";;
509 esac
510 exit
512 '') ;;
513 *) die "Unknown command: $1";;
514 esac
516 test "$#" = 0 ||
517 die "Usage: $0 [--dry-run]"
519 # make sure we're on the correct branch
520 test refs/heads/$BRANCH = $(git symbolic-ref HEAD) ||
521 die "Not on branch $BRANCH"
523 # make sure there are no uncommitted changes
524 git update-index --refresh &&
525 git diff-files --quiet ||
526 die "Have unstaged changes!"
528 # rename the new blog entry if it exists
529 now=$(date +%s)
530 test ! -f $NEW || {
531 mv -i $NEW source-$now.txt &&
532 git add source-$now.txt
533 } ||
534 die "Could not rename source.txt"
536 # commit the images that are referenced and not yet committed
537 test ! -z "$DRYRUN" ||
538 commit_new_images ||
539 die "Could not commit new images"
541 # to find the images reliably, we have to use the commit name, not the branch
542 # we use the latest commit touching an image file.
543 IMAGEFILES="$(get_image_files)"
544 REV=$(git rev-list -1 HEAD -- $IMAGEFILES)
545 test -z "$REV" && REV=$BRANCH
546 URL="$BLOBPLAIN;hb=$REV;f="
548 if test ! -z "$DRYRUN"
549 then
550 # Output to test.html and have local links into the current directory
551 OUTPUT=$TEST
552 URL=
555 make_html > $OUTPUT || die "Could not write $OUTPUT"
557 test ! -z "$DRYRUN" && {
558 move_new_entry_back
559 exit
562 git add $OUTPUT &&
563 git commit -s -m "Update $(make_date $now)" &&
564 git push origin +$BRANCH