-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.sh
executable file
·222 lines (190 loc) · 6.67 KB
/
functions.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
#!/bin/bash
prompt_yn() {
local prompt=$1
while true; do
read -rp "-> $prompt [Y/n]: " yn
case $yn in
[yY][eE][sS]|[yY]|"")
ret_val=1
break
;;
[nN][oO]|[nN])
ret_val=0
break
;;
*)
echo Invalid input, try again...
esac
done
}
prompt_text() {
local prompt=$1
local regex=$2
local default=$3
if [ -n "$default" ]; then
prompt="$prompt [$default]"
fi
while true; do
read -rp "-> $prompt: " ANSWER
if echo "$ANSWER" | grep -qP "$regex"; then
ret_val=$ANSWER
break
elif [ "$ANSWER" == "" ] && [ -n "$default" ]; then
ret_val=$default
break
else
echo Invalid input, try again...
fi
done
}
set_setting() {
local file=$1
local key=$2
local value=$3
local add_quotes=$4
if [ "$add_quotes" == "y" ]; then
value="\"$value\""
fi
if grep -q "$key=" "$file"; then
# Escape sed characters.
value=$(echo "$value" | sed 's/[\/&]/\\&/g')
sed -i "s/^\s*$key=.*/$key=$value/" "$file"
else
echo "$key=$value" | tee -a "$file" &> /dev/null
fi
}
set_env() {
if [ -f ".env" ]; then
prompt_yn "Found .env file. Load it?"
LOAD_ENV=$ret_val
if [ "$LOAD_ENV" -eq 1 ]; then
# Clean from comments and spaces and make all variables in single quotes.
# shellcheck source=/dev/null
if ! source <(sed -e '/^#/d;/^\s*$/d' -e "s/='\(.*\)'/=\1/g" -e "s/=\"\(.*\)\"/=\1/g" -e "s/=\(.*\)/='\1'/g" .env); then
echo "Failed to load .env file."
exit 1
fi
fi
fi
if [ -z "$VTT_LANGUAGE" ]; then
prompt_text "Enter app's language (available: 'en', 'ru')" "^(en|ru)$" "en"
VTT_LANGUAGE=$ret_val
fi
if [ -z "$TGM_CHANNEL_USERNAME" ] && [ -z "$TGM_CHANNEL_ID" ]; then
prompt_yn "Is your main Telegram channel public?"
IS_MAIN_PUBLIC=$ret_val
if [[ "$IS_MAIN_PUBLIC" -eq 1 ]]; then
prompt_text "Enter your main Telegram channel name" "^\w+$"
TGM_CHANNEL_USERNAME=$ret_val
else
prompt_text "Enter your main Telegram channel id (starts with '-100')" "^-100\d+$"
TGM_CHANNEL_ID=$ret_val
fi
fi
if [[ -z "$TGM_PL_CHANNEL_USERNAME" && -z "$TGM_PL_CHANNEL_ID" ]]; then
prompt_yn "Do you want to forward audio playlists (additional Telegram channel required)?"
HAS_PL=$ret_val
if [[ "$HAS_PL" -eq 1 ]]; then
prompt_yn "Is your playlist Telegram channel public?"
IS_PL_PUBLIC=$ret_val
if [[ "$IS_PL_PUBLIC" -eq 1 ]]; then
prompt_text "Enter your playlist Telegram channel name" "^\w+$"
TGM_PL_CHANNEL_USERNAME=$ret_val
else
prompt_text "Enter your playlist Telegram channel id (starts with '-100')" "^-100\d+$"
TGM_PL_CHANNEL_ID=$ret_val
fi
fi
fi
if [ -z "$SERVER_URL" ]; then
prompt_text "Enter your server URL" "^https?:\/\/.+$"
SERVER_URL=$ret_val
fi
if [ -z "$VK_COMMUNITY_ID" ]; then
prompt_text "Enter your VK community id" "^[0-9]+$"
VK_COMMUNITY_ID=$ret_val
fi
if [ -z "$VK_COMMUNITY_TOKEN" ]; then
prompt_text "Enter your VK community token" "^.+$"
VK_COMMUNITY_TOKEN=$ret_val
fi
if [ -z "$VK_SERVER_TITLE" ]; then
prompt_text "Enter title for your VK Callback API server (1-14 characters)" "^.{1,14}$"
VK_SERVER_TITLE=$ret_val
fi
if [ -z "$TGM_BOT_TOKEN" ]; then
prompt_text "Enter your Telegram bot token" "^.+$"
TGM_BOT_TOKEN=$ret_val
fi
if [ -z "$TGM_API_ID" ]; then
prompt_text "Enter your Telegram app id" "^[0-9]+$"
TGM_API_ID=$ret_val
fi
if [ -z "$TGM_API_HASH" ]; then
prompt_text "Enter your Telegram app hash" "^.+$"
TGM_API_HASH=$ret_val
fi
if [ -z "$TGM_CLIENT_PHONE" ]; then
prompt_text "Enter your Telegram phone number" "^\+?[0-9]+$"
TGM_CLIENT_PHONE=$ret_val
fi
if [ -z "$VK_LOGIN" ]; then
prompt_text "Enter your VK login" "^.+$"
VK_LOGIN=$ret_val
fi
if [ -z "$VK_PASSWORD" ]; then
prompt_text "Enter your VK password" "^.+$"
VK_PASSWORD=$ret_val
fi
echo "Setting up .env variables..."
if [ ! -f ".env" ]; then
touch .env
fi
set_setting ".env" "VTT_LANGUAGE" "$VTT_LANGUAGE" "y"
if [ -z "$TGM_CHANNEL_USERNAME" ]; then
set_setting ".env" "TGM_CHANNEL_USERNAME" "$TGM_CHANNEL_USERNAME" "y"
fi
if [ -z "$TGM_CHANNEL_ID" ]; then
set_setting ".env" "TGM_CHANNEL_ID" "$TGM_CHANNEL_ID" "y"
fi
if [[ "$HAS_PL" -eq 1 ]]; then
if [[ "$IS_PL_PUBLIC" -eq 1 ]]; then
set_setting ".env" "TGM_PL_CHANNEL_USERNAME" "$TGM_PL_CHANNEL_USERNAME" "y"
else
set_setting ".env" "TGM_PL_CHANNEL_ID" "$TGM_PL_CHANNEL_ID" "y"
fi
fi
set_setting ".env" "SERVER_URL" "$SERVER_URL" "y"
set_setting ".env" "VK_COMMUNITY_ID" "$VK_COMMUNITY_ID" "y"
set_setting ".env" "VK_COMMUNITY_TOKEN" "$VK_COMMUNITY_TOKEN" "y"
set_setting ".env" "VK_SERVER_TITLE" "$VK_SERVER_TITLE" "y"
set_setting ".env" "TGM_BOT_TOKEN" "$TGM_BOT_TOKEN" "y"
set_setting ".env" "TGM_API_ID" "$TGM_API_ID" "y"
set_setting ".env" "TGM_API_HASH" "$TGM_API_HASH" "y"
set_setting ".env" "TGM_CLIENT_PHONE" "$TGM_CLIENT_PHONE" "y"
set_setting ".env" "VK_LOGIN" "$VK_LOGIN" "y"
set_setting ".env" "VK_PASSWORD" "$VK_PASSWORD" "y"
NGINX_HOST=$(echo "$SERVER_URL" | grep -Po 'https?://\K[^/]+')
set_setting ".env" "NGINX_HOST" "$NGINX_HOST" "y"
echo "Environment variables are set."
}
setup_nginx() {
if ! command -v nginx &> /dev/null; then
echo "Nginx is not installed! Aborting."
exit 1
fi
if [ -z "$NGINX_HOST" ]; then
prompt_text "Enter your server name" "^.+$"
NGINX_HOST=$ret_val
fi
echo "Setting up Nginx config..."
sudo mkdir -p /etc/nginx/sites-available
sudo cp -f "setup/nginx/vtt-cb-receiver.conf" "/etc/nginx/sites-available/"
if ! grep -qx "\s*server_name\s*$NGINX_HOST;" "/etc/nginx/sites-available/vtt-cb-receiver.conf"; then
sudo sed -i "/^\s*server {/a \ \ \ \ server_name $NGINX_HOST;" "/etc/nginx/sites-available/vtt-cb-receiver.conf"
fi
sudo mkdir -p /etc/nginx/sites-enabled
sudo ln -sf "/etc/nginx/sites-available/vtt-cb-receiver.conf" "/etc/nginx/sites-enabled/"
echo "Reloading Nginx..."
sudo systemctl reload nginx
}