tools: Update donation tools for recurring donations
[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)}')
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 \
205             "$htdocs/"index.html
206  do
207    if [ $force = no ]; then
208      [ "$file" -ot "$donors" ] || continue
209    fi
210    if [ "$file" = "$htdocs/donate/"kudos.html ]; then
211      year=$(date +%Y)
212    else
213      year=${file#$htdocs/donate/kudos-}
214      year=${year%.html}
215    fi
216    [ $verbose = yes ] && echo "processing $file" >&2
217    [ -f "$file.tmp" ] && rm "$file.tmp"
218    awk -F: -v year=$year -v donors="$donors" -v dontable="$dontable" \
219            -v monyear="$monyear" -v thisyear="$thisyear" \
220            -v euro="$euro" -v euroyr="$euroyr" \
221            -v nyr="$nyr" -v goal="$goal" -v percent="$percent" \
222            -v recur_nyr="$recur_nyr" -v recur_euroyr="$recur_euroyr" \
223            -v recur_goal="$recur_goal" -v recur_percent="$recur_percent" \
224            -v blogheadline="$blogheadline" \
225             <"$file"  >"$file.tmp" '
226      /<!--BEGIN-DONATIONS-->/ {indon=1; print; insert("") }
227      /<!--END-DONATIONS-->/ {indon=0}
228      /<!--BEGIN-SOME-DONATIONS-->/ {indon=1; print; insertsome("") }
229      /<!--END-SOME-DONATIONS-->/ {indon=0}
230      /<!--BEGIN-DONATIONS_goteo13-->/ {indon=1; print; insert("goteo13") }
231      /<!--END-DONATIONS_goteo13-->/ {indon=0}
232      /<!--BEGIN-DONATION-TABLE-->/ {indon=1; print; print dontable }
233      /<!--END-DONATION-TABLE-->/ {indon=0}
234      /<!--INSERT-MONTH-DATE-->/ {
235            printf "<!--INSERT-MONTH-DATE--> %s\n", monyear;
236            next
237      }
238      /<!--INSERT-THIS-YEAR-->/ {
239            printf "<!--INSERT-THIS-YEAR--> %d\n", thisyear;
240            next
241      }
242      /<!--INSERT-YEAR-EURO-->/ {
243            printf "<!--INSERT-YEAR-EURO--> %s&thinsp;&euro;\n", euroyr;
244            next
245      }
246      /<!--INSERT-YEAR-N-->/ {
247            printf "<!--INSERT-YEAR-N--> %s\n", nyr;
248            next
249      }
250      /<!--INSERT-PROGRESS-LEFT-->/ {
251            printf "<!--INSERT-PROGRESS-LEFT-->%s &euro;\n",
252                   euro;
253            next
254      }
255      /<!--INSERT-PROGRESS-RIGHT-->/ {
256            printf "<!--INSERT-PROGRESS-RIGHT-->goal: %s &euro;\n", goal;
257            next
258      }
259      /<!--REPLACE-PROGRESS-PERCENT-->/ {
260            printf "style=\"width: %d%%\"<!--REPLACE-PROGRESS-PERCENT-->\n",
261                   percent;
262            next
263      }
264      /<!--INSERT-BLOG-HEADLINE-->/ {
265            printf "<!--INSERT-BLOG-HEADLINE--> %s\n", blogheadline;
266            next
267      }
268      /A-CMPGN-RECUR-EURO=""/ {
269            n = index($0,"\"");
270            printf "%s%s\" A-CMPGN-RECUR-EURO=\"\"\n",
271                   substr($0,1,n), recur_euroyr;
272            next
273      }
274      /A-CMPGN-RECUR-EURO-GOAL=""/ {
275            n = index($0,"\"");
276            printf "%s%s\" A-CMPGN-RECUR-EURO-GOAL=\"\"\n",
277                   substr($0,1,n), recur_goal;
278            next
279      }
280      /A-CMPGN-RECUR-PERCENT=""/ {
281            n = index($0,":");
282            printf "%s %s%\" A-CMPGN-RECUR-PERCENT=\"\"\n",
283                   substr($0,1,n), recur_percent;
284            next
285      }
286      /<!--CMPGN-RECUR-EURO-->/ {
287            n = index($0,"<!--CMPGN");
288            printf "%s!--CMPGN-RECUR-EURO-->%s&thinsp;&euro;\n",
289                   substr($0,1,n), recur_euroyr;
290            next
291      }
292      /<!--CMPGN-RECUR-EURO-GOAL-->/ {
293            n = index($0,"<!--CMPGN");
294            printf "%s!--CMPGN-RECUR-EURO-GOAL-->%s&thinsp;&euro;\n",
295                   substr($0,1,n), recur_goal;
296            next
297      }
298      /<!--CMPGN-ONCE-EURO-->/ {
299            n = index($0,"<!--CMPGN");
300            printf "%s!--CMPGN-ONCE-EURO-->%s&thinsp;&euro;\n",
301                   substr($0,1,n), euroyr;
302            next
303      }
304      /<!--CMPGN-RECUR-COUNT-->/ {
305            n = index($0,"<!--CMPGN");
306            printf "%s!--CMPGN-RECUR-COUNT-->%s\n",
307                   substr($0,1,n), recur_nyr;
308            next
309      }
310      !indon { print }
311
312      function insert (tag) {
313        while (getline < donors) {
314          if ( $0 ~ /^(#.*)?$/ )
315             continue;
316          if ( $3 == "" )
317             continue;
318          if ($1==year && $4==tag) {
319            printf "<li>%s</li>\n", $3
320          }
321        }
322        close (donors)
323      }
324
325      function insertsome (tag) {
326        i = 0
327        while (getline < donors) {
328          if ( $0 ~ /^(#.*)?$/ )
329             continue;
330          if ( $3 == "" )
331             continue;
332          if ($4==tag) {
333            data[i++] = $3
334          }
335        }
336        close (donors)
337        j = i > 16 ? ( i - 16 ) : 0
338        while (j < i) {
339            printf "<li>%s</li>\n", data[j++]
340        }
341      }
342      '
343    mv "$file.tmp" "$file" || echo "mkkudos.sh: error updating $file" >&2
344 done