web: Add a devel/ section with one file for now.
[gnupg-doc.git] / tools / mkkudos.sh
1 #!/bin/sh
2
3 # Update the list of donors and a few other things.
4 #
5 # ====================================================================
6 # This org-mode snippet is used to insert the progress bar into a HTML
7 # file:
8 #
9 #  #+BEGIN_HTML
10 #  <div class="donation-progress">
11 #    <div class="donation-progress-bar"
12 #  style="width: 0%"><!--REPLACE-PROGRESS-PERCENT-->
13 #  <p>&nbsp;</p></div>
14 #    <p><span style="float: left">
15 #  <!--INSERT-PROGRESS-LEFT-->
16 #    </span>
17 #    <span style="float: right">
18 #  <!--INSERT-PROGRESS-RIGHT-->
19 #    </span>
20 #  </div>
21 #  <p class="doclear" style="clear: both"></p>
22 #  #+END_HTML
23 #
24 # To use it the code at "Campaign data" below needs to be adjusted as
25 # well.
26 # ===================================================================
27
28 set -e
29
30 usage()
31 {
32     cat <<EOF
33 Usage: $0 [OPTIONS]
34 Options:
35         --force    Force re-creation of files.
36         --verbose  Run in verbose mode
37         --test     Run in test environment
38 EOF
39     exit "$1"
40 }
41
42
43 force=no
44 verbose=no
45 testmode=no
46 while [ $# -gt 0 ]; do
47     case "$1" in
48         # Set up `optarg'.
49         --*=*)
50             optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
51             ;;
52         *)
53             optarg=""
54             ;;
55     esac
56
57     case $1 in
58         --force)
59             force=yes
60             ;;
61         --verbose)
62             verbose=yes
63             ;;
64         --test)
65             testmode=yes
66             ;;
67         --help)
68             usage 0
69             ;;
70         *)
71             usage 1 1>&2
72             ;;
73     esac
74     shift
75 done
76
77 htdocs="/var/www/www/www.gnupg.org/htdocs"
78 donors="$htdocs/donate/donors.dat"
79 donations="$htdocs/donate/donations.dat"
80 blogheadlinefile="/var/www/www/blog.gnupg.org/htdocs/headlines.txt"
81
82 if [ $testmode = yes ]; then
83   htdocs="/home/wk/s/gnupg-doc/stage"
84   donors="$htdocs/../scratch/donors.dat"
85   donations="$htdocs/../scratch/donations.dat"
86   blogheadlinefile="$htdocs/../misc/blog.gnupg.org/headlines.txt"
87 fi
88
89
90 if [ ! -f "$donors" ]; then
91   echo "mkkudos.sh: '$donors' not found" >&2;
92   exit 1
93 fi
94 if [ ! -f "$donations" ]; then
95   echo "mkkudos.sh: '$donations' not found" >&2;
96   exit 1
97 fi
98
99 if [ ! -f "$blogheadlinefile" ]; then
100   echo "mkkudos.sh: '$blogheadlinefile' not found" >&2;
101   blogheadline=""
102 else
103   blogheadline=$(awk -F\| '
104         NR<=3 {printf "<li><a href=\"blog/%s\">%s</a></li>", $1, $2}
105      ' "$blogheadlinefile")
106 fi
107
108 tmp=$(head -1 "$donations")
109 monyear=$(echo "$tmp" | awk -F: 'BEGIN { m[1] = "January";
110       m[2] = "February"; m[3] = "March"; m[4] = "April"; m[5] = "May";
111       m[6] = "June"; m[7] = "July"; m[8] = "August"; m[9] = "September";
112       m[10] = "October"; m[11] = "November"; m[12] = "December"; }
113       {printf "%s %d", m[int($2)] , $1}')
114 thisyear=$(echo "$tmp" | awk -F: '{print $1}')
115 euroyr=$(echo "$tmp" | awk -F: '{printf "%d &euro;", int($10 + 0.5)}')
116 nyr=$(echo "$tmp" | awk -F: '{printf "%d", $9}')
117
118
119 dontable=$(awk -F: <"$donations" -v thisyear="$thisyear" '
120   BEGIN { m[1] = "January";
121           m[2] = "February"; m[3] = "March"; m[4] = "April"; m[5] = "May";
122           m[6] = "June"; m[7] = "July"; m[8] = "August"; m[9] = "September";
123           m[10] = "October"; m[11] = "November"; m[12] = "December" ;
124           printf "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\"";
125           printf " rules=\"groups\" frame=\"hsides\">\n";
126           printf "<colgroup>\n";
127           printf "<col class=\"left\" />\n";
128           printf "<col class=\"right\" />\n";
129           printf "<col class=\"right\" />\n";
130           printf "</colgroup>\n";
131           printf "<thead>\n";
132           printf "<tr>\n";
133           printf "<th scope=\"col\" class=\"left\">Month</th>\n";
134           printf "<th scope=\"col\" class=\"right wideright\">#</th>\n";
135           printf "<th scope=\"col\" class=\"right wideright\">&euro;</th>\n";
136           printf "</tr>\n";
137           printf "</thead>\n";
138           printf "<tbody>\n";
139         }
140   NR==1 { nyear = $9; totalyear = int($10 + 0.5);
141         }
142   $1 != thisyear {
143           printf "</tbody>\n";
144           printf "<tbody>\n";
145           printf "<tr><td class=\"left\">%d</td>\n", thisyear;
146           printf "    <td class=\"right wideright\">%d</td>\n", nyear;
147           printf "    <td class=\"right wideright\">%d</td></tr>\n", totalyear;
148           printf "</tbody>\n";
149           printf "</table>\n";
150           exit 0
151         }
152         { printf "<tr><td class=\"left\">%s</td>\n", m[int($2)];
153           printf "    <td class=\"right wideright\">%d</td>\n", $7;
154           printf "    <td class=\"right wideright\">%d</td></tr>\n",
155                                                   int($8 + 0.5);
156         }
157 ')
158
159
160
161 # Campaign data
162 goal="120000"
163 percent=$(echo "$euroyr:$goal" | awk -F: '{ p = (int($1)*100)/int($2);
164                                           if(p > 100) { p = 100 };
165                                           printf "%d", p}')
166
167 for file in "$htdocs/donate/"kudos-????.html "$htdocs/donate/"kudos.html \
168             "$htdocs/donate/"index.html \
169             "$htdocs/"index.html
170  do
171    if [ $force = no ]; then
172      [ "$file" -ot "$donors" ] || continue
173    fi
174    if [ "$file" = "$htdocs/donate/"kudos.html ]; then
175      year=$(date +%Y)
176    else
177      year=${file#$htdocs/donate/kudos-}
178      year=${year%.html}
179    fi
180    [ $verbose = yes ] && echo "processing $file" >&2
181    [ -f "$file.tmp" ] && rm "$file.tmp"
182    awk -F: -v year="$year" -v donors="$donors" -v dontable="$dontable" \
183            -v monyear="$monyear" -v thisyear="$thisyear" \
184            -v euro="$euro" -v euroyr="$euroyr" \
185            -v nyr="$nyr" -v goal="$goal" -v percent="$percent" \
186            -v blogheadline="$blogheadline" \
187             <"$file"  >"$file.tmp" '
188      /<!--BEGIN-DONATIONS-->/ {indon=1; print; insert("") }
189      /<!--END-DONATIONS-->/ {indon=0}
190      /<!--BEGIN-SOME-DONATIONS-->/ {indon=1; print; insertsome("") }
191      /<!--END-SOME-DONATIONS-->/ {indon=0}
192      /<!--BEGIN-DONATIONS_goteo13-->/ {indon=1; print; insert("goteo13") }
193      /<!--END-DONATIONS_goteo13-->/ {indon=0}
194      /<!--BEGIN-DONATION-TABLE-->/ {indon=1; print; print dontable }
195      /<!--END-DONATION-TABLE-->/ {indon=0}
196      /<!--INSERT-MONTH-DATE-->/ {
197            printf "<!--INSERT-MONTH-DATE--> %s\n", monyear;
198            next
199      }
200      /<!--INSERT-THIS-YEAR-->/ {
201            printf "<!--INSERT-THIS-YEAR--> %d\n", thisyear;
202            next
203      }
204      /<!--INSERT-YEAR-EURO-->/ {
205            printf "<!--INSERT-YEAR-EURO--> %s\n", euroyr;
206            next
207      }
208      /<!--INSERT-YEAR-N-->/ {
209            printf "<!--INSERT-YEAR-N--> %s\n", nyr;
210            next
211      }
212      /<!--INSERT-PROGRESS-LEFT-->/ {
213            printf "<!--INSERT-PROGRESS-LEFT-->%s &euro;\n",
214                   euro;
215            next
216      }
217      /<!--INSERT-PROGRESS-RIGHT-->/ {
218            printf "<!--INSERT-PROGRESS-RIGHT-->goal: %s &euro;\n", goal;
219            next
220      }
221      /<!--REPLACE-PROGRESS-PERCENT-->/ {
222            printf "style=\"width: %d%%\"<!--REPLACE-PROGRESS-PERCENT-->\n",
223                   percent;
224            next
225      }
226      /<!--INSERT-BLOG-HEADLINE-->/ {
227            printf "<!--INSERT-BLOG-HEADLINE--> %s\n", blogheadline;
228            next
229      }
230      !indon { print }
231
232      function insert (tag) {
233        while (getline < donors) {
234          if ( $0 ~ /^(#.*)?$/ )
235             continue;
236          if ( $3 == "" )
237             continue;
238          if ($1==year && $4==tag) {
239            printf "<li>%s</li>\n", $3
240          }
241        }
242        close (donors)
243      }
244
245      function insertsome (tag) {
246        i = 0
247        while (getline < donors) {
248          if ( $0 ~ /^(#.*)?$/ )
249             continue;
250          if ( $3 == "" )
251             continue;
252          if ($4==tag) {
253            data[i++] = $3
254          }
255        }
256        close (donors)
257        j = i > 16 ? ( i - 16 ) : 0
258        while (j < i) {
259            printf "<li>%s</li>\n", data[j++]
260        }
261      }
262      '
263    mv "$file.tmp" "$file" || echo "mkkudos.sh: error updating $file" >&2
264 done