aboutsummaryrefslogtreecommitdiff
path: root/local/bin/allsync
blob: db64b4e963ff0ec06cabb086f6e9f5f14df03a08 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env bash
set -eu
cronline="* * * * * /bin/sh -c 'AUTORUN=y /home/cynerd/.local/bin/allsync'"

notify_i3block() {
	pkill -RTMIN+13 i3blocks
}

i3_astroid() {
	pkill -0 astroid || return 0
	find "/run/user/$(id -u)/i3" "/tmp/i3-$(id -un)".* -name ipc-socket\* 2>/dev/null | \
		while read -r socket; do
			i3-msg -s "$socket" "exec astroid $*"
	done
}

cron_enable() {
	pass mail/cynerd@email.cz >/dev/null # Cache keys
	if command -v crontab >/dev/null; then
		{
			crontab -l 
			echo "$cronline"
		} | crontab -
	else
		systemctl --user start allsync.timer
	fi
	notify_i3block
}

cron_disable() {
	if command -v crontab >/dev/null; then
		crontab -l | grep -Fv "$cronline" | crontab -
	else
		systemctl --user stop allsync.timer
	fi
	notify_i3block
}

cron_enabled() {
	if command -v crontab >/dev/null; then
		crontab -l | grep -Fq "$cronline"
	else
		systemctl --user is-active allsync.timer >/dev/null
	fi
}

if [ "$#" -gt 0 ]; then
	case "$1" in
		enable)
			cron_enable
			;;
		disable)
			cron_disable
			;;
		enabled)
			cron_enabled
			;;
		state)
			if cron_enabled; then
				echo "Enabled"
			else
				echo "Disabled"
			fi
			;;
		toggle)
			if cron_enabled; then
				cron_disable
			else
				cron_enable
			fi
			;;
		*)
			echo "${0##*/}: Unknown argument: $1" >&2
			exit 1
			;;
	esac
	exit
fi

##################################################################################

flock_nonblock=""
if [ "${AUTORUN:-n}" = "y" ]; then
	exec &> >(logger -t "${0##*/}")

	if ! gpg-connect-agent 'KEYINFO --list' /bye | awk 'BEGIN {ec=1} $7 == "1" {ec=0} END {exit ec}'; then
		echo "Key not accessible. Disabling cron.." >&2
		cron_disable
		exit 1
	fi
	flock_nonblock="--nonblock"
fi

if [ "${ALLSYNC_FLOCK:-n}" != "y" ]; then
	ALLSYNC_FLOCK=y exec flock $flock_nonblock --exclusive "$HOME/.mail" "$0" "$@"
	echo "Another instance is alredy running" >&2
	exit 1
fi


sec() {
	echo -e '\e[1;34m==========' "$@" '==========\e[0m'
}

ecode=0
fail() {
	echo -e '\e[1;31m---' "$@" '---' "($?)" '\e[0m'
	ecode=1
}


sec "Mail"
i3_astroid --start-polling
mbsync -a || fail "Mail synchronization reported failure"
notmuch new
~/.local/sbin/newmail-notify || fail "Mail notifications not sent"
notmuch tag --batch --input="$HOME/.notmuch-tag-new"
i3_astroid --stop-polling
pkill -RTMIN+13 i3blocks
notify_i3block

sec "Calendar and contacts"
vdirsyncer sync || fail "Calendar and contacts synchronization reported failure"

sec "Passwords"
pass git pull || fail "Passwords pull failed"
pass git push || fail "Passwords push failed"

if [ "${AUTORUN:-n}" = "y" ]; then
	echo "Reported failure exit code: $ecode" >&2
else
   	exit $ecode
fi