-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
pkg
executable file
·184 lines (163 loc) · 4.91 KB
/
pkg
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
#!/bin/bash
set -eu
USER_AGENT='Termux-PKG/1.0 mirror-checker (termux-tools @PACKAGE_VERSION@) Termux (@TERMUX_APP_PACKAGE@; install-prefix:@TERMUX_PREFIX@)'
show_help() {
local cache_size
cache_size=$(du -sh @TERMUX_CACHE_DIR@/apt/archives 2>/dev/null | cut -f1)
echo 'Usage: pkg command [arguments]'
echo
echo 'A tool for managing packages. Commands:'
echo
echo ' autoclean - Remove all outdated packages from .deb package'
echo ' cache.'
echo
echo ' clean - Remove all packages from .deb package cache.'
[ -n "$cache_size" ] && echo " Using $cache_size now."
echo
echo ' files <packages> - Show all files installed by packages.'
echo
echo ' install <packages> - Install specified packages.'
echo
echo ' list-all - List all packages available in repositories.'
echo
echo ' list-installed - List installed packages.'
echo
echo ' reinstall <packages> - Reinstall specified installed packages at the'
echo ' latest version.'
echo
echo ' search <query> - Search package by query, for example by name or'
echo ' description part.'
echo
echo ' show <packages> - Show basic metadata, such as dependencies.'
echo
echo ' uninstall <packages> - Uninstall specified packages. Configuration files'
echo ' will be left intact.'
echo
echo ' upgrade - Upgrade all installed packages to the latest'
echo ' version.'
echo
exit 1
}
check_mirror() {
local mirror="${1%/}"
local timeout="${2-5}"
timeout "$((timeout + 1))" curl \
--head \
--fail \
--connect-timeout "$timeout" \
--location \
--user-agent "$USER_AGENT" \
"$mirror/dists/stable/Release" >/dev/null 2>&1
}
hostname() {
echo "$1" | awk -F'[/:]' '{print $4}'
}
last_modified() {
local mtime
local now
mtime=$(date -r "$1" '+%s')
now=$(date '+%s')
echo $((mtime - now))
}
select_mirror() {
local main_repo="https://termux.org/packages"
declare -A mirrors
mirrors[30]="$main_repo"
mirrors[20]="https://10.via0.com/ipns/k51qzi5uqu5dg9vawh923wejqffxiu9bhqlze5f508msk0h7ylpac27fdgaskx"
mirrors[19]="https://ipfs.io/ipns/k51qzi5uqu5dg9vawh923wejqffxiu9bhqlze5f508msk0h7ylpac27fdgaskx"
mirrors[18]="https://termux.mentality.rip/termux-packages-24"
mirrors[13]="https://grimler.se/termux-packages-24"
local current_mirror
current_mirror=$(grep -P "^\s*deb\s+" @TERMUX_PREFIX@/etc/apt/sources.list | grep -oP 'https?://[^\s]+')
# Do not update mirror if:
# * If $TERMUX_PKG_NO_MIRROR_SELECT was set.
# * Uses .cn domain - specific to Chinese users.
if [ -n "${TERMUX_PKG_NO_MIRROR_SELECT-}" ] || [[ "$(hostname "$current_mirror")" == *".cn" ]]; then
return
fi
# Mirrors are rotated if 6 hours timeout has been passed or mirror is no longer accessible.
local pkgcache="@TERMUX_CACHE_DIR@/apt/pkgcache.bin"
if (( $(last_modified "$pkgcache") <= 6 * 3600 )); then
if [ -n "$current_mirror" ]; then
echo -n "Checking availability of current mirror: "
if check_mirror "$current_mirror"; then
echo "ok"
return
else
echo "bad"
fi
fi
fi
# Test mirror availability, remove unaccessible mirrors from list.
echo "Testing the available mirrors:"
local w total_mirror_weight=0
for w in "${!mirrors[@]}"; do
echo -n "[*] ${mirrors[$w]}: "
if check_mirror "${mirrors[$w]}"; then
echo "ok"
total_mirror_weight=$((total_mirror_weight + w))
else
echo "bad"
unset "mirrors[$w]"
fi
done
unset w
# Weight-based mirror selection.
local selected_mirror=""
if ((total_mirror_weight > 0)); then
local w random_weight calc_weight=0
random_weight=$((RANDOM % total_mirror_weight + 1))
for w in $(echo "${!mirrors[@]}" | tr ' ' '\n' | sort -n); do
calc_weight=$((calc_weight + w))
if ((calc_weight >= random_weight)); then
echo "Picking mirror: ${mirrors[$w]}"
selected_mirror="${mirrors[$w]}"
break
fi
done
fi
if [ -n "$selected_mirror" ]; then
echo "deb $selected_mirror/ stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
else
echo "Using fallback mirror: $main_repo"
echo "deb $main_repo/ stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
fi
}
update_apt_cache() {
local pkgcache="@TERMUX_CACHE_DIR@/apt/pkgcache.bin"
if (( $(last_modified "$pkgcache") > 1200 )); then
apt update
fi
}
if [ $# = 0 ]; then
show_help
fi
CMD="$1"
shift 1
case "$CMD" in
f*) dpkg -L "$@";;
h*) show_help;;
sh*|inf*) apt show "$@";;
add|i*)
select_mirror
update_apt_cache
apt install "$@"
;;
autoc*) apt autoclean;;
cl*) apt clean;;
list-a*) apt list "$@";;
list-i*) apt list --installed "$@";;
rei*) apt install --reinstall "$@";;
se*)
select_mirror
update_apt_cache
apt search "$@"
;;
un*|rem*|rm|del*) apt remove "$@";;
up*)
select_mirror
apt update
apt full-upgrade "$@"
;;
*) echo "Unknown command: '$CMD' (run 'pkg help' for usage information)"; exit 1;;
esac