6b4d798f129f5bce8a0342dbbda9e2f284d32bc9
[gnupg-doc.git] / tools / append-to-donors.sh
1 #!/bin/sh
2 # append-to-donors.sh
3 # Append new names from the payproc journal tothe donros file.
4
5 pgm="append-to-donors.sh"
6 set -e
7
8
9 htdocs="/var/www/www/www.gnupg.org/htdocs"
10
11 donors="$htdocs/donate/donors.dat"
12 journal_dir="/var/log/payproc"
13 LOCKFILE="$donors.lock"
14
15 if [ ! -f "$donors" ]; then
16   echo "$pgm: '$donors' not found" >&2;
17   exit 1
18 fi
19
20 if ! lockfile -l 7200 -r 2 $LOCKFILE; then
21     echo "$pgm: another instance is still running"
22     exit 0
23 fi
24 trap "rm -f $LOCKFILE" 0
25
26
27 # Find the last entry in donors which we have put in.
28 tmp=$(awk -F: <$donors '
29     /^(#.*)?$/ {next}
30     $5!="" { date=$2;lineno=$5 }
31     END {gsub(/-/,"",date)
32          print date ":" lineno}
33 ')
34 lastdate=$(echo $tmp | cut -d: -f1 | sed 's/T.*//')
35 lastline=$(echo $tmp | cut -d: -f2)
36 [ -z "$lastdate" ] && lastdate=19700101
37 [ -z "$lastline" ] && lastline=0
38
39 cat "$donors" > "$donors.tmp"
40 find $journal_dir -type f -name 'journal-????????.log' -print \
41      | sort | while read fname; do
42     fname=$(basename "$fname")
43     jdate=${fname%.log}
44     jdate=${jdate#journal-}
45     jyear=$(echo $jdate |sed 's/\(....\).*/\1/')
46     if [ "$jdate" -ge "$lastdate" ]; then
47         payproc-jrnl -F_lnr -Fdate -F'[name]' \
48            -S "_lnr > $lastline" -Stype=C -Saccount==1 \
49            --html --print "$journal_dir/journal-$jdate.log" \
50          | while IFS=: read lnr datestr name rest; do
51             if [ -n "$name" ]; then
52                echo "$jyear:$datestr:$name::$lnr:" >> "$donors.tmp"
53             fi
54          done
55     fi
56 done
57 if ! mv "$donors.tmp" "$donors"; then
58   echo "$pgm: error updating $donors" >&2
59   exit 1
60 fi