summaryrefslogtreecommitdiffstats
path: root/.config/offlineimap/cron
blob: 592b2a5ab6cef4f87303d112ab6b44038744fa55 (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
#!/bin/sh

set -eu

reap_proc()
{
	local host="${1}"
	local user="${2}"
	shift 2
	local pid=
	local t=0

	{ read -r pid <~/".cache/offlineimap/Account-${host}-${user}.pid"; } \
		2>/dev/null || return 0

	while ps -p ${pid} -o cmd | grep -Fq offlineimap; do
		case ${t} in
			0 | 10) kill ${pid} 1>/dev/null 2>&1 || return 0;;
			    20) return 1;;  # Give up after 20 seconds.
		esac
		sleep 1
		t=$((${t} + 1))
	done

	return 0
}

ping_srvr()
{
	local host="${1}"
	local user="${2}"
	shift 2

	# ping -c 1 -W 10 "${host}" || return 1
	ssh -o ConnectTimeout=10 "${user}@${host}" exit 0 || return 1

	return 0
}

sync()
{
	local host="${1}"
	local user="${2}"
	shift 2

	reap_proc "${host}" "${user}" || return 1
	ping_srvr "${host}" "${user}" || return 1
	offlineimap -a "${host}-${user}" \
		1>>~/".cache/offlineimap/Account-${host}-${user}.log" 2>&1 &
	printf '%d\n' ${!} >~/".cache/offlineimap/Account-${host}-${user}.pid"
}

#sync 'pehjota.net'   'pj'                || :
sync 'libiquity.com' 'patrick.mcdermott' || :