diff --git a/README.md b/README.md index 6583c53..4f27563 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ will create the symbolic link for each script in the \ directory. - [sync-date](scripts/sync-date) sets system time according to Google. - [xsmartlight](scripts/xsmartlight) allows to adjust ASUS smartscreen's brightness. - [use](scripts/use) shows Gentoo USE flag's description. +- [qz](scripts/qz) looks for Gentoo packages on [http://gpo.zugaina.org](http://gpo.zugaina.org) website. ## Development tools diff --git a/scripts/qz b/scripts/qz new file mode 100755 index 0000000..739973f --- /dev/null +++ b/scripts/qz @@ -0,0 +1,100 @@ +#!/bin/bash + +# +# Поиск пакета Gentoo на сайте zugaina. +# +# Идея: https://megabaks.blogspot.com/2013/04/portage.html +# + +set -e +set -o errtrace +trap stacktrace ERR + +\unalias -a + +IFS=$' \t\n' + +stacktrace() { + [ -n "$TRACED" ] && return + + local depth=${#FUNCNAME[@]} + + for ((i=1; i < "$depth"; i++)); do + printf '%*scall at %s:%s: %s()\n' "$i" '' \ + "${BASH_SOURCE[$i]}" \ + "${BASH_LINENO[$((i-1))]}" \ + "${FUNCNAME[$i]}" + done + + TRACED=true +} + +die() { + echo -e "$@" >&2 + exit 1 +} + +# ------------------------------------------------------------------------------ + +join() { + paste -d " " - - +} + +zxpath() { + curl -sq "$1" | xmllint --html --xpath "$2" - 2>&- +} + +search_packages() { + zxpath "${ZUGAINA_URL}/Search?search=${1}" "//div[@id='search_results']/a/div/descendant::text()" | join +} + +get_package_overlays() { + read -r category package_name rest <<<"$(qatom "$1")" + + local packages + local pages + + pages=$(zxpath "${ZUGAINA_URL}/${category}/${package_name}" "//a[contains(@href, '#')]/@href" \ + | awk -F '/' '{printf "%s/%s\n", $2, $3}' \ + | sort -u) + + for page in $pages;do + packages=$(zxpath "${ZUGAINA_URL}/${page}" "//b[not(./a)]/text()|//b/a/text()" | join | sort --version-sort --reverse) + + while read -r version overlay; do + OVERLAYS+="${category}/${version}:"${overlay//$'\n'/ }$'\n' + done <<<"$packages" + done +} + +# ------------------------------------------------------------------------------ + +[[ "${BASH_VERSINFO[0]}" -lt 5 ]] && die "ERROR: Bash >= 5 required." + +[[ -z "$1" ]] && die "USAGE: $(basename "$0") package" + +DEPENDENCIES=(xmllint awk) +for dependency in "${DEPENDENCIES[@]}"; do + command -V "$dependency" >&- 2>&- || die "ERROR: The required '$dependency' command is not found." +done + +green="\033[01;32m" +rstc="\033[00m" + +ZUGAINA_URL="http://gpo.zugaina.org" + +while read -r package desc;do + echo -e "${green}${package}${rstc}: ${desc}" + get_package_overlays "$package" +done < <(search_packages "$1") + +if [[ -n "${OVERLAYS}" ]]; then + echo -e ":" + echo -e "PACKAGE:REPOSITORY" + echo -e "-------:----------" + echo -e "${OVERLAYS}" + echo -e ":" +else + die "Package '$1' not found." +fi | column -t -s ':' +