#!/bin/sh # xlocate [-g | -S | PATTERN] - locate files in all XBPS packages : ${XDG_CACHE_HOME:=~/.cache} : ${XDG_CONFIG_HOME:=~/.config} : ${XLOCATE_CONF:="${XDG_CONFIG_HOME}/xlocate.conf"} if [ -f "${XLOCATE_CONF}" ]; then . "${XLOCATE_CONF}"; fi : ${XLOCATE_GIT:=$XDG_CACHE_HOME/xlocate.git} : ${XLOCATE_REPO:=https://alpha.de.repo.voidlinux.org/xlocate/xlocate.git} if command -v pv >/dev/null; then PROGRESS="pv -l" else PROGRESS=cat fi xupdategit() { set -e DIR=$(mktemp -dt xlocate.XXXXXX) DIR=$(/usr/bin/realpath -e "$DIR") git init -q $DIR cd $DIR xbps-query -M -Ro '*' | $PROGRESS | awk ' $0 ~ ": " { s = index($0, ": ") pkg = substr($0, 1, s-1) file = substr($0, s+2) sub(" *\\([^)]*\\)$", "", file) print file >>pkg }' printf '%s\n' ./* | LC_ALL= xargs -d'\n' -I'{}' -n1 -P "$(nproc)" -r -- \ sort -o {} {} git add ./* git -c user.name=xupdategit -c user.email=xupdategit@none commit -q -m 'xupdategit' git repack -ad rm -rf "$XLOCATE_GIT" .git/COMMIT_EDITMSG .git/description \ .git/index .git/hooks .git/logs [ -n "${XLOCATE_GIT%/*}" ] && mkdir -p "${XLOCATE_GIT%/*}" mv .git "$XLOCATE_GIT" rm -rf "$DIR" } xsyncgit() { if ! [ -d "$XLOCATE_GIT" ]; then [ -n "${XLOCATE_GIT%/*}" ] && mkdir -p "${XLOCATE_GIT%/*}" git clone --bare "$XLOCATE_REPO" "$XLOCATE_GIT" fi git -C "$XLOCATE_GIT" fetch -u -f "$XLOCATE_REPO" master:master } case "$1" in -g) xupdategit exit $?;; -S) xsyncgit exit $?;; '') echo "Usage: xlocate [-g | -S | PATTERN]" 1>&2 exit 1;; esac if [ -d "$XLOCATE_GIT" ]; then if [ -f "$XLOCATE_GIT/refs/heads/master" ]; then BASE="$XLOCATE_GIT/refs/heads/master" elif [ -f "$XLOCATE_GIT/FETCH_HEAD" ]; then BASE="$XLOCATE_GIT/FETCH_HEAD" fi if [ -z "$BASE" ] || find /var/db/xbps/ -name '*repodata' -newer "$BASE" | grep -q .; then if grep -q origin "$XLOCATE_GIT/config"; then echo "xlocate: database outdated, please run xlocate -S." 1>&2 else echo "xlocate: database outdated, please run xlocate -g." 1>&2 fi fi git -c grep.lineNumber=false --git-dir="$XLOCATE_GIT" grep -- "$@" @ | sed 's/^@://; s/:/\t/' | grep . else echo "xlocate: database not found, please use xlocate -S." 1>&2 exit 1 fi