* ditch irrelevant scores
[spamassassin_config.git] / update_spamassassin
1 #!/bin/sh
2
3 # This command updates the spamassassin configuration; tests the new
4 # configuration; then moves the updated configuration in place
5
6 set -e
7
8 SVN=$(which svn)
9
10 TMPDIR=$(mktemp -d);
11
12 if [ "$HOSTNAME" = "rietz" ] || [ "$HOSTNAME" = "busoni" ]; then
13     BASEDIR="/org/bugs.debian.org"
14     USERCONF="bugs/user_prefs";
15 elif [ "$HOSTNAME" = "liszt" ]; then
16     BASEDIR="/var/list/.etc";
17     USERCONF="lists/user_prefs";
18 else
19     echo "Unknown hostname '$HOSTNAME'";
20     exit 1;
21 fi;
22
23 SACONFIG="$BASEDIR/spamassassin_config";
24 BAKDIR="$BASEDIR/spamassassin_config_bak";
25
26 # because alioth continuously fails, make sure that svn info will
27 # succeed first before cluttering the log. Ideally we would report
28 # this error, but it happens far too frequently, so we bail out here.
29 if ! svn info $(svn info $SACONFIG|awk '/^URL:/{print $2}') >/dev/null 2>&1; then
30     exit 0;
31 fi;
32
33 # figure out if there's an update to be made
34 ORIGREV=$(svn info $SACONFIG |awk '/^Revision:/{print $2}')
35 NEWREV=$(svn info $(svn info $SACONFIG|awk '/^URL:/{print $2}')|awk '/^Revision:/{print $2}')
36
37 # no changes, bug out.
38 if [ "0$ORIGREV" -ge "0$NEWREV" ]; then
39     exit 0;
40 fi;
41
42 if [ -e "$SACONFIG/.update_spamassassin" ] && kill -0 $(cat "$SACONFIG/.update_spamassassin") >/dev/null 2>&1; then
43     echo "Another update_spamassassin appears to be running"
44     exit 1;
45 else
46     echo $$ > "$SACONFIG/.update_spamassassin";
47 fi;
48
49 function remove_pidfile(){
50     rm "$SACONFIG/.update_spamassassin";
51 }
52
53 trap remove_pidfile ERR;
54
55 FAILUREMESSAGE="";
56 function report_failure(){
57     # Hrm; a previous update failed; don't report again until the file
58     # is removed or it's more than a day old
59     if [ -e "$SACONFIG/failed_update" ] &&
60         [ $(($(date +%s) - $(stat -c '%Y' "$SACONFIG/failed_update"))) -lt 86400 ];  then
61         rm -rf "$TMPDIR"
62         exit 0;
63     fi;
64     echo "$FAILUREMESSAGE" > "$SACONFIG/failed_update"
65     echo "$FAILUREMESSAGE";
66     cat - <<EOF;
67
68 update_spamassassin will only report this error once a day, and
69 updates will not occur until this is fixed.
70
71 Please fix the problem and then remove
72 $SACONFIG/failed_update
73 EOF
74     rm -rf "$TMPDIR"
75     exit 1;
76 }
77
78 cp -ar $SACONFIG $TMPDIR/.
79 OLDDIR="$(pwd)"
80 cd $TMPDIR/spamassassin_config;
81 svn update -q --non-interactive; 
82 if svn status |grep -q '^C'; then
83     FAILUREMESSAGE=$'The following conflicts were found\n\n'
84     FAILUREMESSAGE="$FAILUREMESSAGE$(svn status|grep ^C)";
85     report_failure
86 fi;
87 cd "$(pwd)"
88
89 if ! spamassassin -p $TMPDIR/spamassassin_config/$USERCONF --lint > $TMPDIR/sa_lint 2>&1; then
90     FAILUREMESSAGE="$(echo -e 'spamassassin -p $TMPDIR/spamassassin_config/$USERCONF --lint\nfailed with\n'| cat - $TMPDIR/sa_lint)";
91     report_failure
92 fi;
93 mv "$SACONFIG" "$BAKDIR/spamassassin_config_r$ORIGREV"
94 mv "$TMPDIR/spamassassin_config" "$SACONFIG"
95 trap - ERR
96 remove_pidfile
97 rm -rf "$TMPDIR"
98
99 if [ "$HOSTNAME" == "rietz" ]; then
100     touch /home/debbugs/.spamassassin/user_prefs
101 elif [ "$HOSTNAME" == "liszt" ]; then
102     # do nothing
103     true;
104 else
105     echo "Unknown hostname '$HOSTNAME'";
106     exit 1;
107 fi;
108
109
110 exit 0;