-
Notifications
You must be signed in to change notification settings - Fork 6
/
hubic
executable file
·149 lines (130 loc) · 2.97 KB
/
hubic
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /bin/sh
### BEGIN INIT INFO
# Provides: hubic
# Required-Start: dbus networking
# Required-Stop: dbus networking
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the hubiC synchronization client
# Description: starts the hubiC synchronization client
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="hubiC"
NAME=hubic
DAEMON=/usr/bin/dbus-launch
EXEC=/usr/bin/hubic
RUNFILE=/var/run/$NAME.run
SCRIPTNAME=/etc/init.d/$NAME
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Define LSB log_* functions.
. /lib/lsb/init-functions
if [ -z "$EMAIL" ] || [ -z "$PASSWORD_PATH" ] || [ -z "$SYNC_DIR" ]; then
log_failure_msg "The synchronization is not configured in /etc/default/hubic."
exit 3
fi
if [ ! -r "$PASSWORD_PATH" ]; then
log_failure_msg "The password file '$PASSWORD_PATH' is not readable."
exit 3
fi
if [ -n "$EXCLUSIONS_PATH" ] && [ ! -r "$EXCLUSIONS_PATH" ]; then
log_failure_msg "The exclusion file '$EXCLUSIONS_PATH' is not readable."
exit 3
fi
do_exec()
{
. $RUNFILE
DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" start-stop-daemon --start --chuid $USER --exec $EXEC --chdir $PWD -- $@
}
check_exec()
{
if [ $? != 0 ]; then
log_end_msg 1
log_failure_msg "$1"
kill $DBUS_SESSION_BUS_PID 2> /dev/null
rm -f $RUNFILE
exit 3
fi
}
add_exclusions()
{
if [ -n "$EXCLUSIONS_PATH" ]; then
while read -r exclusion; do
if [ -n "`echo "$exclusion" |grep -v "^\s*#"`" ]; then
do_exec exclude add "$SYNC_DIR/$exclusion"
fi
done < "$EXCLUSIONS_PATH"
fi
}
do_reload()
{
check_start
log_begin_msg "Updating the sync directory and the exclusions of $DESC"
do_exec pause
do_exec exclude clear
do_exec syncdir "$SYNC_DIR"
add_exclusions
do_exec resume
log_end_msg 0
}
do_start()
{
if [ -f $RUNFILE ]; then
log_warning_msg "$DESC already running" && exit 0
fi
log_begin_msg "Starting $DESC"
start-stop-daemon --start --chuid $USER --exec $DAEMON > $RUNFILE
check_exec "dbus failed to start"
do_exec login --password_path="$PASSWORD_PATH" $EMAIL "$SYNC_DIR"
check_exec "login failed"
# Wait to avoid the error "Command failed: System.InvalidOperationException: This method requires a connection."
wait_connection
add_exclusions
log_end_msg 0
}
check_start()
{
if [ ! -f $RUNFILE ]; then
log_failure_msg "$DESC is not running" && exit 3
fi
}
check_connection()
{
do_exec status | grep '^Account: ' | cut -d':' -f2
}
wait_connection()
{
while [ -z $(check_connection) ]; do
sleep 1
done
}
do_stop()
{
check_start
log_begin_msg "Stopping $DESC"
do_exec stop
check_exec "error while stopping the client"
kill $DBUS_SESSION_BUS_PID
check_exec "dbus is not running"
rm -f $RUNFILE
log_end_msg 0
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
reload)
do_reload
;;
*)
check_start
do_exec $@
;;
esac