-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallFromRemote.sh
More file actions
executable file
·126 lines (102 loc) · 2.82 KB
/
Copy pathInstallFromRemote.sh
File metadata and controls
executable file
·126 lines (102 loc) · 2.82 KB
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
#!/usr/bin/env bash
set -euo pipefail
# InstallFromRemote
# Downloads and installs ONLY the first matching file.
# Usage:
# InstallFromRemote "https://example.com/downloads" ".deb"
# InstallFromRemote "https://example.com/downloads" "noble_amd64.deb"
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <url> <ends-with>"
echo "Example: $0 \"https://example.com/downloads\" \".deb\""
echo "Example: $0 \"https://example.com/downloads\" \"noble_amd64.deb\""
exit 1
fi
PAGE_URL="$1"
EXT="$2"
DOWNLOAD_DIR="$HOME/Downloads"
mkdir -p "$DOWNLOAD_DIR"
need_cmd() { command -v "$1" >/dev/null 2>&1; }
install_deps_deb() {
if need_cmd apt-get; then
sudo apt-get update -y
sudo apt-get install -y curl grep sed awk coreutils
fi
}
for c in curl grep sed awk; do
if ! need_cmd "$c"; then
echo "Missing required command: $c"
echo "Attempting to install dependencies (Debian/Ubuntu/Mint)..."
install_deps_deb
break
fi
done
echo "Fetching HTML from: $PAGE_URL"
HTML="$(curl -fsSL "$PAGE_URL")"
ALL_LINKS="$(
printf '%s' "$HTML" \
| grep -Eoi 'href=["'\''][^"'\'']+["'\'']' \
| sed -E 's/^href=["'\'']//; s/["'\'']$//' \
| sed -E 's/[[:space:]]+//g' \
| awk 'NF' \
| sort -u
)"
if [[ -z "${ALL_LINKS// }" ]]; then
echo "No links found on the page."
exit 0
fi
SCHEME_HOST="$(printf '%s' "$PAGE_URL" | awk -F/ '{print $1"//"$3}')"
PAGE_DIR="$PAGE_URL"
if [[ "$PAGE_DIR" != */ ]]; then
PAGE_DIR="$(dirname "$PAGE_DIR")/"
fi
resolve_url() {
local href="$1"
if [[ "$href" =~ ^https?:// ]]; then
printf '%s' "$href"
elif [[ "$href" =~ ^// ]]; then
local scheme
scheme="$(printf '%s' "$PAGE_URL" | awk -F: '{print $1}')"
printf '%s' "${scheme}:${href}"
elif [[ "$href" =~ ^/ ]]; then
printf '%s' "${SCHEME_HOST}${href}"
else
printf '%s' "${PAGE_DIR}${href}"
fi
}
install_file() {
local path="$1"
if [[ "$path" == *.deb ]]; then
echo "Installing .deb via apt: $path"
sudo apt-get update -y >/dev/null 2>&1 || true
sudo apt-get install -y "$path"
else
echo "Downloaded file is not a .deb package: $path"
echo "No install rule for this file type."
exit 1
fi
}
first_match=""
first_url=""
while IFS= read -r href; do
[[ -z "$href" ]] && continue
full_url="$(resolve_url "$href")"
url_no_query="${full_url%%\?*}"
filename="$(basename "$url_no_query")"
if [[ "$filename" == *"$EXT" ]]; then
first_match="$filename"
first_url="$full_url"
break
fi
done <<< "$ALL_LINKS"
if [[ -z "$first_url" ]]; then
echo "No links found where filename ends with '$EXT'."
exit 0
fi
out_path="$DOWNLOAD_DIR/$first_match"
echo "First matching file:"
echo " $first_url"
echo "Downloading: $first_url"
curl -fL --retry 3 --retry-delay 2 -o "$out_path" "$first_url"
install_file "$out_path"
echo "Done. File saved to: $out_path"
echo "Installed file: $out_path"