-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinstall.sh
472 lines (432 loc) · 16.8 KB
/
install.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/bin/bash
# Get the directory of the script
scriptdir=$(dirname "$(readlink -f "$0")")
# Function to get the current version of FTPauto
function getCurrentVersion {
if [[ -f "$scriptdir/ftpauto.sh" ]]; then
# Get local version
i_version=$(sed -n '2p' "$scriptdir/ftpauto.sh")
i_version=${i_version#$"s_version=\""}
i_version=${i_version%\"}
else
i_version=0
fi
}
# Function to handle Ctrl+C interruption
function control_c() {
echo -ne '\n'
echo -e " \e[00;31mUser hit CTRL+C, exiting...\e[00m"
echo -e " Run install.sh uninstall to remove created files before trying again!\n"
exit 0
}
trap control_c SIGINT
# Function to compare versions
function version_compare {
if [[ "$1" == "$2" ]]; then
new_version="false"
else
local IFS=.
local n1=($1) n2=($2)
local len=${#n1[@]}
for ((i=0; i<$len; i++)); do
if [[ ${n1[i]:-0} -gt ${n2[i]:-0} ]]; then
new_version="true"
break
elif [[ ${n1[i]:-0} -lt ${n2[i]:-0} ]]; then
new_version="false"
break
fi
done
fi
}
# Function to install/update lftp
function lftp_update {
argument="$1"
# Check if lftp is installed
if [[ -n $(command -v lftp) ]]; then
# Get the latest version of lftp from GitHub
local lftpversion=$(curl -s https://github.com/lavv17/lftp/tags | grep -oP '(?<=tags/v)\d+\.\d+\.\d+' | sort -V | tail -n 1)
# Get the current version of lftp
local c_lftpversion=$(lftp --version | grep -Eo 'Version\ [0-9].[0-9].[0-9]' | cut -d' ' -f2)
# Compare versions
version_compare "$lftpversion" "$c_lftpversion"
if [[ "$new_version" == "true" ]]; then
# Prompt user to update lftp
echo -e " [\e[00;33m$lftpversion available, current version $c_lftpversion\e[00m]"
read -p " Do you wish to update (y/n)? "
if [[ "$REPLY" == "y" ]]; then
# Remove old version of lftp
echo -n "Removing old version ..."
sudo apt-get -y remove lftp &> /dev/null
sudo rm -rf "$scriptdir/dependencies/lftp*"
argument="install"
else
echo -e " lftp update ... [\e[00;33mSKIPPED\e[00m]"
fi
else
# Display that lftp is up to date
echo -e " \e[00;32m [Latest - v$c_lftpversion]\e[00m"
fi
fi
# Install lftp if specified or if it's not installed
if [[ "$argument" == "install" ]]; then
# Install required dependencies
sudo apt-get -y build-dep lftp &> /dev/null
sudo apt-get -y install gcc openssl build-essential automake readline-common libreadline-dev pkg-config ncurses-dev libssl-dev libncurses5-dev libreadline-dev zlib1g-dev &> /dev/null
# Download and install lftp
cd "$scriptdir/dependencies" || exit
local lftpversion=$(curl -s https://github.com/lavv17/lftp/tags | grep -oP '(?<=tags/v)\d+\.\d+\.\d+' | sort -V | tail -n 1)
wget "https://github.com/lavv17/lftp/archive/refs/tags/v$lftpversion.tar.gz" &> /dev/null
mv "$scriptdir/dependencies/v$lftpversion.tar.gz" "$scriptdir/dependencies/lftp-$lftpversion.tar.gz"
tar -xzvf "lftp-$lftpversion.tar.gz" &> /dev/null
rm "$scriptdir/dependencies/lftp-$lftpversion.tar.gz"
cd "lftp-$lftpversion" && ./configure --with-openssl --silent &> /dev/null && make --silent &> /dev/null && sudo checkinstall -y &> /dev/null
# Check if lftp is installed
echo -n " Checking for lftp ..."
if [[ -n $(command -v lftp) ]]; then
echo -e " \e[00;32m [Latest - v$lftpversion]\e[00m"
else
echo -e "INFO: Could not install program using sudo.\nYou have to install \"lftp\" manually... Exiting\n"
exit 0
fi
fi
}
# Function to install/update lftp
function install_lftp {
echo -n " Checking/updating lftp ..."
# Check if lftp is installed
if [[ -z $(command -v lftp) ]]; then
# Prompt user to install lftp if not installed
echo -e "lftp is not installed! It is required for FTPauto to work!"
read -p " Do you want to install it (y/n)? "
if [[ "$REPLY" == "y" ]]; then
# Ask user for installation method
read -p " Do you want latest version (needs to be compiled - SLOW - Any installed version will be removed) or the package from the repo (y/n)? "
if [[ "$REPLY" == "y" ]]; then
# Install lftp using latest version
lftp_update install
else
# Install lftp from repository
sudo apt-get -y install lftp &> /dev/null
# Check if installation was successful
if [[ $? -eq 1 ]]; then
echo "ERROR: Installation of lftp failed. Please install manually before continuing."
echo -e "... Exiting\n"
exit 1
fi
fi
# Check if lftp is installed after installation
echo -n "Checking lftp .."
if [[ -z $(command -v lftp) ]]; then
echo -e " \e[00;32m [OK]\e[00m"
fi
else
# Inform user that FTPauto will not work without lftp and exit
echo "FTPauto will not work without lftp. Exiting..."
echo -e " Run install.sh uninstall to remove created files\n"
exit 0
fi
else
# Update lftp if it is already installed
lftp_update
fi
}
# Function to install/update rar2fs
function rar2fs_update {
argument="$1"
# Check if rar2fs is installed
if [[ -n $(builtin type -p rar2fs) ]]; then
# Get the current version of rar2fs
local c_rar2fsversion=$(rar2fs --version 2> /dev/null | grep -Eo 'rar2fs\sv[0-9][0-9]?\.[0-9][0-9]?\.[0-9][0-9]?' | cut -d' ' -f2 | cut -d'v' -f2)
# Get the latest release version of rar2fs from GitHub
local rar2fsversion=$(curl -s https://api.github.com/repos/hasse69/rar2fs/releases | grep browser_download_url | head -n 1 | cut -d '"' -f 4 | cut -d '/' -f8 | cut -d'v' -f2)
# Compare the versions
version_compare "$rar2fsversion" "$c_rar2fsversion"
if [[ "$new_version" == "true" ]]; then
# Prompt user to update if a new version is available
echo -e " [\e[00;33m$rar2fsversion available, current version $c_rar2fsversion\e[00m]"
read -p " Do you wish to update(y/n)? "
if [[ "$REPLY" == "y" ]]; then
if [[ -n $(builtin type -p rar2fs) ]]; then
# Remove old version and proceed with installation
echo -n "Removing old version ..."
sudo apt-get -y remove rar2fs &> /dev/null
sudo rm -rf "$scriptdir/dependencies/rar2fs*"
argument="install"
fi
else
echo -e " rar2fs update ... [\e[00;33mSKIPPED\e[00m]"
fi
else
echo -e " \e[00;32m [Latest -v$rar2fsversion]\e[00m"
fi
fi
# Install rar2fs if specified or if it's not installed
if [[ "$argument" == "install" ]]; then
cd "$scriptdir/dependencies/"
# Download and install rar2fs
local var=$(curl -s https://api.github.com/repos/hasse69/rar2fs/releases | grep browser_download_url | head -n 1 | cut -d '"' -f 4)
wget -q "$var"
local name=$(basename "$var")
tar zxf "$name" && rm "$name"
name=${name::-7}
cd "$name"
wget -q https://www.rarlab.com/rar/unrarsrc-6.2.12.tar.gz && tar -zxf unrarsrc-6.2.12.tar.gz && rm unrarsrc-6.2.12.tar.gz
cd unrar && make lib &> /dev/null && sudo make install-lib &> /dev/null && cd ..
autoreconf -f -i &> /dev/null && ./configure --silent && make --silent && sudo checkinstall -y &> /dev/null
echo -n " Checking for rar2fs ..."
if [[ -n $(builtin type -p rar2fs) ]]; then
c_rar2fsversion=$(rar2fs --version 2> /dev/null | grep -Eo 'rar2fs\sv[0-9][0-9]?\.[0-9][0-9]?\.[0-9][0-9]?' | cut -d' ' -f2 | cut -d'v' -f2)
echo -e " \e[00;32m [Latest - v$c_rar2fsversion]\e[00m"
else
echo -e "INFO: Could not install program using sudo.\nYou have to install \"rar2fs\" manually... Exiting\n"; exit 0
fi
fi
}
# Function to install/update rar2fs
function install_rar2fs {
echo -n " Checking/updating rar2fs ..."
# Check if rar2fs is installed
if [[ -z $(builtin type -p rar2fs) ]]; then
echo -e "\n \"rar2fs\" is not installed! It is needed to mount rarfiles in order to send video files only. The file(s) will be transferred in original format otherwise"
read -p " Do you want to install it(needs to be compiled - SLOW)(y/n)? "
if [[ "$REPLY" == "y" ]]; then
# Install required dependencies
sudo apt-get -y install libfuse-dev autoconf &> /dev/null
if [[ $? -eq 1 ]]; then
echo -e "INFO: Could not install program using sudo.\nYou have to install \"$i\" manually using root, typing \"su root\"; \"apt-get install $i\"\n... Exiting\n"
exit 0
else
# Proceed with rar2fs installation
rar2fs_update install
fi
else
echo -e "Checking rar2fs ... [\e[00;33mSKIPPED\e[00m] NOTE: \"video\" will not work as send option"
fi
# If rar2fs is installed, check for updates
elif [[ -n $(builtin type -p rar2fs) ]]; then
rar2fs_update
fi
}
# Function to install dependencies
function installDependencies {
# Create necessary directories
echo "Installing dependency tools ..."
mkdir -p "$scriptdir/run" "$scriptdir/users"
echo -n " Creating directories ..."
echo -e "\e[00;32m [OK]\e[00m"
# Check for existence of main files
local main_files=("ftpauto.sh" "dependencies/transfer_main.sh" "dependencies/setup.sh" "dependencies/server_alive_test.sh" "dependencies/server_login.sh" "dependencies/server_list.sh" "dependencies/server_size_management.sh" "dependencies/help.sh" "plugins/largefile.sh" "dependencies/setup.sh" "dependencies/sorting.sh")
for i in "${main_files[@]}"; do
if [[ ! -f "$scriptdir/$i" ]]; then
echo -e "\n\e[00;31m$i not found.\nScript will not work without... Try reinstalling it. Exiting...\e[00m\n"
exit 1
fi
done
echo -n " Checking files ..."
echo -e "\e[00;32m [OK]\e[00m"
# Install lftp
install_lftp
# Install optional tools
echo "Installing optional tools ..."
local programs=("rar" "cksfv")
for i in "${programs[@]}"; do
echo -n " Checking $i ..."
if [[ -z $(builtin type -p "$i") ]]; then
echo -e "\n \"$i\" is not installed. It is required for servers to split large files before sending them. Otherwise large file will be send normally"
read -p " Do you want to install it(y/n)? "
if [[ "$REPLY" == "y" ]]; then
sudo apt-get -y install "$i" &> /dev/null
if [[ $? -eq 1 ]]; then
echo "INFO: Could not install program using sudo."
echo "You have to install \"$i\" manually using root, typing \"su root\"; \"apt-get install $i\""
fi
if [[ -z $(builtin type -p "$i") ]]; then
echo -e " \e[00;32m [OK]\e[00m"
fi
else
echo -e "Checking $i ... [\e[00;33mSKIPPED\e[00m] NOTE: \"split_files\" and \"create_sfv\" will not work"
break
fi
else
echo -e "\e[00;32m [OK]\e[00m"
fi
done
# Install rar2fs
install_rar2fs
# Prompt for user installation
echo "Finalizing ..."
read -p " Do you want to install a user? (You can add user later on)(y/n)? "
if [[ "$REPLY" == "y" ]]; then
read -p " What username do you want to use (leave empty for 'default')? "
if [[ -n "$REPLY" ]]; then
username="$REPLY"
else
username="default"
fi
echo -n " Adding user, $username ..."
source "$scriptdir/dependencies/help.sh"
write_config
if [[ -f "$scriptdir/users/$username/config" ]]; then
read -p " Do you want to configure $user now(y/n)? "
if [[ "$REPLY" == "y" ]]; then
nano "$scriptdir/users/$user/config"
else
echo "NOTE: You can edit the user later using bash ftpauto.sh --user=$user --edit"
fi
else
echo -e "\nUser already exists"
fi
else
echo -e " Adding user ... [\e[00;33mSKIPPED\e[00m] NOTE: See ftpauto.sh --help for more info"
fi
echo -e "\n\e[00;32m[Installation done]\e[00m\nEnjoy! Start using FTPauto by using ftpauto.sh --help\n"
exit 0
}
# Function to handle installation
function install {
# Prompt the user to confirm installation
read -p "Do you wish to install? (y/n): "
if [[ "$REPLY" == "n" ]]; then
echo -e "... Exiting\n"
exit 0
fi
# Install minimum required tools
echo -e "\nInstalling minimum required tools ..."
local programs=( "bc" "curl" "openssl" "date" "tail" "awk" "mkdir" "sed" "tput" "nano" "cut" "checkinstall")
for i in "${programs[@]}"; do
echo -n "Checking for $i ..."
if [[ -z $(builtin type -p "$i") ]]; then
echo -e "[Not found]"
sudo apt-get -y install "$i" &> /dev/null
if [[ $? -eq 1 ]]; then
echo -e "INFO: Could not install program using sudo.\nYou have to install \"$i\"... Exiting\n"
exit 0
fi
echo -n " Checking $i ..."
if [[ -n $(builtin type -p "$i") ]]; then
echo -e "[OK]"
else
echo -e "Script will not work without... exiting\n"
exit 0
fi
else
echo -e "[OK]"
fi
done
# Update the script to the latest version
updateScript installNew
# Install dependencies
installDependencies
}
# Function to uninstall FTPauto and its dependencies
function uninstall {
# List of programs to be removed
local programs=("lftp" "rar" "cksfv" "rar2fs" "pkg-config" "automake" "libfuse-dev" "checkinstall" "libssl-dev" "libncurses5-dev" "libreadline-dev" "zlib1g-dev" "autoconf")
# Prompt the user to confirm uninstallation method
echo "The following programs will be removed: ${programs[@]}"
read -p "Do you want to remove all at once (y) or one by one (n)? (all=y/one-by-one(safe)=n): "
# If user chooses to remove all at once
if [[ "$REPLY" == "y" ]]; then
# Confirm the user's intention to remove all programs
read -p "WARNING: THIS WILL REMOVE ALL PROGRAMS. ARE YOU SURE? (y/n): "
if [[ "$REPLY" == "y" ]]; then
# Remove all programs
for i in "${programs[@]}"; do
sudo apt-get -y remove "$i" &> /dev/null
echo -e "$i [REMOVED]"
done
fi
else
# If user chooses to remove one by one
for i in "${programs[@]}"; do
# Check if program exists before attempting to remove it
if builtin type -p "$i" &>/dev/null; then
echo -n "Removing $i ..."
# Prompt the user to confirm removal of each program
read -p "Do you want to remove it? (y/n): "
if [[ "$REPLY" == "y" ]]; then
sudo apt-get -y remove "$i" &> /dev/null
echo -e "[REMOVED]"
else
echo -e "[KEPT]"
fi
fi
done
fi
# Remove FTPauto and its dependencies
echo -n "Removing source files ..."
sudo rm -rf "$scriptdir/dependencies/" "$scriptdir/plugins/"
rm -f "$scriptdir/ftpauto.sh" "$scriptdir/LICENCE" "$scriptdir/README.md" "$scriptdir/ftpauto.sh"
echo -e "[OK]"
echo -n "Removing user files ..."
rm -rf "$scriptdir/run" "$scriptdir/users"
echo -n "Removing remainder of FTPauto ..."
rm -rf "$scriptdir"
# Inform user of successful removal
echo -e "[OK]\nComplete removal of FTPauto and dependencies!\n"
exit 0
}
# Function to handle downloading the script
function downloadScript {
echo -n " Downloading FTPauto..."
wget -q "https://github.com/Meliox/FTPauto/archive/FTPauto-v$release_version.tar.gz"
echo -e "\e[00;32m [OK]\e[00m"
echo -n " Extracting ..."
tar -xzf "${scriptdir}/FTPauto-v${release_version}.tar.gz" --overwrite --strip-components 1
rm -f "${scriptdir}/FTPauto-v${release_version}.tar.gz"
echo -e "\e[00;32m [OK]\e[00m"
sed "6c lastUpdate=$(date +'%s')" -i "$scriptdir/ftpauto.sh" # set update time
sed "7c message=\"\"" -i "$scriptdir/ftpauto.sh" # reset update message
echo -e "\nPlease execute installer again.\n";
exit 0;
}
# Function to handle updating the script
function updateScript {
argument="$1"
getCurrentVersion
echo -n " Checking/updating FTPauto ..."
local release_version=$(curl -s https://api.github.com/repos/Meliox/ftpauto/tags | grep -oP '"name": "\KFTPauto-v\d+\.\d+\.\d+' | sort -V | tail -n 1 | cut -d'v' -f2)
version_compare "$release_version" "$i_version"
if [[ "$i_version" == "0" ]] && [[ $argument != installNew ]]; then
echo -e "\e[00;31m [ERROR]\e[00m\nNo installation found. Execute script with install as argument instead. Exiting.\n"
exit 0
elif [[ "$new_version" == "true" ]] && [[ $argument == installNew ]]; then
echo -e "\e[00;32m [Found - v$release_version]\e[00m"
read -p " Do you wish to update(y/n)? "
if [[ "$REPLY" == "y" ]]; then
downloadScript
else
echo -e " FTPauto update ... [\e[00;33mSKIPPED\e[00m]"
fi
elif [[ "$new_version" == "true" ]] && [[ $argument != installNew ]]; then
echo -e "\e[00;33m [Found - v$release_version]\e[00m"
read -p " Do you wish to update(y/n)? "
if [[ "$REPLY" == "y" ]]; then
downloadScript
else
echo -e " FTPauto update ... [\e[00;33mSKIPPED\e[00m]"
fi
else
if [[ "$argument" == "installNew" ]]; then
echo -e " \e[00;32m[Local - v$i_version, Latest - v$release_version]\e[00m"
else
echo -e " \e[00;32m [Local - v$i_version, Latest - v$i_version]\e[00m"
fi
fi
}
case "$1" in
installNew)
updateScript installNew
;;
install)
install
;;
uninstall)
uninstall
;;
*)
echo "Usage: $0 {install|uninstall}" >&2
exit 1
;;
esac