-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slm.sh
306 lines (280 loc) · 9.11 KB
/
slm.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
#!/bin/bash
cd "$(dirname "$0")" || return
# Set local variables
executable="slm"
dir_current=$(pwd)
dir_download="$dir_current/slm"
dir_download_temp="${dir_download}_temp"
dir_download_dist="$dir_download/dist"
dir_download_dist_executable="$dir_download_dist/$executable"
file_python="$dir_download/$executable.py"
file_spec="$dir_download/$executable.spec"
dir_existing="$dir_current/_internal"
dir_existing_upgrade="$dir_existing/program_files"
dir_download_upgrade="$dir_download/_internal"
link="https://www.dropbox.com/scl/fi/b5loo1yndyfasqgek1vv3/slm_python.zip?rlkey=g1wcyl22kewg05cu55nqssbt7&dl=1"
outfile="slm.zip"
continue_install="false"
continue_startup="false"
continue_port="false"
# Function to check if the OS is Synology
is_synology() {
if [ -f /etc.defaults/VERSION ]; then
return 0
else
return 1
fi
}
# Check the system type (distribution) and install necessary prerequisites
if ! command -v unzip &> /dev/null; then
echo "unzip is not installed. Installing..."
if [[ -f /etc/os-release ]]; then
source /etc/os-release
case "$ID" in
debian|ubuntu|linuxmint)
# Debian/Ubuntu/Mint
sudo apt-get install unzip
;;
fedora|centos|rhel)
# RedHat/CentOS/Fedora
sudo dnf install unzip
;;
arch|manjaro)
# Arch/Manjaro
sudo pacman -S unzip
;;
opensuse)
# OpenSUSE
sudo zypper install unzip
;;
*)
if is_synology; then
# Synology
sudo synopkg install unzip
else
echo "Unknown system type or unsupported distribution."
fi
;;
esac
else
echo "Unable to determine system type."
fi
fi
if ! command -v pip &> /dev/null; then
echo "pip is not installed. Installing..."
if [[ -f /etc/os-release ]]; then
source /etc/os-release
case "$ID" in
debian|ubuntu|linuxmint)
# Debian/Ubuntu/Mint
sudo apt-get install python3-pip
;;
fedora|centos|rhel)
# RedHat/CentOS/Fedora
sudo dnf install python3-pip
;;
arch|manjaro)
# Arch/Manjaro
sudo pacman -S python-pip
;;
opensuse)
# OpenSUSE
sudo zypper install python3-pip
;;
*)
if is_synology; then
# Synology
sudo synopkg install python3-pip
else
echo "Unknown system type or unsupported distribution."
fi
;;
esac
else
echo "Unable to determine system type."
fi
fi
# Check if pip3 is available
if command -v pip3 &> /dev/null
then
PIP_CMD="pip3"
# Check if pip is available
elif command -v pip &> /dev/null
then
PIP_CMD="pip"
else
echo "Neither pip nor pip3 is installed."
exit 1
fi
# Run the executable
if [ $# -eq 0 ]; then
if [ -e "$executable" ]; then
# Run Executable in background and disown it
nohup "./$executable" > /dev/null 2>&1 &
disown
else
# Display error message and pause
echo "Stream Link Manager not installed. Please install using './slm.sh install'."
read -n 1 -s -r -p "Press any key to continue..."
fi
fi
# Handle additional commands
if [ -n "$1" ]; then
case "$1" in
install)
echo "WARNING: Installing will overwrite any existing installation and user files."
while true; do
read -p "Do you wish to continue? (Y/N): " choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ]; then
echo "Beginning installation..."
continue_install="true"
break
elif [ "$choice" == "N" ] || [ "$choice" == "n" ]; then
echo "Installation canceled."
break
else
echo "Invalid input. Please try again."
fi
done
;;
upgrade)
echo "Beginning upgrade..."
continue_install="true"
;;
startup)
echo "Beginning startup routine..."
continue_startup="true"
;;
port)
echo "Beginning port routine..."
continue_port="true"
;;
*)
echo "Invalid command. Usage: 'slm [install | upgrade | startup | port]'"
exit 1
;;
esac
fi
# Installation and Upgrade
if [ $continue_install = "true" ]; then
# Check if the process is running
pid=$(pgrep -f "^./$executable$")
if [ -n "$pid" ]; then
echo "Process $executable is running with PID $pid. Killing it now..."
kill -9 $pid
echo "Process $executable has been killed."
else
echo "Process $executable is not running."
fi
# Download and extract files
echo "Downloading Stream Link Manager files..."
wget -q -O "$outfile" "$link"
sleep 5
echo "Extracting Stream Link Manager files..."
if [ -f "$executable" ]; then
rm -f "$executable"
fi
unzip -q "$outfile" -d "${outfile%.zip}"
sleep 5
echo "Building Stream Link Manager executable..."
cd "$dir_download" || return
$PIP_CMD install -r requirements.txt
pyinstaller --noconfirm --onedir --console --add-data "requirements.txt:." --add-data "static:static/" --add-data "templates:templates/" "$file_python"
rm -f "requirements.txt"
rm -rf static
rm -rf templates
rm -f "$file_python"
rm -f "$file_spec"
rm -rf build
cp -r "$dir_download_dist_executable"/* "$dir_download"
[ -d "$dir_download_dist" ] && rm -rf "$dir_download_dist"
cd "$dir_current" || return
sleep 5
if [ "$1" == "upgrade" ] && [ -d "$dir_existing_upgrade" ]; then
echo "Moving user program files..."
mv -f "$dir_existing_upgrade" "$dir_download_upgrade"
sleep 5
fi
# Clean up files
echo "Cleaning up files..."
rm -f "$outfile"
# [ -f "$executable" ] && rm -f "$executable"
[ -d "$dir_existing" ] && rm -rf "$dir_existing"
sleep 5
# Move files to final destination
echo "Moving files to final destination..."
mv "$dir_download" "$dir_download_temp"
cp -r "$dir_download_temp"/* "$dir_current"
[ -d "$dir_download_temp" ] && rm -rf "$dir_download_temp"
chmod +x "$executable"
sleep 5
if [ "$1" == "upgrade" ]; then
echo "Upgrade completed!"
sleep 5
else
echo "Installation completed!"
fi
fi
# Startup commands
if [ "$continue_startup" = "true" ]; then
script_path=$(realpath "$0")
startup_script_name="${executable}_startup.sh"
echo "Setting up $startup_script_name to run at startup..."
# Create the startup script in the current directory
echo "#!/bin/bash" > "$startup_script_name"
echo "$script_path" >> "$startup_script_name"
chmod +x "$startup_script_name"
# Determine the OS and move the script to the appropriate startup folder
if [[ -f /etc/os-release ]]; then
source /etc/os-release
case "$ID" in
debian|ubuntu|linuxmint)
# Debian/Ubuntu/Mint
sudo mv "$startup_script_name" /etc/init.d/
sudo update-rc.d "$startup_script_name" defaults
;;
fedora|centos|rhel|arch|manjaro|opensuse)
# RedHat/CentOS/Fedora/Arch/Manjaro/OpenSUSE
sudo mv "$startup_script_name" /etc/systemd/system/
sudo systemctl enable "$startup_script_name"
;;
*)
if is_synology; then
# Synology
sudo mv "$startup_script_name" /usr/local/etc/rc.d/
else
echo "Unknown system type or unsupported distribution."
fi
;;
esac
else
echo "Unable to determine system type."
fi
echo "Finished setting up startup commands."
fi
# Port commands
if [ $continue_port = "true" ]; then
while true; do
read -p "Enter a port number (1000-9999) or press Enter to use the default (5000): " port
if [ -z "$port" ]; then
port=5000
break
elif [[ "$port" =~ ^[0-9]+$ ]] && [ "$port" -ge 1000 ] && [ "$port" -le 9999 ]; then
break
else
echo "Invalid input. Please enter a number between 1000 and 9999."
fi
done
export SLM_PORT=$port
echo "SLM_PORT set to $port"
sudo ufw allow $port
echo "Port $port opened in the firewall."
# Append the environment variable to .bashrc
echo "export SLM_PORT=$port" >> ~/.bashrc
echo "SLM_PORT=$port has been added to .bashrc"
# Reminder to source .bashrc manually
echo "Please run 'source ~/.bashrc' to apply the changes to your current shell session."
fi
#Finish and Exit
echo ""
exit 0