campaign: Print only the one-time donations since the new page is up.
[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 # Watchout for the 9074 below which are the donations received before the
194 # campaign start.
195 goal="120000"
196 recur_goal="15000"
197 percent=$(echo "$euroyr:$goal" | awk -F: '{ p = (int($1)*100)/int($2);
198                                           if(p > 100) { p = 100 };
199                                           printf "%d", p}')
200 recur_percent=$(echo "$recur_euroyr:$recur_goal" \
201                                | awk -F: '{ p = (int($1)*100)/int($2);
202                                           if(p > 100) { p = 100 };
203                                           printf "%d", p}')
204
205 for file in "$htdocs/donate/"kudos-????.html "$htdocs/donate/"kudos.html \
206             "$htdocs/donate/"index.html "$htdocs/donate/"index.??.html \
207             "$htdocs/"index.html
208  do
209    [ -f "$file" ] || continue
210    if [ $force = no ]; then
211      [ "$file" -ot "$donors" ] || continue
212    fi
213    if [ "$file" = "$htdocs/donate/"kudos.html ]; then
214      year=$(date +%Y)
215    else
216      year=${file#$htdocs/donate/kudos-}
217      year=${year%.html}
218    fi
219    [ $verbose = yes ] && echo "processing $file" >&2
220    [ -f "$file.tmp" ] && rm "$file.tmp"
221    # We need gawk to use "%'d" in inprint
222    gawk -F: -v year=$year -v donors="$donors" -v dontable="$dontable" \
223            -v monyear="$monyear" -v thisyear="$thisyear" \
224            -v euro="$euro" -v euroyr="$euroyr" \
225            -v nyr="$nyr" -v goal="$goal" -v percent="$percent" \
226            -v recur_nyr="$recur_nyr" -v recur_euroyr="$recur_euroyr" \
227            -v recur_goal="$recur_goal" -v recur_percent="$recur_percent" \
228            -v blogheadline="$blogheadline" \
229             <"$file"  >"$file.tmp" '
230      /<!--BEGIN-DONATIONS-->/ {indon=1; print; insert("") }
231      /<!--END-DONATIONS-->/ {indon=0}
232      /<!--BEGIN-SOME-DONATIONS-->/ {indon=1; print; insertsome("") }
233      /<!--END-SOME-DONATIONS-->/ {indon=0}
234      /<!--BEGIN-DONATIONS_goteo13-->/ {indon=1; print; insert("goteo13") }
235      /<!--END-DONATIONS_goteo13-->/ {indon=0}
236      /<!--BEGIN-DONATION-TABLE-->/ {indon=1; print; print dontable }
237      /<!--END-DONATION-TABLE-->/ {indon=0}
238      /<!--INSERT-MONTH-DATE-->/ {
239            printf "<!--INSERT-MONTH-DATE--> %s\n", monyear;
240            next
241      }
242      /<!--INSERT-THIS-YEAR-->/ {
243            printf "<!--INSERT-THIS-YEAR--> %d\n", thisyear;
244            next
245      }
246      /<!--INSERT-YEAR-EURO-->/ {
247            printf "<!--INSERT-YEAR-EURO--> %s&#x202f;&euro;\n", euroyr;
248            next
249      }
250      /<!--INSERT-YEAR-N-->/ {
251            printf "<!--INSERT-YEAR-N--> %s\n", nyr;
252            next
253      }
254      /<!--INSERT-PROGRESS-LEFT-->/ {
255            printf "<!--INSERT-PROGRESS-LEFT-->%s &euro;\n",
256                   euro;
257            next
258      }
259      /<!--INSERT-PROGRESS-RIGHT-->/ {
260            printf "<!--INSERT-PROGRESS-RIGHT-->goal: %s &euro;\n", goal;
261            next
262      }
263      /<!--REPLACE-PROGRESS-PERCENT-->/ {
264            printf "style=\"width: %d%%\"<!--REPLACE-PROGRESS-PERCENT-->\n",
265                   percent;
266            next
267      }
268      /<!--INSERT-BLOG-HEADLINE-->/ {
269            printf "<!--INSERT-BLOG-HEADLINE--> %s\n", blogheadline;
270            next
271      }
272      /A-CMPGN-RECUR-EURO=""/ {
273            n = index($0,"\"");
274            printf "%s%s\" A-CMPGN-RECUR-EURO=\"\"\n",
275                   substr($0,1,n), recur_euroyr;
276            next
277      }
278      /A-CMPGN-RECUR-EURO-GOAL=""/ {
279            n = index($0,"\"");
280            printf "%s%s\" A-CMPGN-RECUR-EURO-GOAL=\"\"\n",
281                   substr($0,1,n), recur_goal;
282            next
283      }
284      /A-CMPGN-RECUR-PERCENT=""/ {
285            n = index($0,":");
286            printf "%s %s%\" A-CMPGN-RECUR-PERCENT=\"\"\n",
287                   substr($0,1,n), recur_percent;
288            next
289      }
290      /<!--CMPGN-RECUR-EURO-->/ {
291            n = index($0,"<!--CMPGN");
292            printf "%s!--CMPGN-RECUR-EURO-->%s&#x202f;&euro;\n",
293                   substr($0,1,n), format_number(recur_euroyr);
294            next
295      }
296      /<!--CMPGN-RECUR-EURO-GOAL-->/ {
297            n = index($0,"<!--CMPGN");
298            printf "%s!--CMPGN-RECUR-EURO-GOAL-->%s&#x202f;&euro;\n",
299                   substr($0,1,n), format_number(recur_goal);
300            next
301      }
302      /<!--CMPGN-ONCE-EURO-->/ {
303            n = index($0,"<!--CMPGN");
304            printf "%s!--CMPGN-ONCE-EURO-->%s&#x202f;&euro;\n",
305                   substr($0,1,n), format_number( int(euroyr) - 9074 );
306            next
307      }
308      /<!--CMPGN-RECUR-COUNT-->/ {
309            n = index($0,"<!--CMPGN");
310            printf "%s!--CMPGN-RECUR-COUNT-->%s\n",
311                   substr($0,1,n), format_number(recur_nyr);
312            next
313      }
314      !indon { print }
315
316      function format_number (n) {
317           buf = sprintf("%'"'"'d", int(n));
318           gsub(/,/, "\\&#x202f;", buf);
319           return buf;
320      }
321
322      function insert (tag) {
323        while (getline < donors) {
324          if ( $0 ~ /^(#.*)?$/ )
325             continue;
326          if ( $3 == "" )
327             continue;
328          if ($1==year && $4==tag) {
329            printf "<li>%s</li>\n", $3
330          }
331          else if ($1==year && $4=="S") {
332            printf "<li>%s*</li>\n", $3
333          }
334        }
335        close (donors)
336      }
337
338      function insertsome (tag) {
339        i = 0
340        while (getline < donors) {
341          if ( $0 ~ /^(#.*)?$/ )
342             continue;
343          if ( $3 == "" )
344             continue;
345          if ($4==tag) {
346            data[i++] = $3
347          }
348          else if ($4=="S") {
349            data[i++] = $3 "*"
350          }
351        }
352        close (donors)
353        j = i > 16 ? ( i - 16 ) : 0
354        while (j < i) {
355            printf "<li>%s</li>\n", data[j++]
356        }
357      }
358      '
359    mv "$file.tmp" "$file" || echo "mkkudos.sh: error updating $file" >&2
360 done