This repository has been archived by the owner on Jan 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBTSync_Installer.sh
367 lines (281 loc) · 13.1 KB
/
BTSync_Installer.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/bin/bash
# BTSync Installer Script for Linux, v1.01
# Copyright 2014 vinadoros; Distributed under the LGPL v2.1
# All external code/work is copyrighted, and subject to the respective author's licenses.
# See thread at: http://forum.bittorrent.com/topic/26521-btsync-installer-script-for-linux/
# Exit if there is any error in the program.
set -e
###############################################################################
########################### Variables ###########################
###############################################################################
# Set home folder for current user.
HOMEFOLDER=~
# URL's from http://forum.bittorrent.com/topic/24781-latest-1282-build/
btsyncdownurl=http://www.bittorrent.com/sync/downloads
# Test the btsyncdownurl to see if it works.
test2_url=`curl --silent -Is $btsyncdownurl | head -n 1 | sed -r 's/.* ([0-9]*) .*/\1/'`
if [ "$test2_url" != "200" ]; then
echo version $1 not found.
exit 1
fi
# Get the BTSync download page, and then read the version number.
wget -P ~/ $btsyncdownurl
version=$(perl -ne 'print $1 if s/.*Linux i386.glibc 2.3.<.a>.*<span>([\.\d]+)<.span>.*/\1/;' ~/downloads)
rm ~/downloads
BTSYNC_64BITURL=http://syncapp.bittorrent.com/$version/btsync_x64-$version.tar.gz
BTSYNC_32BITURL=http://syncapp.bittorrent.com/$version/btsync_i386-$version.tar.gz
# Set the version of BTSYNC to use, based on 32bit or 64bit machine. Currently only works for x86 and x86_64 architectures.
MACHINEARCH=$(uname -m)
if [ "${MACHINEARCH}" = "x86_64" ]; then
url=$BTSYNC_64BITURL
else
url=$BTSYNC_32BITURL
fi
# Port number for BTSync web-ui
BTPORTNUMBER=8888
###############################################################################
########################## Root Check ###########################
###############################################################################
# This code checks to see if you are running as root/sudo. Script is not designed to run as root, however it can be done. When you go to uninstall the program, you must also run the script as root again.
if [ "$(id -u)" == "0" ]; then
while true; do
read -p "I have noticed you are root, or have run the script as sudo. The script is designed to run as a normal user. Only do this if you are sure you want to use the root user for BTSync. Are you sure you want to do this? Answer no if you are unsure. (y/n)" ROOTQUESTION
case $ROOTQUESTION in
[Yy]* )
echo "You asked to install BTSync as superuser/root."
break;;
[Nn]* )
echo "You asked not to install BTSync as superuser/root. Please exit superuser, or login as a normal user."
exit 1;
break;;
* ) echo "Please input y (yes), or n (no).";;
esac
done
fi
###############################################################################
########################### Functions ###########################
###############################################################################
###############################################################################
# Function wgetbtsync: Get the btsync tarball and install BTSync.
###############################################################################
wgetbtsync(){
# First test if the URL is valid.
test_url=`curl --silent -Is $url | head -n 1 | sed -r 's/.* ([0-9]*) .*/\1/'`
if [ "$test_url" != "200" ]; then
echo version $1 not found.
exit 1
fi
# If the URL is valid, get the version, and untar it.
echo "Installing btsync from $url."
wget --quiet $url -O - | sudo tar -C /usr/local/bin -zxv btsync
}
###############################################################################
# Function installbtsyncconfig: Install local btsync configuration file.
###############################################################################
installbtsyncconfig(){
if [ ! -d $HOMEFOLDER/.sync/ ]; then
echo "Creating $HOMEFOLDER/.sync/"
mkdir $HOMEFOLDER/.sync/
else
echo "$HOMEFOLDER/.sync/ already exists."
fi
if [ ! -f $HOMEFOLDER/.sync/sync.conf ]; then
echo "Creating $HOMEFOLDER/.sync/sync.conf"
# Dump a sync.conf in the user folder. It will overwrite any existing content in the sync.conf.
cat >>$HOMEFOLDER/.sync/sync.conf <<EOL
{
"device_name": "$(uname -n)",
"listening_port" : 0, // 0 - randomize port
/* storage_path dir contains auxilliary app files
if no storage_path field: .sync dir created in the directory
where binary is located.
otherwise user-defined directory will be used
*/
"storage_path" : "$HOMEFOLDER/.sync",
"check_for_updates" : true,
/* "use_upnp" : true, // use UPnP for port mapping
*/
/* limits in kB/s
0 - no limit
*/
/* "download_limit" : 0,
"upload_limit" : 0, */
/* remove "listen" field to disable WebUI
remove "login" and "password" fields to disable credentials check
*/
"webui" :
{
"listen" : "127.0.0.1:$BTPORTNUMBER"
/* ,"login" : "admin",
"password" : "admin" */
}
/* !!! if you set shared folders in config file WebUI will be DISABLED !!!
shared directories specified in config file
override the folders previously added from WebUI.
*/
// Advanced preferences can be added to config file.
// Info is available in BitTorrent Sync User Guide.
}
EOL
else
echo "$HOMEFOLDER/.sync/sync.conf already exists, will not recreate."
fi
#This section of the code creates a .desktop file so you can access the web interface through a shortcut.
# Check to see if the entire folder is there.
if [ ! -d /usr/share/applications ]; then
echo "Creating /usr/share/applications"
mkdir /usr/share/applications
else
echo "/usr/share/applications already exists. Will not create."
fi
# Check to see if btsync-user.desktop exists.
if [ ! -f /usr/share/applications/btsync-user.desktop ]; then
echo "Creating /usr/share/applications/btsync-user.desktop"
# Create a desktop file in the global application entries folder. This file was modified from a version taken from https://github.com/tuxpoldo/btsync-deb/blob/master/btsync-user/scripts/btsync-user.desktop. Credit goes to tuxpoldo on github, and any other authors/contributors related.
sudo sh -c "cat >>/usr/share/applications/btsync-user.desktop" <<EOL
[Desktop Entry]
Name=BitTorrent Sync Web UI
Comment=BitTorrent Sync management interface
Exec=xdg-open http://127.0.0.1:$BTPORTNUMBER
Icon=btsync-user
Terminal=false
Type=Application
Categories=Network
EOL
fi
# Get an icon as well. This file was taken from https://github.com/tuxpoldo/btsync-deb/tree/master/btsync-user/icons/. Credit goes to tuxpoldo on github, and any other authors/contributors related.
if [ ! -f /usr/share/icons/hicolor/16x16/apps/btsync-user.png ]; then
echo "Retreiving /usr/share/icons/hicolor/16x16/apps/btsync-user.png"
sudo wget --quiet https://raw2.github.com/tuxpoldo/btsync-deb/master/btsync-user/icons/16/btsync-user.png -O /usr/share/icons/hicolor/16x16/apps/btsync-user.png
fi
if [ ! -f /usr/share/icons/hicolor/32x32/apps/btsync-user.png ]; then
echo "Retreiving /usr/share/icons/hicolor/32x32/apps/btsync-user.png"
sudo wget --quiet https://raw2.github.com/tuxpoldo/btsync-deb/master/btsync-user/icons/32/btsync-user.png -O /usr/share/icons/hicolor/32x32/apps/btsync-user.png
fi
if [ ! -f /usr/share/icons/hicolor/48x48/apps/btsync-user.png ]; then
echo "Retreiving /usr/share/icons/hicolor/48x48/apps/btsync-user.png"
sudo wget --quiet https://raw2.github.com/tuxpoldo/btsync-deb/master/btsync-user/icons/48/btsync-user.png -O /usr/share/icons/hicolor/48x48/apps/btsync-user.png
fi
if [ ! -f /usr/share/icons/hicolor/96x96/apps/btsync-user.png ]; then
echo "Retreiving /usr/share/icons/hicolor/96x96/apps/btsync-user.png"
sudo wget --quiet https://raw2.github.com/tuxpoldo/btsync-deb/master/btsync-user/icons/96/btsync-user.png -O /usr/share/icons/hicolor/96x96/apps/btsync-user.png
fi
}
###############################################################################
# Function installbtsyncstartup: Install btsync to run on startup as the current user.
###############################################################################
installbtsyncstartup(){
# Create the systemd service.
if [ ! -f /etc/systemd/system/[email protected] ]; then
sudo sh -c "cat >>/etc/systemd/system/[email protected]" <<'EOL'
[Unit]
Description=BTSync for %i
[Service]
Type=simple
User=%i
ExecStart=/usr/local/bin/btsync --nodaemon --config %h/.sync/sync.conf
WorkingDirectory=%h
[Install]
WantedBy=multi-user.target
EOL
fi
# Enable the systemd service as the current user, and start it.
sudo systemctl enable btsync@$USER.service
sudo systemctl start btsync@$USER.service
}
###############################################################################
# Function removebtsync: Remove all traces of BTSync produced by this script.
###############################################################################
removebtsync(){
while true; do
read -p "Are you sure you want to completely remove BTSync? (y/n)?: " RMQUESTION
case $RMQUESTION in
[Yy]* )
echo "You asked to remove BTSync."
while true; do
read -p "Do you want to remove your existing BTSync Configuration files in your home folder? (y/n)?: " BTCONFQUESTION
case $BTCONFQUESTION in
[Yy]* )
echo "You asked to remove the config files. I will delete $HOMEFOLDER/.sync"
if [ -d $HOMEFOLDER/.sync/ ]; then
rm -rf $HOMEFOLDER/.sync
fi
break;;
[Nn]* )
echo "You asked not to remove the config files."
break;;
* ) echo "Please input y (yes) or n (no).";;
esac
done
echo "Deleting systemd startup info."
if [ -f /etc/systemd/system/[email protected] ]; then
sudo systemctl stop btsync@$USER.service
sudo systemctl disable btsync@$USER.service
sudo rm /etc/systemd/system/[email protected]
fi
echo "Deleting BTSync."
if [ -f /usr/local/bin/btsync ]; then
sudo rm /usr/local/bin/btsync
fi
# Delete the application icon and desktop file too.
echo "Deleting BTSync shortcuts."
if [ -f /usr/share/applications/btsync-user.desktop ]; then
sudo rm /usr/share/applications/btsync-user.desktop
fi
if [ -f /usr/share/icons/hicolor/16x16/apps/btsync-user.png ]; then
sudo rm /usr/share/icons/hicolor/16x16/apps/btsync-user.png
fi
if [ -f /usr/share/icons/hicolor/32x32/apps/btsync-user.png ]; then
sudo rm /usr/share/icons/hicolor/32x32/apps/btsync-user.png
fi
if [ -f /usr/share/icons/hicolor/48x48/apps/btsync-user.png ]; then
sudo rm /usr/share/icons/hicolor/48x48/apps/btsync-user.png
fi
if [ -f /usr/share/icons/hicolor/96x96/apps/btsync-user.png ]; then
sudo rm /usr/share/icons/hicolor/96x96/apps/btsync-user.png
fi
break;;
[Nn]* )
echo "You asked not to remove BTSync."
break;;
* ) echo "Please input y (yes) or n (no).";;
esac
done
}
###############################################################################
########################## Main Program ##########################
###############################################################################
while true; do
read -p "Do you want to install (i) BTSync for the first time, update (u) an existing install, remove (r) BTSync, or exit(e)? (i/u/r/e)?: " QUESTION
case $QUESTION in
[Ii]* )
echo "You asked to install BTSync for the first time on this machine."
if [ -f "/etc/systemd/system/[email protected]" -o -f "/usr/local/bin/btsync" -o -f "/usr/share/applications/btsync-user.desktop" ]; then
echo "I have detected existing BTSync files. You will now be asked if you want to remove any previous BTSync files."
removebtsync
fi
wgetbtsync
installbtsyncconfig
installbtsyncstartup
break;;
[Uu]* )
echo "You asked to update BTSync."
# Stop the BTSync service, update BTSync, start it again.
sudo systemctl stop btsync@$USER.service
wgetbtsync
sudo systemctl start btsync@$USER.service
break;;
[Rr]* )
echo "You asked to remove BTSync."
removebtsync
break;;
[Ee]* )
echo "You asked not to install, update, or remove BTSync."
break;;
* ) echo "Please input i (install), u (update), r (remove), or e (exit).";;
esac
done
# Changelog
#
# 1.01 - Updated to add version detection of the BTSync url, as suggested by JimH44 on the BTSync forums.
# 1.0 - Initial Release