fix the sed for the remote branch
[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 TMPDIR=$(mktemp -d);
9
10 HOSTNAME=$(hostname);
11
12 if [ "$HOSTNAME" = "buxtehude" ]; then
13     BASEDIR="/org/bugs.debian.org"
14     USERCONF="bugs/user_prefs";
15 elif [ "$HOSTNAME" = "bendel" ]; 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
25 if [ -e "$SACONFIG/.update_spamassassin" ] && kill -0 $(cat "$SACONFIG/.update_spamassassin") >/dev/null 2>&1; then
26     echo "Another update_spamassassin appears to be running"
27     exit 1;
28 else
29     echo $$ > "$SACONFIG/.update_spamassassin";
30 fi;
31
32 remove_pidfile() {
33     rm -f "$SACONFIG/.update_spamassassin"
34 }
35
36 OLDDIR="$(pwd)";
37 cd "$SACONFIG";
38 git fetch --all >/dev/null || exit 0;
39 REMOTE_BRANCH="$(git status --porcelain --branch|grep '^##'|sed 's/.*\.\.\.//;s/ \[.*$//;')";
40 CURRENT_HEAD="$(git rev-parse HEAD)"
41 REMOTE_BRANCH_HEAD="$(git rev-parse "$REMOTE_BRANCH")"
42 cd "$OLDDIR";
43 ## there aren't any updates, stop here.
44 if [ "$REMOTE_BRANCH_HEAD" = "$CURRENT_HEAD" ]; then
45     remove_pidfile;
46     exit 0;
47 fi;
48 trap remove_pidfile 0;
49
50 FAILUREMESSAGE="";
51 report_failure () {
52     # Hrm; a previous update failed; don't report again until the file
53     # is removed or it's more than a day old
54     if [ -e "$SACONFIG/failed_update" ] &&
55                [ $(($(date +%s) - $(stat -c '%Y' "$SACONFIG/failed_update"))) -lt 86400 ];  then
56         rm -rf "$TMPDIR";
57         remove_pidfile;
58             exit 0;
59     fi;
60     echo "$FAILUREMESSAGE" > "$SACONFIG/failed_update"
61     echo "$FAILUREMESSAGE";
62     cat - <<EOF;
63
64 update_spamassassin will only report this error once a day, and
65 updates will not occur until this is fixed.
66
67 Please fix the problem and then remove
68 $SACONFIG/failed_update
69 EOF
70     remove_pidfile;
71     rm -rf "$TMPDIR";
72     exit 1;
73 }
74
75 cp -ar $SACONFIG $TMPDIR/.;
76 OLDDIR="$(pwd)";
77 cd $TMPDIR/spamassassin_config;
78 if ! git merge --quiet --ff-only $REMOTE_BRANCH --; then
79     FAILUREMESSAGE="The update from $REMOTE_BRANCH to $REMOTE_BRANCH_HEAD\nis not a fast-forward\n\n"
80     report_failure;
81 fi;
82
83 if ! spamassassin -p $TMPDIR/spamassassin_config/$USERCONF --lint > $TMPDIR/sa_lint 2>&1; then
84     FAILUREMESSAGE="$(echo -e 'spamassassin -p $TMPDIR/spamassassin_config/$USERCONF --lint\nfailed with\n'| cat - $TMPDIR/sa_lint)";
85     report_failure
86 fi;
87 cd "$SACONFIG":
88 git merge --ff-only  --quiet $REMOTE_BRANCH --;
89 trap - 0
90 remove_pidfile
91 rm -rf "$TMPDIR"
92
93 if [ "$HOSTNAME" = "buxtehude" ]; then
94     touch /home/debbugs/.spamassassin/user_prefs
95 elif [ "$HOSTNAME" = "bendel" ]; then
96     # do nothing
97     true;
98 else
99     echo "Unknown hostname '$HOSTNAME'";
100     exit 1;
101 fi;
102
103
104 exit 0;