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