104 lines
3.2 KiB
Bash
Executable File
104 lines
3.2 KiB
Bash
Executable File
#!/bin/bash
|
|
set -e -u
|
|
|
|
show_help() {
|
|
echo 'Usage: pkg command [arguments]'
|
|
echo ''
|
|
echo 'A tool for managing packages. Commands:'
|
|
echo ''
|
|
echo ' autoclean - remove all outdated packages from .deb package cache'
|
|
echo " clean - remove all packages from .deb package cache: $(du -sh /data/data/com.termux/cache/apt/archives | cut -f1) now used"
|
|
echo ' files <packages> - show all files installed by packages'
|
|
echo ' install <packages>'
|
|
echo ' list-all'
|
|
echo ' list-installed'
|
|
echo ' reinstall <packages> - reinstall specified installed packages at the latest version'
|
|
echo ' search <query>'
|
|
echo ' show <packages> - show basic metadata, such as dependencies'
|
|
echo ' uninstall <packages>'
|
|
echo ' upgrade - update all installed packages to latest version'
|
|
exit 1
|
|
}
|
|
|
|
select_mirror() {
|
|
local main_repo="https://termux.org/packages"
|
|
local top_mirrors="https://dl.bintray.com/termux/termux-packages-24 https://termux.mentality.rip/termux-packages-24 https://grimler.se/termux-packages-24 https://main.termux-mirror.ml"
|
|
|
|
local current_mirror
|
|
current_mirror=$(grep -P "^\s*deb\s+" @TERMUX_PREFIX@/etc/apt/sources.list | grep -oP 'https?://[a-z0-9/._-]+')
|
|
|
|
# Do not update mirror if:
|
|
# * Uses .cn domain - specific to Chinese users.
|
|
# * If $TERMUX_PKG_NO_MIRROR_SELECT was set.
|
|
if [ -n "${TERMUX_PKG_NO_MIRROR_SELECT-}" ] || grep -qP '.+\.cn/' <(echo "${current_mirror-x}"); then
|
|
return
|
|
fi
|
|
|
|
if [ -n "$(find /data/data/com.termux/cache/apt/pkgcache.bin -mmin -180 2>/dev/null)" ]; then
|
|
# Mirrors are rotated if either they are not working or 180 minutes timeout
|
|
# has passed.
|
|
if [ -n "${current_mirror}" ] && curl --user-agent 'Termux-PKG/1.0 mirror-checker' --head --fail --location "${current_mirror}/dists/stable/Release" >/dev/null 2>&1; then
|
|
echo "Reusing mirror: ${current_mirror}"
|
|
return
|
|
fi
|
|
fi
|
|
|
|
# Shuffle mirrors and pick the first one which is accessible.
|
|
local m selected_mirror=""
|
|
for m in $(echo "$top_mirrors" | tr ' ' '\n' | shuf); do
|
|
echo "Trying mirror: $m"
|
|
if curl --user-agent 'Termux-PKG/1.0 mirror-checker' --head --fail --location "${m}/dists/stable/Release" >/dev/null 2>&1; then
|
|
selected_mirror="$m"
|
|
break
|
|
fi
|
|
done
|
|
|
|
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() {
|
|
if [ -z "$(find /data/data/com.termux/cache/apt/pkgcache.bin -mmin -5 2>/dev/null)" ]; then
|
|
apt update
|
|
fi
|
|
}
|
|
|
|
if [ $# = 0 ]; then
|
|
show_help
|
|
fi
|
|
|
|
CMD="$1"
|
|
shift 1
|
|
|
|
case "$CMD" in
|
|
f*) dpkg -L "$@";;
|
|
h*) show_help;;
|
|
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 "$@"
|
|
;;
|
|
sh*) apt show "$@";;
|
|
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
|