41cf2bc1c958998ce6608e55db974da8f55cd9e4
[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" ]; then
13     BASEDIR="/org/bugs.debian.org"
14     USERCONF="bugs/user_prefs";
15 elif [ "$HOSTNAME" == "murphy" ]; then
16     BASEDIR="/var/list/.etc";
17     USERCONF="bugs/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 # figure out if there's an update to be made
27 ORIGREV=$(svn info $SACONFIG |awk '/^Revision:/{print $2}')
28 NEWREV=$(svn info $(svn info $SACONFIG|awk '/^URL:/{print $2}')|awk '/^Revision:/{print $2}')
29
30 # no changes, bug out.
31 if [ $ORIGREV -ge $NEWREV ];
32     exit 0;
33 fi;
34
35 if [ -e "$SACONFIG/.update_spamassassin" ] && kill -0 $(cat "$SACONFIG/.update_spamassassin"); then
36     echo "Another update_spamassassin appears to be running"
37     exit 1;
38 else
39     echo $$ > "$SACONFIG/.update_spamassassin";
40 fi;
41
42 function remove_pidfile(){
43     rm "$SACONFIG/.update_spamassassin";
44 }
45
46 trap remove_pidfile ERR;
47
48 FAILUREMESSAGE="";
49 function report_failure(){
50     # Hrm; a previous update failed; don't report again until the file
51     # is removed
52     if [ -e "$SACONFIG/failed_update" ]; then
53         exit 0;
54     fi;
55     echo "$FAILUREMESSAGE" >> "$SACONFIG/failed_update"
56     echo "$FAILUREMESSAGE";
57     cat - <<EOF;
58
59 Update_spamassassin will not report any additional errors until this
60 is fixed.
61
62 Please fix the problem and then remove
63 $SACONFIG/failed_update
64 EOF
65     exit 1;
66 }
67
68 cp -ar $SACONFIG $TMPDIR/.
69 cd $TMPDIR/spamassassin_config;
70 svn update -q --non-interactive; 
71 if svn status |grep -q '^C'; then
72     FAILUREMESSAGE="$(echo -e 'The following conflicts were found\n')"
73     FAILUREMESSAGE="$FAILUREMESSAGE$(svn status|grep ^C)";
74     report_failure
75 fi;
76 cd - >/dev/null 2>&1
77
78 FAILUREMESSAGE="$(spamassassin -C $TMPDIR/spamassassin_config/$USERCONF --lint)"
79 if [ "$?" != "0" ]; then
80     FAILUREMESSAGE="$(echo -e 'spamassassin -C $TMPDIR/spamassassin_config/$USERCONF --lint\nfailed with\n')$FAILUREMESSAGE";
81     report_failure
82 fi;
83 mv "$SACONFIG" "$BAKDIR/spamassassin_config_r$ORIGREV"
84 mv "$TMPDIR/spamassassin_config" "$SACONFIG"
85 trap ERR
86 rm -rf "$TMPDIR"
87
88 if [ "$HOSTNAME" == "rietz" ]; then
89     touch /home/debbugs/.spamassassin/user_prefs
90 elif [ "$HOSTNAME" == "murphy" ]; then
91     # do nothing
92 else
93     echo "Unknown hostname '$HOSTNAME'";
94     exit 1;
95 fi;
96
97
98 exit 0;