-
Notifications
You must be signed in to change notification settings - Fork 197
/
installTorrServerMac.sh
executable file
·235 lines (227 loc) · 9.45 KB
/
installTorrServerMac.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
#!/bin/bash
dirInstall="/Users/Shared/TorrServer"
serviceName="torrserver"
function getLang() {
lang=$(locale | grep LANG | cut -d= -f2 | tr -d '"' | cut -d_ -f1)
[[ $lang != "ru" ]] && lang="en"
}
function checkArch() {
case $(uname -m) in
i386) architecture="386" ;;
i686) architecture="386" ;;
x86_64) architecture="amd64" ;;
arm64) architecture="arm64" ;;
aarch64) architecture="arm64" ;;
*) [[ $lang == "en" ]] && { echo ""; echo " Unsupported Arch. Can't continue."; exit 1; } || { echo ""; echo " Не поддерживаемая архитектура. Продолжение невозможно."; exit 1; } ;;
esac
}
function getLatestRelease() {
curl -s "https://api.github.com/repos/YouROK/TorrServer/releases" |
grep -iE '"tag_name":|"version":' |
sed -E 's/.*"([^"]+)".*/\1/' |
head -1
}
function killRunning() {
self="$(basename "$0")"
runningPid=$(ps -ax|grep -i torrserver|grep -v grep|grep -v "$self"|awk '{print $1}')
[[ -z $runningPid ]] || sudo kill -9 $runningPid
}
function cleanup() {
sudo rm -f /Library/LaunchAgents/*torrserver*
sudo rm -f /Library/LaunchDaemons/*torrserver*
sudo rm -f $HOME/Library/LaunchAgents/*torrserver*
sudo rm -f $HOME/Library/LaunchDaemons/*torrserver*
killRunning
}
function uninstall() {
[[ $lang == "en" ]] && {
echo ""
echo " TorrServer install dir - ${dirInstall}"
echo ""
echo " This action will delete TorrServer including all it's torrents, settings and files on path above."
echo ""
} || {
echo ""
echo " Директория c TorrServer - ${dirInstall}"
echo ""
echo " Это действие удалит все данные TorrServer включая базу данных торрентов и настройки по указанному выше пути."
echo ""
}
[[ $lang == "en" ]] && read -p ' Are you shure you want to delete TorrServer? (Yes/No) ' answer_del </dev/tty || read -p ' Вы уверены что хотите удалить программу? (Да/Нет) ' answer_del </dev/tty
if [ "$answer_del" != "${answer_del#[YyДд]}" ]; then
cleanup
sudo rm -rf $dirInstall
echo ""
[[ $lang == "en" ]] && echo " TorrServer deleted from Mac" || echo " TorrServer удален c вашего Mac"
echo ""
sleep 5
else
echo ""
echo "OK"
echo ""
sleep 5
fi
}
function installTorrServer() {
[[ $lang == "en" ]] && {
echo ""
echo " Install TorrServer $(getLatestRelease)…"
echo ""
} || {
echo ""
echo " Устанавливаем TorrServer $(getLatestRelease)…"
echo ""
}
user=$(whoami)
binName="TorrServer-darwin-${architecture}"
[[ ! -d "$dirInstall" ]] && mkdir -p ${dirInstall} && chmod a+rw ${dirInstall}
urlBin="https://github.com/YouROK/TorrServer/releases/download/$(getLatestRelease)/${binName}"
if [[ ! -f "$dirInstall/$binName" ]] | [[ ! -x "$dirInstall/$binName" ]] || [[ $(stat -c%s "$dirInstall/$binName" 2>/dev/null) -eq 0 ]]; then
curl -L --progress-bar -# -o "$dirInstall/$binName" "$urlBin"
chmod a+rx "$dirInstall/$binName"
xattr -r -d com.apple.quarantine "$dirInstall/$binName"
fi
[[ $lang == "en" ]] && {
echo ""
echo " Add autostart service for TorrServer $(getLatestRelease)…"
echo ""
echo " System can ask your admin account password"
echo ""
} || {
echo ""
echo " Создаем сервис автозагрузки TorrServer $(getLatestRelease)…"
echo ""
echo " Система может запросить ваш пароль администратора"
echo ""
}
###
cleanup
###
[[ $lang == "en" ]] && read -p ' Change TorrServer web port? (Yes/No) ' answer_cp </dev/tty || read -p ' Хотите изменить веб-порт для TorrServer? (Да/Нет) ' answer_cp </dev/tty
if [ "$answer_cp" != "${answer_cp#[YyДд]}" ]; then
echo ""
[[ $lang == "en" ]] && read -p ' Enter port number: ' answer_port </dev/tty || read -p ' Введите номер порта: ' answer_port </dev/tty
servicePort=$answer_port
echo ""
else
servicePort="8090"
echo ""
fi
cat << EOF > $dirInstall/$serviceName.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${serviceName}</string>
<key>ServiceDescription</key>
<string>TorrServer service for MacOS</string>
<key>LaunchOnlyOnce</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>${dirInstall}/TorrServer-darwin-${architecture}</string>
<string>--port</string>
<string>${servicePort}</string>
<string>--path</string>
<string>${dirInstall}</string>
<string>--logpath</string>
<string>${dirInstall}/torrserver.log</string>
<string>--httpauth</string>
</array>
<key>StandardOutPath</key>
<string>${dirInstall}/torrserver.log</string>
<key>StandardErrorPath</key>
<string>${dirInstall}/torrserver.log</string>
</dict>
</plist>
EOF
[[ $lang == "en" ]] && read -p ' Enable HTTP Authorization? (Yes/No) ' answer_auth </dev/tty || read -p ' Включить авторизацию на сервере? (Да/Нет) ' answer_auth </dev/tty
if [ "$answer_auth" != "${answer_auth#[YyДд]}" ]; then
isAuth=1
else
isAuth=0
fi
echo ""
if [[ "$isAuth" == 1 ]]; then
[[ $lang == "en" ]] && echo " HTTP Auth Install choosen" || echo " Вы выбрали установку с авторизацией"
[[ ! -f "$dirInstall/accs.db" ]] && {
echo ""
[[ $lang == "en" ]] && read -p ' User: ' answer_user </dev/tty || read -p ' Пользователь: ' answer_user </dev/tty
isAuthUser=$answer_user
echo ""
[[ $lang == "en" ]] && read -p ' Password: ' answer_pass </dev/tty || read -p ' Пароль: ' answer_pass </dev/tty
isAuthPass=$answer_pass
echo ""
[[ $lang == "en" ]] && echo " Added credentials: $isAuthUser:$isAuthPass" || echo " Устанавливаем логин и пароль: $isAuthUser:$isAuthPass"
echo ""
echo -e "{\n \"$isAuthUser\": \"$isAuthPass\"\n}" > $dirInstall/accs.db
} || {
echo ""
[[ $lang == "en" ]] && echo " Use ${dirInstall}/accs.db credentials for access" || echo " Используйте реквизиты из ${dirInstall}/accs.db для входа"
echo ""
}
else
sed -i '' -e '/httpauth/d' $dirInstall/$serviceName.plist
fi
[[ $lang == "en" ]] && read -p ' Add autostart for current user (1) or all users (2)? ' answer_cu </dev/tty || read -p ' Добавить автозагрузку для текущего пользователя (1) или для всех (2)? ' answer_cu </dev/tty
if [ "$answer_cu" != "${answer_cu#[1]}" ]; then
# user
sysPath="${HOME}/Library/LaunchAgents"
[[ ! -d "$sysPath" ]] && mkdir -p ${sysPath}
cp "$dirInstall/$serviceName.plist" $sysPath
sudo chown $user "$sysPath/$serviceName.plist"
chmod 0644 "$sysPath/$serviceName.plist"
launchctl load -w "$sysPath/$serviceName.plist" 1>/dev/null 2>&1
else
# root
sysPath="/Library/LaunchDaemons"
[[ ! -d "$sysPath" ]] && sudo mkdir -p ${sysPath}
sudo cp "$dirInstall/$serviceName.plist" $sysPath
sudo chown root:wheel "$sysPath/$serviceName.plist"
sudo chmod 0644 "$sysPath/$serviceName.plist"
sudo launchctl load -w "$sysPath/$serviceName.plist" 1>/dev/null 2>&1
fi
[[ $lang == "en" ]] && {
echo ""
echo " Autostart service added to ${sysPath}"
echo ""
echo " TorrServer $(getLatestRelease) for ${architecture} Mac installed to ${dirInstall}"
echo ""
echo " You can now open browser URL http://localhost:$servicePort to access TorrServer GUI"
echo ""
} || {
echo ""
echo " Сервис автозагрузки записан в ${sysPath}"
echo ""
echo " TorrServer $(getLatestRelease) для ${architecture} Mac установлен в ${dirInstall}"
echo ""
echo " Теперь вы можете открыть браузер по адресу http://localhost:$servicePort для доступа к вебу TorrServer"
echo ""
}
if [[ "$isAuth" == 1 && $isAuthUser > 0 ]]; then
[[ $lang == "en" ]] && echo " Use user \"$isAuthUser\" with password \"$isAuthPass\" for web auth" || echo " Для авторизации введите пользователя $isAuthUser с паролем $isAuthPass"
echo ""
fi
sleep 30
}
while true; do
getLang
echo ""
echo "=============================================================="
[[ $lang == "en" ]] && echo " TorrServer install, update and uninstall script for MacOS " || echo " Скрипт установки, обновления и удаления TorrServer для MacOS "
echo "=============================================================="
echo ""
[[ $lang == "en" ]] && read -p " Do You want to install or update TorrServer? (Yes or No). Enter \"Delete\" to Uninstall TorrServer. " ydn </dev/tty || read -p " Хотите установить или обновить TorrServer? (Да|Нет). Для удаления введите «Удалить». " ydn </dev/tty
case $ydn in
[YyДд]*) checkArch; installTorrServer; break ;;
[DdУу]*) uninstall; break ;;
[NnНн]*) exit ;;
*) [[ $lang == "en" ]] && { echo ""; echo " Enter \"Yes\", \"No\" or \"Delete\"."; } || { echo ""; echo " Ввведите «Да», «Нет» или «Удалить»."; } ;;
esac
done
echo " Have Fun!"
echo ""
sleep 5