-
Notifications
You must be signed in to change notification settings - Fork 0
/
calsync
executable file
·111 lines (96 loc) · 3.95 KB
/
calsync
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
#!/bin/bash
usage="Usage: $(basename "$0") [-q | --quiet] [-h | --help] <davmail-config> <org file> <khalorg list arg>
Sychronize all \`vdirsyncer\` calendars and export them to org with \`khalorg\`.
Davmail is used as the CalDav server. If it is not already running, it will be
activated temporarly.
where:
options
-q --quiet suppresses the /dev/stderr messages
-h --help shows this help messages
positional
<davmail-config> your davmailconfig
<org file> the output file
<khalorg list arg> see \`khalorg list -h\`
"
error=/dev/stderr
output=/dev/stdout
options=$(getopt -o qh --long quiet,help -- "$@")
[ $? -eq 0 ] || { echo "Incorrect options provided"; exit 1; }
eval set -- "$options"
while true; do
case "$1" in
-q|--quiet) output=/dev/null; error=/dev/null; shift ;;
-h|--help) echo "$usage"; exit ;;
--) shift; break ;;
*) echo "Internal error"; exit 1 ;;
esac
done
[ $# -eq 0 ] && { echo "$usage"; exit ;}
[ $# -lt 5 ] && { echo "Missing positional arguments"; exit ;}
davmailconfig=$1
orgfile=$2
shift 2
khalorg=$@
##############################################################################
# This function is the main function of the script. It checks if the user is
# logged in, if there is an internet connection, starts davmail, runs the
# synchronization and kills davmail afterwards.
##############################################################################
main(){
is_logged_in || { echo "$USER not logged in; sync will not run."; exit ;}
has_internet || { echo "No internet connection detected."; exit ;}
start_davmail
vdirsyncer -v CRITICAL sync || { echo "vdirsyncer failed"; kill_davmail; exit ; }
khalorg_list || { echo "khalorg failed"; kill_davmail; exit; }
kill_davmail
echo 'Calendar sync done.'
}
##############################################################################
# This function checks if the user is logged in. It does so by checking if the
# user has a process with the same name as the user id.
##############################################################################
is_logged_in() {
pgrep -u "${USER:=$LOGNAME}" 1> $output 2> $error
}
##############################################################################
# This function checks if the user has an internet connection. It does so by
# pinging a known ip address.
##############################################################################
has_internet() {
ping -q -c 1 1.1.1.1 1> $output 2> $error
}
##############################################################################
# This function starts davmail if it is not already running. It does so by
# checking if there is a process with the name 'davmail.jar'.
##############################################################################
start_davmail() {
pgrep -f davmail\.jar 1> $output 2> $error && return
davmail $davmailconfig 1> $output 2> $error &
pid_davmail=$!
sleep 0.5
}
##############################################################################
# This function kills davmail if it is running. It does so by checking if there
# is a process with the name 'davmail.jar'.
##############################################################################
kill_davmail() {
[[ $pid_davmail ]] && kill $pid_davmail
}
##############################################################################
# This function runs khalorg list.
##############################################################################
khalorg_list() {
khalorg list $khalorg > $orgfile
}
##############################################################################
# Execute the main function. If the script is run in a tmux session, the output
# will be displayed in a tmux message.
##############################################################################
start_message='Calendar sync started'
if [[ $TMUX ]]; then
tmux display -d 1000 "$start_message"
tmux display -d 2000 "$(main)"
else
echo "$start_message"
main
fi