-
-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathinstall.sh
executable file
·252 lines (208 loc) · 7.14 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
#!/usr/bin/env bash
# Downloads the latest tarball from https://github.com/amir1376/ab-download-manager/releases and unpacks it into ~/.local/.
# Creates a .desktop entry for the app in ~/.local/share/applications based on FreeDesktop specifications.
set -euo pipefail
DEPENDENCIES=(curl tar)
LOG_FILE="/tmp/ab-dm-installer.log"
# --- Custom Logger
logger() {
timestamp=$(date +"%Y/%m/%d %H:%M:%S")
if [[ "$1" == "error" ]]; then
# Red color for errors
echo -e "${timestamp} -- ABDM-Installer [Error]: \033[0;31m$@\033[0m" | tee -a ${LOG_FILE}
else
# Default color for non-error messages
echo -e "${timestamp} -- ABDM-Installer [Info]: $@" | tee -a ${LOG_FILE}
fi
}
remove_if_exists() {
local target="$1"
if [ -z "$target" ]; then
logger "No target specified in remove_if_exists function"
return 1
fi
if [ -e "$target" ]; then
logger "File \"$target\" Removed"
rm -rf "$target"
else
logger "File \"$target\" does not exist"
fi
}
# --- Detect OS and The Package Manager to use
detect_package_manager() {
if [ -f /etc/os-release ]; then
source /etc/os-release
local OS=${NAME}
elif type lsb_release >/dev/null 2>&1; then
local OS=$(lsb_release -si)
elif [ -f /etc/lsb-release ]; then
source /etc/lsb-release
local OS="${DISTRIB_ID}"
elif [ -f /etc/debian_version ]; then
local OS=Debian
else
logger error "Your Linux Distro is not Supperted."
logger error "Please install ${DEPENDENCIES[@]} Manually."
exit 1
fi
if `grep -E 'Debian|Ubuntu' <<< $OS > /dev/null` ; then
systemPackage="apt"
elif `grep -E 'Fedora|CentOS|Red Hat|AlmaLinux' <<< $OS > /dev/null`; then
systemPackage="dnf"
fi
}
detect_package_manager
# --- Install dependencies
install_dependencies() {
local answer
read -p "Do you want to install $1? [Y/n]: " -r answer
answer=${answer:-Y} # Set default to 'Y' if no input is given
case $answer in
[Yy]* )
sudo ${systemPackage} update -y
logger "installing $1 package ..."
sudo ${systemPackage} install -y $1
;;
[Nn]* )
logger "Skipping the installation of $1."
;;
* )
logger error "Please answer yes or no."
install_dependencies "$1" # re-prompt for the same package
;;
esac
}
# Check dependencies and install if missing
check_dependencies() {
for pkg in "${DEPENDENCIES[@]}"; do
if ! command -v "$pkg" >/dev/null 2>&1; then
logger "$pkg is not installed. Installing..."
install_dependencies "$pkg"
else
logger "$pkg is already installed."
fi
done
}
APP_NAME="ABDownloadManager"
PLATFORM="linux"
ARCH="x64"
EXT="tar.gz"
RELEASE_URL="https://api.github.com/repos/amir1376/ab-download-manager/releases/latest"
GITHUB_RELEASE_DOWNLOAD="https://github.com/amir1376/ab-download-manager/releases/download"
LATEST_VERSION=$(curl -fSs "${RELEASE_URL}" | grep '"tag_name":' | sed -E 's/.*"tag_name": ?"([^"]+)".*/\1/')
ASSET_NAME="${APP_NAME}_${LATEST_VERSION:1}_${PLATFORM}_${ARCH}.${EXT}"
DOWNLOAD_URL="$GITHUB_RELEASE_DOWNLOAD/${LATEST_VERSION}/$ASSET_NAME"
BINARY_PATH="$HOME/.local/$APP_NAME/bin/$APP_NAME"
# --- Delete the old version Application if exists
delete_old_version() {
# Find the PID(s) of the application
PIDS=$(pidof "$APP_NAME") || true
if [ -n "$PIDS" ]; then
echo "Found $APP_NAME with PID(s): $PIDS. Attempting to kill..."
# Attempt to terminate the process gracefully
kill $PIDS 2>/dev/null || echo "Graceful kill failed..."
# Wait for a short period to allow graceful shutdown
sleep 2
# Check if the process is still running
PIDS=$(pidof "$APP_NAME") || true
if [ -n "$PIDS" ]; then
echo "Process still running. Force killing..."
kill -9 $PIDS 2>/dev/null || echo "Force kill failed..."
else
echo "$APP_NAME terminated successfully."
fi
else
echo "$APP_NAME is not running."
fi
# Remove old version directories
# First Remove link to "$HOME/.local/$APP_NAME"
remove_if_exists "$HOME/.local/bin/$APP_NAME"
# then Remove the main binary files directory
remove_if_exists "$HOME/.local/$APP_NAME"
# Log the removal action
logger "Removed old version of $APP_NAME"
}
# --- Generate a .desktop file for the app
generate_desktop_file() {
cat <<EOF > "$HOME/.local/share/applications/abdownloadmanager.desktop"
[Desktop Entry]
Name=AB Download Manager
Comment=Manage and organize your download files better than before
GenericName=Downloader
Categories=Utility;Network;
Exec=$BINARY_PATH
Icon=$HOME/.local/$APP_NAME/lib/$APP_NAME.png
Terminal=false
Type=Application
StartupWMClass=com-abdownloadmanager-desktop-AppKt
EOF
}
# --- Download the latest version of the app
download_zip() {
# Remove the app tarball if it exists in /tmp
remove_if_exists "/tmp/$ASSET_NAME"
logger "downloading AB Download Manager ..."
# Perform the download with curl
if curl --progress-bar -fSL -o "/tmp/$ASSET_NAME" "${DOWNLOAD_URL}"; then
logger "download finished successfully"
else
logger error "Download failed! Something Went Wrong"
logger error "Check Your Internet Connectivity"
# Optionally remove the partially downloaded file
remove_if_exists "/tmp/$ASSET_NAME"
fi
}
# --- Install the app
install_app() {
logger "Installing AB Download Manager ..."
# --- Setup ~/.local directories
mkdir -p "$HOME/.local/bin" "$HOME/.local/share/applications"
tar -xzf "/tmp/$ASSET_NAME" -C "$HOME/.local"
# --- remove tarball after installation
remove_if_exists "/tmp/$ASSET_NAME"
# Link the binary to ~/.local/bin
ln -sf "$BINARY_PATH" "$HOME/.local/bin/$APP_NAME"
# Create a .desktop file in ~/.local/share/applications
generate_desktop_file
logger "AB Download Manager installed successfully"
logger "it can be found in Applications menu or run '$APP_NAME' in terminal"
logger "Make sure $HOME/.local/bin exists in PATH"
logger "installation logs saved in: ${LOG_FILE}"
}
# --- Check if the app is installed
check_if_installed() {
local installed_version
installed_version=$($APP_NAME --version 2>/dev/null)
if [ -n "$installed_version" ]; then
echo "$installed_version"
else
echo ""
fi
}
# --- Update the app
update_app() {
logger "checking update"
if [ "$1" != "${LATEST_VERSION:1}" ]; then
logger "new version is available: v${LATEST_VERSION:1}. Updating..."
download_zip
delete_old_version
install_app
else
logger "You have the latest version installed."
exit 0
fi
}
main() {
echo "" > "$LOG_FILE"
local installed_version
check_dependencies
installed_version=$(check_if_installed)
if [ -n "$installed_version" ]; then
logger "AB Download Manager v$installed_version is currently installed."
update_app "$installed_version"
else
download_zip
install_app
fi
}
main "$@"