tools: Update translated versions of the donation page
[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 # For the 2017 campaign new variables which work slightly different
25 # are introduced:
26 #
27 #  #+BEGIN_HTML
28 #  <div class="progress-bar progress-bar-striped active"
29 #       role="progressbar"
30 #       aria-valuenow="0" A-CMPGN-RECUR-EURO=""
31 #       aria-valuemin="0"
32 #       aria-valuemax="0" A-CMPGN-RECUR-EURO-GOAL=""
33 #       style="width: 0%" A-CMPGN-RECUR-PERCENT=""
34 #       >
35 #    <span class="sr-only"><!--CMPGN-RECUR-EURO-->
36 #                            a month</span>
37 #  </div>
38 #  <div class="col-xs-12 col-sm-6 col-lg-12 camp-progress-info">
39 #    <h3><!--CMPGN-RECUR-EURO-->
40 #        a month <small>of
41 #        <!--CMPGN-RECUR-EURO-GOAL-->
42 #        needed</small>
43 #    </h3>
44 #    <h4>+ <!--CMPGN-ONCE-EURO-->
45 #        <small>in one-time donations</small>
46 #    </h4>
47 #    <h4><!--CMPGN-RECUR-COUNT-->
48 #        <small>Supporters</small>
49 #    </h4>
50 #  #+END_HTML
51 #
52 # To use it the code at "Campaign data" below needs to be adjusted as
53 # well.
54 # ===================================================================
55
56 set -e
57
58 LD_LIBRARY_PATH=/usr/local/lib
59 export LD_LIBRARY_PATH
60
61
62 usage()
63 {
64     cat <<EOF
65 Usage: $0 [OPTIONS]
66 Options:
67         --verbose  Run in verbose mode
68         --force    Force re-creation of files.
69         --test     Run in test environment (preview.gnupg.org)
70 EOF
71     exit $1
72 }
73
74
75 force=no
76 verbose=no
77 testmode=no
78 while [ $# -gt 0 ]; do
79     case "$1" in
80         # Set up `optarg'.
81         --*=*)
82             optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
83             ;;
84         *)
85             optarg=""
86             ;;
87     esac
88
89     case $1 in
90         --force)
91             force=yes
92             ;;
93         --verbose)
94             verbose=yes
95             ;;
96         --test)
97             testmode=yes
98             ;;
99         --help)
100             usage 0
101             ;;
102         *)
103             usage 1 1>&2
104             ;;
105     esac
106     shift
107 done
108
109
110 if [ $testmode = yes ]; then
111   htdocs="/var/www/www/preview.gnupg.org/htdocs"
112 else
113   htdocs="/var/www/www/www.gnupg.org/htdocs"
114 fi
115 donors="$htdocs/donate/donors.dat"
116 donations="$htdocs/donate/donations.dat"
117 blogheadlinefile="/var/www/www/blog.gnupg.org/htdocs/headlines.txt"
118
119
120 if [ ! -f "$donors" ]; then
121   echo "mkkudos.sh: '$donors' not found" >&2;
122   exit 1
123 fi
124 if [ ! -f "$donations" ]; then
125   echo "mkkudos.sh: '$donations' not found" >&2;
126   exit 1
127 fi
128
129 if [ ! -f "$blogheadlinefile" ]; then
130   echo "mkkudos.sh: '$blogheadlinefile' not found" >&2;
131   blogheadline=""
132 else
133   blogheadline=$(awk -F\| '
134         NR<=3 {printf "<li><a href=\"blog/%s\">%s</a></li>", $1, $2}
135      ' "$blogheadlinefile")
136 fi
137
138 tmp=$(head -1 "$donations")
139 monyear=$(echo "$tmp" | awk -F: 'BEGIN { m[1] = "January";
140       m[2] = "February"; m[3] = "March"; m[4] = "April"; m[5] = "May";
141       m[6] = "June"; m[7] = "July"; m[8] = "August"; m[9] = "September";
142       m[10] = "October"; m[11] = "November"; m[12] = "December"; }
143       {printf "%s %d", m[int($2)] , $1}')
144 thisyear=$(echo "$tmp" | awk -F: '{print $1}')
145 nyr=$(echo "$tmp" | awk -F: '{printf "%d", $9}')
146 euroyr=$(echo "$tmp" | awk -F: '{printf "%d", int($10 + 0.5)}')
147 recur_nyr=$(echo "$tmp" | awk -F: '{printf "%d", $13}')
148 recur_euroyr=$(echo "$tmp" | awk -F: '{printf "%d", int($14 + 0.5)/12}')
149
150 dontable=$(awk -F: <"$donations" -v thisyear="$thisyear" '
151   BEGIN { m[1] = "January";
152           m[2] = "February"; m[3] = "March"; m[4] = "April"; m[5] = "May";
153           m[6] = "June"; m[7] = "July"; m[8] = "August"; m[9] = "September";
154           m[10] = "October"; m[11] = "November"; m[12] = "December" ;
155           printf "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\"";
156           printf " rules=\"groups\" frame=\"hsides\">\n";
157           printf "<colgroup>\n";
158           printf "<col class=\"left\" />\n";
159           printf "<col class=\"right\" />\n";
160           printf "<col class=\"right\" />\n";
161           printf "</colgroup>\n";
162           printf "<thead>\n";
163           printf "<tr>\n";
164           printf "<th scope=\"col\" class=\"left\">Month</th>\n";
165           printf "<th scope=\"col\" class=\"right wideright\">#</th>\n";
166           printf "<th scope=\"col\" class=\"right wideright\">&euro;</th>\n";
167           printf "</tr>\n";
168           printf "</thead>\n";
169           printf "<tbody>\n";
170         }
171   NR==1 { nyear = $9; totalyear = int($10 + 0.5);
172         }
173   $1 != thisyear {
174           printf "</tbody>\n";
175           printf "<tbody>\n";
176           printf "<tr><td class=\"left\">%d</td>\n", thisyear;
177           printf "    <td class=\"right wideright\">%d</td>\n", nyear;
178           printf "    <td class=\"right wideright\">%d</td></tr>\n", totalyear;
179           printf "</tbody>\n";
180           printf "</table>\n";
181           exit 0
182         }
183         { printf "<tr><td class=\"left\">%s</td>\n", m[int($2)];
184           printf "    <td class=\"right wideright\">%d</td>\n", $7;
185           printf "    <td class=\"right wideright\">%d</td></tr>\n",
186                                                   int($8 + 0.5);
187         }
188 ')
189
190
191
192 # Campaign data
193 goal="120000"
194 recur_goal="15000"
195 percent=$(echo "$euroyr:$goal" | awk -F: '{ p = (int($1)*100)/int($2);
196                                           if(p > 100) { p = 100 };
197                                           printf "%d", p}')
198 recur_percent=$(echo "$recur_euroyr:$recur_goal" \
199                                | awk -F: '{ p = (int($1)*100)/int($2);
200                                           if(p > 100) { p = 100 };
201                                           printf "%d", p}')
202
203 for file in "$htdocs/donate/"kudos-????.html "$htdocs/donate/"kudos.html \
204             "$htdocs/donate/"index.html "$htdocs/donate/"index.??.html \
205             "$htdocs/"index.html
206  do
207    [ -f "$file" ] || continue
208    if [ $force = no ]; then
209      [ "$file" -ot "$donors" ] || continue
210    fi
211    if [ "$file" = "$htdocs/donate/"kudos.html ]; then
212      year=$(date +%Y)
213    else
214      year=${file#$htdocs/donate/kudos-}
215      year=${year%.html}
216    fi
217    [ $verbose = yes ] && echo "processing $file" >&2
218    [ -f "$file.tmp" ] && rm "$file.tmp"
219    # We need gawk to use "%'d" in inprint
220    gawk -F: -v year=$year -v donors="$donors" -v dontable="$dontable" \
221            -v monyear="$monyear" -v thisyear="$thisyear" \
222            -v euro="$euro" -v euroyr="$euroyr" \
223            -v nyr="$nyr" -v goal="$goal" -v percent="$percent" \
224            -v recur_nyr="$recur_nyr" -v recur_euroyr="$recur_euroyr" \
225            -v recur_goal="$recur_goal" -v recur_percent="$recur_percent" \
226            -v blogheadline="$blogheadline" \
227             <"$file"  >"$file.tmp" '
228      /<!--BEGIN-DONATIONS-->/ {indon=1; print; insert("") }
229      /<!--END-DONATIONS-->/ {indon=0}
230      /<!--BEGIN-SOME-DONATIONS-->/ {indon=1; print; insertsome("") }
231      /<!--END-SOME-DONATIONS-->/ {indon=0}
232      /<!--BEGIN-DONATIONS_goteo13-->/ {indon=1; print; insert("goteo13") }
233      /<!--END-DONATIONS_goteo13-->/ {indon=0}
234      /<!--BEGIN-DONATION-TABLE-->/ {indon=1; print; print dontable }
235      /<!--END-DONATION-TABLE-->/ {indon=0}
236      /<!--INSERT-MONTH-DATE-->/ {
237            printf "<!--INSERT-MONTH-DATE--> %s\n", monyear;
238            next
239      }
240      /<!--INSERT-THIS-YEAR-->/ {
241            printf "<!--INSERT-THIS-YEAR--> %d\n", thisyear;
242            next
243      }
244      /<!--INSERT-YEAR-EURO-->/ {
245            printf "<!--INSERT-YEAR-EURO--> %s&#x202f;&euro;\n", euroyr;
246            next
247      }
248      /<!--INSERT-YEAR-N-->/ {
249            printf "<!--INSERT-YEAR-N--> %s\n", nyr;
250            next
251      }
252      /<!--INSERT-PROGRESS-LEFT-->/ {
253            printf "<!--INSERT-PROGRESS-LEFT-->%s &euro;\n",
254                   euro;
255            next
256      }
257      /<!--INSERT-PROGRESS-RIGHT-->/ {
258            printf "<!--INSERT-PROGRESS-RIGHT-->goal: %s &euro;\n", goal;
259            next
260      }
261      /<!--REPLACE-PROGRESS-PERCENT-->/ {
262            printf "style=\"width: %d%%\"<!--REPLACE-PROGRESS-PERCENT-->\n",
263                   percent;
264            next
265      }
266      /<!--INSERT-BLOG-HEADLINE-->/ {
267            printf "<!--INSERT-BLOG-HEADLINE--> %s\n", blogheadline;
268            next
269      }
270      /A-CMPGN-RECUR-EURO=""/ {
271            n = index($0,"\"");
272            printf "%s%s\" A-CMPGN-RECUR-EURO=\"\"\n",
273                   substr($0,1,n), recur_euroyr;
274            next
275      }
276      /A-CMPGN-RECUR-EURO-GOAL=""/ {
277            n = index($0,"\"");
278            printf "%s%s\" A-CMPGN-RECUR-EURO-GOAL=\"\"\n",
279                   substr($0,1,n), recur_goal;
280            next
281      }
282      /A-CMPGN-RECUR-PERCENT=""/ {
283            n = index($0,":");
284            printf "%s %s%\" A-CMPGN-RECUR-PERCENT=\"\"\n",
285                   substr($0,1,n), recur_percent;
286            next
287      }
288      /<!--CMPGN-RECUR-EURO-->/ {
289            n = index($0,"<!--CMPGN");
290            printf "%s!--CMPGN-RECUR-EURO-->%s&#x202f;&euro;\n",
291                   substr($0,1,n), format_number(recur_euroyr);
292            next
293      }
294      /<!--CMPGN-RECUR-EURO-GOAL-->/ {
295            n = index($0,"<!--CMPGN");
296            printf "%s!--CMPGN-RECUR-EURO-GOAL-->%s&#x202f;&euro;\n",
297                   substr($0,1,n), format_number(recur_goal);
298            next
299      }
300      /<!--CMPGN-ONCE-EURO-->/ {
301            n = index($0,"<!--CMPGN");
302            printf "%s!--CMPGN-ONCE-EURO-->%s&#x202f;&euro;\n",
303                   substr($0,1,n), format_number(euroyr);
304            next
305      }
306      /<!--CMPGN-RECUR-COUNT-->/ {
307            n = index($0,"<!--CMPGN");
308            printf "%s!--CMPGN-RECUR-COUNT-->%s\n",
309                   substr($0,1,n), format_number(recur_nyr);
310            next
311      }
312      !indon { print }
313
314      function format_number (n) {
315           buf = sprintf("%'"'"'d", int(n));
316           gsub(/,/, "\\&#x202f;", buf);
317           return buf;
318      }
319
320      function insert (tag) {
321        while (getline < donors) {
322          if ( $0 ~ /^(#.*)?$/ )
323             continue;
324          if ( $3 == "" )
325             continue;
326          if ($1==year && $4==tag) {
327            printf "<li>%s</li>\n", $3
328          }
329        }
330        close (donors)
331      }
332
333      function insertsome (tag) {
334        i = 0
335        while (getline < donors) {
336          if ( $0 ~ /^(#.*)?$/ )
337             continue;
338          if ( $3 == "" )
339             continue;
340          if ($4==tag) {
341            data[i++] = $3
342          }
343        }
344        close (donors)
345        j = i > 16 ? ( i - 16 ) : 0
346        while (j < i) {
347            printf "<li>%s</li>\n", data[j++]
348        }
349      }
350      '
351    mv "$file.tmp" "$file" || echo "mkkudos.sh: error updating $file" >&2
352 done