-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhookline.sh
executable file
·221 lines (173 loc) · 4.45 KB
/
hookline.sh
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
print_help () {
echo
echo "Usage:"
echo
if [ -z "$1" ]; then
echo "hookline [command] <arg1> ..."
echo
echo "Commands:"
echo
echo " help "
echo " add "
echo " cat "
echo " del "
echo " stat "
echo " start "
echo " stop "
echo " stop-all "
echo " tail "
echo
elif [ "$1" == "help" ]; then
if [ -z "$2" ]; then
echo " hookline help <command>"
echo
echo " Get help on a specific command"
echo
elif [ "$2" == "add" ]; then
echo " hookline add <alias> <source> <target>"
echo
echo " Adds a new syncer by alias"
echo
elif [ "$2" == "cat" ]; then
echo " hookline cat <alias>"
echo
echo " Displays the lsyncd config by alias"
echo
elif [ "$2" == "del" ]; then
echo " hookline del <alias>"
echo
echo " Removes a syncer by alias"
echo
elif [ "$2" == "stat" ]; then
echo " hookline stat"
echo
echo " Get the status of all syncers"
echo
elif [ "$2" == "start" ]; then
echo " hookline start <alias>"
echo
echo " Starts a syncer by alias"
echo
elif [ "$2" == "stop" ]; then
echo " hookline stop <alias>"
echo
echo " Stops a syncer by alias"
echo
elif [ "$2" == "stop-all" ]; then
echo " hookline stop-all"
echo
echo " Stops all syncers"
echo
elif [ "$2" == "tail" ]; then
echo " hookline tail"
echo
echo " Tail the log file"
echo
fi
fi
}
hl_dir="$HOME/.hookline"
hl_configs_dir="$hl_dir"
hl_runtime_dir="$hl_dir"
if [ -e "$hl_dir/config" ]; then
. "$hl_dir/config"
fi
if [ ! -d "$hl_dir" ]; then
mkdir "$hl_dir"
fi
if [ ! -d "$hl_configs_dir" ]; then
mkdir "$hl_configs_dir"
fi
if [ ! -d "$hl_runtime_dir" ]; then
mkdir "$hl_runtime_dir"
fi
if [ "$#" == 0 ]; then
print_help
exit -1
elif [ "$1" == "help" ]; then
print_help $1 $2
elif [ "$1" == "add" ]; then
if [ ! "$#" == 4 ]; then
print_help help $1
exit -1
fi
opt="{ checksum = true, update = true, times = false, perms = false, links = true, cvs_exclude = true, _extra = { \"--chmod=ugo=rwX\" } }"
exc="{ \".env\", \".git*\", \"node-modules\" }"
echo "sync {" > "$hl_configs_dir/$2.cfg"
echo " default.rsync," >> "$hl_configs_dir/$2.cfg"
echo " delay = 1," >> "$hl_configs_dir/$2.cfg"
echo " source = \"$3\"," >> "$hl_configs_dir/$2.cfg"
echo " target = \"$4\"," >> "$hl_configs_dir/$2.cfg"
echo " delete = false," >> "$hl_configs_dir/$2.cfg"
echo " exclude = $exc," >> "$hl_configs_dir/$2.cfg"
echo " rsync = $opt" >> "$hl_configs_dir/$2.cfg"
echo "}" >> "$hl_configs_dir/$2.cfg"
echo "0" > "$hl_runtime_dir/$2.pid"
elif [ "$1" == "stop-all" ]; then
for i in `ls -1 "$hl_runtime_dir/"*.pid 2>/dev/null`; do
pid=`cat "$i"`
if [ ! "$pid" == "0" ]; then
cmd=`ps -l $pid | awk '{ print $14 }' | tail -1`
if [ "$cmd" == "lsyncd" ]; then
kill $pid
fi
fi
echo "0" > "$hl_runtime_dir/$2.pid"
done
elif [ "$1" == "cat" ]; then
if [ ! "$#" == 2 ]; then
print_help help $1
exit -1
fi
cat "$hl_configs_dir/$2.cfg"
elif [ "$1" == "del" ]; then
if [ ! "$#" == 2 ]; then
print_help help $1
exit -1
fi
$0 stop $2
rm -f "$hl_configs_dir/$2.cfg"
rm -f "$hl_runtime_dir/$2.pid"
elif [ "$1" == "stat" ]; then
for i in `ls -1 "$hl_configs_dir/"*.cfg 2>/dev/null`; do
stat="off"
aid=`basename "$i" | sed 's/.cfg$//'`
pid=`cat "$hl_runtime_dir/$aid.pid" 2>/dev/null`
if [ ! "$pid" == "0" ] && [ ! "$pid" == "" ]; then
cmd=`ps -l $pid | awk '{ print $14 }' | tail -1`
if [ -z "$cmd" ]; then
stat="off"
elif [ "$cmd" == "lsyncd" ]; then
stat="on"
else
echo "0" > "$hl_runtime_dir/$aid.pid"
fi
fi
echo -e "[$stat]\t\t$aid"
done
elif [ "$1" == "start" ]; then
if [ ! "$#" == 2 ]; then
print_help help $1
exit -1
fi
lsyncd -pidfile "$hl_runtime_dir/$2.pid" -logfile "$hl_runtime_dir/hookline.log" "$hl_configs_dir/$2.cfg"
elif [ "$1" == "stop" ]; then
if [ ! "$#" == 2 ]; then
print_help help $1
exit -1
fi
pid=`cat "$hl_runtime_dir/$2.pid"`
if [ ! "$pid" == "0" ]; then
cmd=`ps -l $pid | awk '{ print $14 }' | tail -1`
if [ "$cmd" == "lsyncd" ]; then
kill $pid
fi
fi
echo "0" > "$hl_runtime_dir/$2.pid"
elif [ "$1" == "tail" ]; then
tail -f "$hl_runtime_dir/hookline.log"
elif [ "$1" == "install" ]; then
sudo cp $0 /usr/bin/hookline
sudo chmod 755 /usr/bin/hookline
fi