aboutsummaryrefslogtreecommitdiff
path: root/templates/.bin/mailsync
blob: 9bb013f4fbf94d8874d08e37a67a1ee6b5492479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash

BUNDLE=com.benburwell.mailsync
PLIST="$HOME/Library/LaunchAgents/$BUNDLE.plist"

monitor () {
	local pid=$1 i=0
	while ps "$pid" &>/dev/null; do
		if (( i++ > 10 )); then
			echo "Max checks reached. Sending SIGKILL to $pid..." >&2
			kill -9 "$pid"; return 1
		fi
		sleep 10
	done
	return 0
}

if [[ "$1" == "install" ]]; then
	SCRIPTPATH="$( cd "$(dirname "$0")" || exit 1; pwd -P )/$(basename "$0")"
	cat > "$PLIST" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>$BUNDLE</string>

        <key>ProgramArguments</key>
        <array>
            <string>$SCRIPTPATH</string>
        </array>

        <key>StartInterval</key>
        <integer>180</integer>

        <key>StandardErrorPath</key>
        <string>/tmp/offlineimap.err.log</string>

        <key>StandardOutPath</key>
        <string>/tmp/offlineimap.log</string>
    </dict>
</plist>
EOF
	launchctl load "$PLIST"
	exit 0
elif [[ "$1" == "uninstall" ]]; then
	launchctl unload "$PLIST"
	rm -f "$PLIST"
	exit $?
elif [[ $# -gt 0 ]]; then
	echo "Unrecognized options, did you mean 'install' or 'uninstall'?"
	exit 1
fi

echo "STARTING $(date)"
offlineimap -o & monitor $!
echo "DONE $(date)"