Add command line options "show" and "remote"
[git/dscho.git] / upload.sh
blob83beeca0f7f0216e9e5a3e0e3350a5efc6fd06d5
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 "source.txt" exists, it is
12 # automatically renamed using the current timestamp.
14 # TODO: handle images (git add them and rewrite the URL dynamically)
15 # TODO: generate an RSS feed, too
17 BRANCH=blog
18 URL="dscho.git?a=blob_plain;hb=$BRANCH;f="
19 CSS=blog.css
20 NEW=new
21 OUTPUT=index.html
22 TEST=test.html
23 TITLE="Dscho's blog"
25 LC_ALL=C
26 export LC_ALL
28 die () {
29 echo "$*" >&2
30 exit 1
33 nth () {
34 # add illogical suffix
35 case "$1" in
36 *1?|*[04-9]) echo "$1th";;
37 *1) echo "$1st";;
38 *2) echo "$1nd";;
39 *3) echo "$1rd";;
40 esac
43 make_chinese_hour () {
44 case $1 in
45 23|00) echo Rat;;
46 01|02) echo Buffalo;;
47 03|04) echo Tiger;;
48 05|06) echo Rabbit;;
49 07|08) echo Dragon;;
50 09|10) echo Snake;;
51 11|12) echo Horse;;
52 13|14) echo Goat;;
53 15|16) echo Monkey;;
54 17|18) echo Rooster;;
55 19|20) echo Dog;;
56 21|22) echo Pig;;
57 esac
60 digit_to_roman () {
61 case $1 in
62 1) echo $2;;
63 2) echo $2$2;;
64 3) echo $2$2$2;;
65 4) echo $2$3;;
66 5) echo $3;;
67 6) echo $3$2;;
68 7) echo $3$2$2;;
69 8) echo $3$2$2$2;;
70 9) echo $2$4;;
71 esac
74 make_roman_number () {
75 case $1 in
76 '') ;;
77 ?) digit_to_roman $1 I V X;;
78 ??) echo $(digit_to_roman ${1%?} X L C)$(make_roman_number ${1#?});;
79 ???) echo $(digit_to_roman ${1%??} C D M)$(make_roman_number ${1#?});;
80 ????) echo $(digit_to_roman ${1%???} M)$(make_roman_number ${1#?});;
81 esac
84 make_date () {
85 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
86 $(date +%A -d @$1) \
87 $(nth $(date +%e -d @$1)) \
88 $(date +%B -d @$1) \
89 $(make_roman_number $(date +%Y -d @$1)) \
90 $(make_chinese_hour $(date +%H -d @$1))
93 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
94 markup_substitution () {
95 case "$1" in
96 ?) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
97 ??)
98 tmp="[^${1%?}]*"
99 tmp2="\\|${1%?}[^${1#?}]$tmp"
100 tmp3="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
101 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
103 esac
106 # transform markup in stdin to HTML
107 markup () {
108 sed -e 's!^$!</p><p>!' \
109 -e 's!IMHO!in my humble opinion!g' \
110 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
111 -e 's!:-)!\&#x263a;!g' \
112 -e "$(markup_substitution "''" i)" \
113 -e "$(markup_substitution "_" u)"
116 # make HTML page
117 make_html () {
118 cat << EOF
119 <html>
120 <head>
121 <title>$TITLE</title>
122 <meta http-equiv="Content-Type"
123 content="text/html; charset=UTF-8"/>
124 <link rel="stylesheet" type="text/css" href="$URL$CSS">
125 </head>
126 <body>
127 <div class=content>
128 <h1>$TITLE</h1>
131 # timestamps will not need padding to sort correctly, for some time...
132 for file in $(ls -r source-*.txt)
134 basename=${file%.txt}
135 timestamp=${basename#source-}
136 echo "<h6>$(make_date $timestamp)</h6>"
137 echo "<h2>$(sed 1q < $file | markup)</h2>"
138 echo ""
139 echo "<p>"
140 sed 1d < $file | markup
141 echo "</p>"
142 done |
143 sed -e 's/^./\t\t\t&/'
145 cat << EOF
146 </div>
147 </body>
148 </html>
153 # make sure we're in the correct working directory
154 cd "$(dirname "$0")"
156 # parse command line option
157 case "$1" in
158 *dry*) DRYRUN=1; shift;;
159 *show*) firefox "$(pwd)"/$TEST; exit;;
160 *remote*) firefox http://repo.or.cz/w/git/$URL$OUTPUT; exit;;
161 esac
163 test "$#" = 0 ||
164 die "Usage: $0 [--dry-run]"
166 # make sure we're on the correct branch
167 test refs/heads/$BRANCH = $(git symbolic-ref HEAD) ||
168 die "Not on branch $BRANCH"
170 # make sure there are no uncommitted changes
171 git update-index --refresh &&
172 git diff-files --quiet &&
173 git diff-index --quiet --cached HEAD ||
174 die "Have uncommitted changes!"
176 # rename the new blog entry if it exists
177 now=$(date +%s)
178 test ! -f $NEW || {
179 mv -i $NEW source-$now.txt &&
180 git add source-$now.txt
181 } ||
182 die "Could not rename source.txt"
184 if test -z "$DRYRUN"
185 then
186 # rewrite URLs
187 sed -e "s/url(/&$URL/g" < $CSS.in > $CSS &&
188 git add $CSS ||
189 die "Rewriting $CSS failed"
190 else
191 OUTPUT=$TEST
192 CSS=$CSS.in
193 URL=
196 make_html > $OUTPUT || die "Could not write $OUTPUT"
198 test ! -z "$DRYRUN" && exit
200 git add $OUTPUT &&
201 git commit -s -m "Update $(make_date $now)" &&
202 git push origin +$BRANCH