-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsetup.sh
executable file
·199 lines (175 loc) · 5.44 KB
/
setup.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
#!/usr/bin/env bash
PS1B=$PS1
set -euo pipefail
C_RED="\033[0;31m"
C_GREEN="\033[0;32m"
C_YEllOW="\033[1;33m"
C_NONE="\033[0m"
# Get path of this script, resolving all symlinks.
SOURCE="${BASH_SOURCE[0]}"
SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" > /dev/null 2>&1 && pwd)"
LN_CMD=('ln' '-sf' "$SCRIPT_DIR/tordl.sh")
CMD_BIN_PATH="$HOME/.local/bin"
while [[ -h $SOURCE ]]; do
SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" > /dev/null 2>&1 && pwd)"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE = /* ]] || SOURCE="$SCRIPT_DIR/$SOURCE"
done
if [[ $PATH =~ "$CMD_BIN_PATH" ]]; then
if [ ! -d "$CMD_BIN_PATH" ]; then
printf "${C_YEllOW}You Have $CMD_BIN_PATH in your PATH environment variable but directory does not exist. We will create it.${C_NONE}\n"
mkdir "$CMD_BIN_PATH"
fi
BIN_DIR="$HOME/.local/bin/tordl"
LN_CMD+=("$BIN_DIR")
BIN_DIR_SUDO=0
else
BIN_DIR='/usr/local/bin/tordl'
LN_CMD=('sudo' "${LN_CMD[@]}" "$BIN_DIR")
BIN_DIR_SUDO=1
fi
XDG_CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/torrentdl"
VENV_DIR="$XDG_CONFIG_DIR/.venv"
PYTHON_LATEST_VERSION=12
PYTHON_MIN_VERSION=8
PYTHON_DEFAULT_VERSION="$(python3 -V | tr -d '[A-Za-z]' | awk -F . '{print $2}')"
UNINSTALL=0
UPDATE=0
TORDL_BIN_PATH="${LN_CMD[${#LN_CMD[@]}-1]}"
function print_help {
printf "${C_GREEN}-h | --help${C_NONE} : Print this help\n"
printf "${C_GREEN}-p=* | --python=*${C_NONE} : Use specific python3 minor version (8,9,10,11) [example: setup.sh -p=11]\n"
printf "${C_GREEN}-u | --uninstall${C_NONE} : Uninstall tordl from the System [example: setup.sh -u]\n"
printf "${C_GREEN}-i | --update${C_NONE} : Try to pull latest changes and update search engine definitions\n\n"
}
for i in "$@"; do
case $i in
-p=*|--python=*)
PYTHON_DEFAULT_VERSION=""
PYTHON_LATEST_VERSION="${i#*=}"
if [[ -z $(which "python3.$PYTHON_LATEST_VERSION") ]]; then
printf "${C_RED}User set python3.$PYTHON_LATEST_VERSION not found on the System${C_NONE}"
exit 1
fi
;;
-u|--uninstall)
UNINSTALL=1
;;
-i|--update)
UPDATE=1
;;
-h|--help)
print_help
exit 0
;;
*)
echo "Unknown Argument $i"
exit 1
;;
esac
done
if (( $UPDATE == 1 )); then
printf "${C_GREEN}Trying to update the repo...${C_NONE}\n"
git checkout master
git pull origin master
printf "${C_GREEN}Reloading engine definitions from the code...${C_NONE}\n"
tordl -r
printf "${C_GREEN}Done.${C_NONE}\n"
exit 0
fi
# Uninstall tordl from the System
if (( $UNINSTALL == 1 )); then
printf "${C_GREEN}Uninstalling tordl from the System...${C_NONE}\n"
echo "Removing $XDG_CONFIG_DIR..."
if [[ -e $XDG_CONFIG_DIR ]]; then
rm -rf "$XDG_CONFIG_DIR"
printf "${C_GREEN}${XDG_CONFIG_DIR} Removed ${C_NONE}\n"
else
printf "${C_RED}Unable to remove Config Dir $XDG_CONFIG_DIR, does not exist${C_NONE}\n"
fi
`echo` "Removing Binary Link $TORDL_BIN_PATH..."
if [[ -e $TORDL_BIN_PATH ]]; then
if (( "$BIN_DIR_SUDO" == 0 )); then
rm $TORDL_BIN_PATH
printf "${C_GREEN}${TORDL_BIN_PATH} Removed ${C_NONE}\n"
else
sudo rm $TORDL_BIN_PATH
fi
else
printf "${C_RED}Unable to remove tordl bin Link $TORDL_BIN_PATH, does not exist${C_NONE}\n"
fi
exit 0
fi
printf ${C_GREEN}
if [[ -z "$PYTHON_DEFAULT_VERSION" ]]; then
printf "Using Python Version 3.$PYTHON_LATEST_VERSION\n"
else
printf "Using Python Version 3.$PYTHON_DEFAULT_VERSION\n"
fi
printf ${C_NONE}
if [[ -d $VENV_DIR ]]; then
while true; do
printf ${C_GREEN}
read -p 'Delete and re-install virtualenv? [y/N]: ' choice
printf ${C_NONE}
# Convert $choice to lowercase to keep things clean.
case "${choice,,}" in
'y')
rm -rf "$VENV_DIR"
# Now we can proceed.
break
;;
''|'n')
exit
;;
esac
done
# Not sure how this would happen.
# Maybe corruption or an accidental overwrite?
# Might as well plan for the worst.
elif [[ -e $VENV_DIR ]]; then
printf '%s\n' 'Broken virtualenv detected. Reinstalling...'
# Backup old venv in case it's still of some value.
# Won't overwrite existing files.
mv --backup=numbered "$VENV_DIR"{,.bak}
printf '${C_GREEN}%s %s\n\n' 'Created backup of virtualenv at' "$VENV_DIR.bak${C_NONE}"
fi
if [[ $PYTHON_DEFAULT_VERSION -ge $PYTHON_MIN_VERSION ]]; then
# if possible, use default system python version
PYTHON_BIN='python3'
else
# Default python version too low (or not found) try to find specific version
for (( i="$PYTHON_LATEST_VERSION"; i >= "$PYTHON_MIN_VERSION"; i--)); do
if [[ -n "$(which python3."$i")" ]]; then
PYTHON_BIN="python3.$i"
break
fi
done
if [[ -z $PYTHON_BIN ]]; then
# No suitable python version was found, inform user and exit
printf '${C_RED}%s\n' 'tordl requires Python 3.8 or higher.' \
'Please install it (on debian based systems: $ sudo apt-get install python3.8)${C_NONE}'
exit 1
fi
fi
printf "${C_GREEN}Using Python $PYTHON_BIN, located here: $(which $PYTHON_BIN)${C_NONE}\n"
virtualenv -p $PYTHON_BIN "$VENV_DIR"
PS1=$PS1B
. "$VENV_DIR/bin/activate"
pip3 install -r "$SCRIPT_DIR/requirements.txt"
while true; do
# This ugly expansion is to get the last elem of $LN_CMD in a more compatible way.
printf ${C_GREEN}
read -p "Do you want to link tordl.sh to $TORDL_BIN_PATH ? [Y/n]: " choice
printf ${C_NONE}
case "${choice,,}" in
''|'y')
"${LN_CMD[@]}"
printf "${C_GREEN}tordl successfully linked 'tordl' or 'tordl SEARCH_TERM'${C_NONE}\n"
break
;;
'n')
break
;;
esac
done