aboutsummaryrefslogtreecommitdiff
path: root/templates/.bin/mailsync
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-08-13 14:40:54 -0400
committerBen Burwell <ben@benburwell.com>2019-08-13 14:40:54 -0400
commitc09d91fe95fca4367f47c06fc808cd02a1a8e6a5 (patch)
tree6c5c8dee0c219ab701dc1ff4c39e905dbac08e70 /templates/.bin/mailsync
add bins
Diffstat (limited to 'templates/.bin/mailsync')
-rwxr-xr-xtemplates/.bin/mailsync57
1 files changed, 57 insertions, 0 deletions
diff --git a/templates/.bin/mailsync b/templates/.bin/mailsync
new file mode 100755
index 0000000..9bb013f
--- /dev/null
+++ b/templates/.bin/mailsync
@@ -0,0 +1,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)"