2020-07-31 13:18:43 +02:00
#!/bin/bash
2017-02-17 23:13:49 +01:00
set -e -u
show_help() {
2020-08-01 01:47:03 +02:00
local cache_size
cache_size=$(du -sh /data/data/com.termux/cache/apt/archives 2>/dev/null | cut -f1)
2017-10-01 21:42:37 +02:00
echo 'Usage: pkg command [arguments]'
2020-08-01 01:47:03 +02:00
echo
2017-02-17 23:13:49 +01:00
echo 'A tool for managing packages. Commands:'
2020-08-01 01:47:03 +02:00
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
2017-02-17 23:13:49 +01:00
exit 1
}
2020-07-31 13:18:43 +02:00
select_mirror() {
2020-07-31 13:29:31 +02:00
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"
2020-07-31 13:18:43 +02:00
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
2020-07-31 18:31:42 +02:00
if [ -n "$(find /data/data/com.termux/cache/apt/pkgcache.bin -mmin -180 2>/dev/null)" ]; then
2020-07-31 13:18:43 +02:00
# 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() {
2020-07-31 18:31:42 +02:00
if [ -z "$(find /data/data/com.termux/cache/apt/pkgcache.bin -mmin -5 2>/dev/null)" ]; then
2019-11-10 15:29:05 +01:00
apt update
fi
}
if [ $# = 0 ]; then
show_help
fi
2017-02-17 23:13:49 +01:00
CMD="$1"
shift 1
case "$CMD" in
2019-05-06 22:22:47 +02:00
f*) dpkg -L "$@";;
2017-03-10 15:22:44 +01:00
h*) show_help;;
2020-07-31 13:18:43 +02:00
add|i*)
select_mirror
update_apt_cache
apt install "$@"
;;
2020-08-01 14:21:10 +02:00
autoc*) apt autoclean;;
2020-07-31 18:31:42 +02:00
cl*) apt clean;;
2019-05-06 22:22:47 +02:00
list-a*) apt list "$@";;
list-i*) apt list --installed "$@";;
2019-11-13 15:38:40 +01:00
rei*) apt install --reinstall "$@";;
2020-07-31 13:18:43 +02:00
se*)
select_mirror
update_apt_cache
apt search "$@"
;;
2019-05-06 22:22:47 +02:00
sh*) apt show "$@";;
2019-11-13 15:38:40 +01:00
un*|rem*|rm|del*) apt remove "$@";;
2020-07-31 13:18:43 +02:00
up*)
select_mirror
apt update
apt full-upgrade "$@"
;;
2019-11-10 15:29:05 +01:00
*) echo "Unknown command: '$CMD' (run 'pkg help' for usage information)"; exit 1;;
2017-02-17 23:13:49 +01:00
esac