chiark / gitweb /
ubuntu-paste: convert to python3/argparse/requests
[bin.git] / sa-learn-cache
1 #! /bin/sh
2 # Depends: chiark-utils-bin
3 set -e
4
5 DIR="$HOME/tmp/sa-learn-cache"
6
7 LOCKED=false
8 if [ "$1" = --locked ]; then
9     LOCKED=:
10     shift
11 fi
12
13 if [ "$1" = --spam ]; then
14     CACHE="$DIR/spam"
15 elif [ "$1" = --ham ]; then
16     CACHE="$DIR/ham"
17 else
18     echo "don't know how to handle anything other than --spam or --ham" >&2
19     exit 1
20 fi
21 LOCK="$CACHE.lock"
22
23 mkdir -p "$DIR"
24
25 if ! $LOCKED; then
26     exec with-lock-ex -w "$LOCK" "$0" --locked "$@"
27 else
28     if [ -e "$CACHE" ]; then
29         SIZE="$(stat -c %s "$CACHE")"
30     else
31         SIZE=0
32     fi
33     cat >> "$CACHE"
34     if [ "$SIZE" -gt 4194304 ]; then
35         sa-learn --mbox --no-sync "$@" < "$CACHE"
36         rm -f "$CACHE"
37     fi
38 fi
39
40 exit 0