From eafde2d67cc813a6cc7794018ebf6d294c933f02 Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Tue, 4 Sep 2018 10:21:22 -0400
Subject: lddd: update

---
 build_tools/bin/lddd | 198 ++++++++++++++++++---------------------------------
 1 file changed, 70 insertions(+), 128 deletions(-)

diff --git a/build_tools/bin/lddd b/build_tools/bin/lddd
index a16b332..590fe6c 100755
--- a/build_tools/bin/lddd
+++ b/build_tools/bin/lddd
@@ -2,6 +2,18 @@
 #
 # lddd - find broken library links on your machine
 #
+# License: Unspecified
+
+#!/hint/bash
+# This may be included with or without `set -euE`
+
+# License: Unspecified
+
+[[ -z ${_INCLUDE_COMMON_SH:-} ]] || return 0
+_INCLUDE_COMMON_SH=true
+
+# shellcheck disable=1091
+. /usr/share/makepkg/util.sh
 
 # Avoid any encoding problems
 export LANG=C
@@ -9,68 +21,37 @@ export LANG=C
 shopt -s extglob
 
 # check if messages are to be printed using color
-unset ALL_OFF BOLD BLUE GREEN RED YELLOW
 if [[ -t 2 ]]; then
-	# prefer terminal safe colored and bold text when tput is supported
-	if tput setaf 0 &>/dev/null; then
-		ALL_OFF="$(tput sgr0)"
-		BOLD="$(tput bold)"
-		BLUE="${BOLD}$(tput setaf 4)"
-		GREEN="${BOLD}$(tput setaf 2)"
-		RED="${BOLD}$(tput setaf 1)"
-		YELLOW="${BOLD}$(tput setaf 3)"
-	else
-		ALL_OFF="\e[1;0m"
-		BOLD="\e[1;1m"
-		BLUE="${BOLD}\e[1;34m"
-		GREEN="${BOLD}\e[1;32m"
-		RED="${BOLD}\e[1;31m"
-		YELLOW="${BOLD}\e[1;33m"
-	fi
+	colorize
+else
+	# shellcheck disable=2034
+	declare -gr ALL_OFF='' BOLD='' BLUE='' GREEN='' RED='' YELLOW=''
 fi
-readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
-
-plain() {
-	local mesg=$1; shift
-	printf "${BOLD}    ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-msg() {
-	local mesg=$1; shift
-	printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-msg2() {
-	local mesg=$1; shift
-	printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-warning() {
-	local mesg=$1; shift
-	printf "${YELLOW}==> WARNING:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-error() {
-	local mesg=$1; shift
-	printf "${RED}==> ERROR:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
 
 stat_busy() {
 	local mesg=$1; shift
-	printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}...${ALL_OFF}" >&2
+	# shellcheck disable=2059
+	printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}...${ALL_OFF}" "$@" >&2
 }
 
 stat_done() {
+	# shellcheck disable=2059
 	printf "${BOLD}done${ALL_OFF}\n" >&2
 }
 
+_setup_workdir=false
 setup_workdir() {
-	[[ -z $WORKDIR ]] && WORKDIR=$(mktemp -d --tmpdir "${0##*/}.XXXXXXXXXX")
+	[[ -z ${WORKDIR:-} ]] && WORKDIR=$(mktemp -d --tmpdir "${0##*/}.XXXXXXXXXX")
+	_setup_workdir=true
+	trap 'trap_abort' INT QUIT TERM HUP
+	trap 'trap_exit' EXIT
 }
 
 cleanup() {
-	[[ -n $WORKDIR ]] && rm -rf "$WORKDIR"
-	exit ${1:-0}
+	if [[ -n ${WORKDIR:-} ]] && $_setup_workdir; then
+		rm -rf "$WORKDIR"
+	fi
+	exit "${1:-0}"
 }
 
 abort() {
@@ -94,81 +75,54 @@ die() {
 	cleanup 255
 }
 
-trap 'trap_abort' INT QUIT TERM HUP
-trap 'trap_exit' EXIT
-
 ##
-#  usage : in_array( $needle, $haystack )
-# return : 0 - found
-#          1 - not found
+#  usage : lock( $fd, $file, $message, [ $message_arguments... ] )
 ##
-in_array() {
-	local needle=$1; shift
-	local item
-	for item in "$@"; do
-		[[ $item = $needle ]] && return 0 # Found
-	done
-	return 1 # Not Found
-}
+lock() {
+	# Only reopen the FD if it wasn't handed to us
+	if ! [[ "/dev/fd/$1" -ef "$2" ]]; then
+		mkdir -p -- "$(dirname -- "$2")"
+		eval "exec $1>"'"$2"'
+	fi
 
-##
-#  usage : get_full_version( [$pkgname] )
-# return : full version spec, including epoch (if necessary), pkgver, pkgrel
-##
-get_full_version() {
-	# set defaults if they weren't specified in buildfile
-	pkgbase=${pkgbase:-${pkgname[0]}}
-	epoch=${epoch:-0}
-	if [[ -z $1 ]]; then
-		if (( ! epoch )); then
-			echo $pkgver-$pkgrel
-		else
-			echo $epoch:$pkgver-$pkgrel
-		fi
-	else
-		for i in pkgver pkgrel epoch; do
-			local indirect="${i}_override"
-			eval $(declare -f package_$1 | sed -n "s/\(^[[:space:]]*$i=\)/${i}_override=/p")
-			[[ -z ${!indirect} ]] && eval ${indirect}=\"${!i}\"
-		done
-		if (( ! $epoch_override )); then
-			echo $pkgver_override-$pkgrel_override
-		else
-			echo $epoch_override:$pkgver_override-$pkgrel_override
-		fi
+	if ! flock -n "$1"; then
+		stat_busy "${@:3}"
+		flock "$1"
+		stat_done
 	fi
 }
 
 ##
-#  usage : lock( $fd, $file, $message )
+#  usage : slock( $fd, $file, $message, [ $message_arguments... ] )
 ##
-lock() {
-	eval "exec $1>"'"$2"'
-	if ! flock -n $1; then
-		stat_busy "$3"
-		flock $1
+slock() {
+	# Only reopen the FD if it wasn't handed to us
+	if ! [[ "/dev/fd/$1" -ef "$2" ]]; then
+		mkdir -p -- "$(dirname -- "$2")"
+		eval "exec $1>"'"$2"'
+	fi
+
+	if ! flock -sn "$1"; then
+		stat_busy "${@:3}"
+		flock -s "$1"
 		stat_done
 	fi
 }
 
 ##
-#  usage : slock( $fd, $file, $message )
+#  usage : lock_close( $fd )
 ##
-slock() {
-	eval "exec $1>"'"$2"'
-	if ! flock -sn $1; then
-		stat_busy "$3"
-		flock -s $1
-		stat_done
-	fi
+lock_close() {
+	local fd=$1
+	# https://github.com/koalaman/shellcheck/issues/862
+	# shellcheck disable=2034
+	exec {fd}>&-
 }
 
 ##
 # usage: pkgver_equal( $pkgver1, $pkgver2 )
 ##
 pkgver_equal() {
-	local left right
-
 	if [[ $1 = *-* && $2 = *-* ]]; then
 		# if both versions have a pkgrel, then they must be an exact match
 		[[ $1 = "$2" ]]
@@ -187,7 +141,7 @@ pkgver_equal() {
 find_cached_package() {
 	local searchdirs=("$PWD" "$PKGDEST") results=()
 	local targetname=$1 targetver=$2 targetarch=$3
-	local dir pkg pkgbasename pkgparts name ver rel arch size r results
+	local dir pkg pkgbasename name ver rel arch r results
 
 	for dir in "${searchdirs[@]}"; do
 		[[ -d $dir ]] || continue
@@ -225,7 +179,7 @@ find_cached_package() {
 			return 1
 			;;
 		1)
-			printf '%s\n' "$results"
+			printf '%s\n' "${results[0]}"
 			return 0
 			;;
 		*)
@@ -235,18 +189,6 @@ find_cached_package() {
 	esac
 }
 
-##
-#  usage : check_root ("$0" "$@")
-##
-check_root() {
-	(( EUID == 0 )) && return
-	if type -P sudo >/dev/null; then
-		exec sudo -- "$@"
-	else
-		exec su root -c "$(printf ' %q' "$@")"
-	fi
-}
-
 
 ifs=$IFS
 IFS="${IFS}:"
@@ -259,10 +201,10 @@ TEMPDIR=$(mktemp -d --tmpdir lddd-script.XXXX)
 msg 'Go out and drink some tea, this will take a while :) ...'
 #  Check ELF binaries in the PATH and specified dir trees.
 for tree in $PATH $libdirs $extras; do
-	msg2 "DIR $tree"
+	msg2 "DIR %s" "$tree"
 
 	#  Get list of files in tree.
-	files=$(find $tree -type f ! -name '*.a' ! -name '*.la' ! -name '*.py*' ! -name '*.txt' ! -name '*.h' ! -name '*.ttf' ! \
+	files=$(find "$tree" -type f ! -name '*.a' ! -name '*.la' ! -name '*.py*' ! -name '*.txt' ! -name '*.h' ! -name '*.ttf' ! \
 	-name '*.rb' ! -name '*.ko' ! -name '*.pc' ! -name '*.enc' ! -name '*.cf' ! -name '*.def' ! -name '*.rules' ! -name \
 	'*.cmi' ! -name  '*.mli' ! -name '*.ml' ! -name '*.cma' ! -name '*.cmx' ! -name '*.cmxa' ! -name '*.pod' ! -name '*.pm' \
 	! -name '*.pl' ! -name '*.al' ! -name '*.tcl' ! -name '*.bs' ! -name '*.o' ! -name '*.png' ! -name '*.gif' ! -name '*.cmo' \
@@ -270,22 +212,22 @@ for tree in $PATH $libdirs $extras; do
 	-name '*.mcopclass' ! -name '*.mcoptype')
 	IFS=$ifs
 	for i in $files; do
-		if (( $(file $i | grep -c 'ELF') != 0 )); then
+		if (( $(file "$i" | grep -c 'ELF') != 0 )); then
 			#  Is an ELF binary.
-			if (( $(ldd $i 2>/dev/null | grep -c 'not found') != 0 )); then
+			if (( $(ldd "$i" 2>/dev/null | grep -c 'not found') != 0 )); then
 				#  Missing lib.
-				echo "$i:" >> $TEMPDIR/raw.txt
-				ldd $i 2>/dev/null | grep 'not found' >> $TEMPDIR/raw.txt
+				echo "$i:" >> "$TEMPDIR/raw.txt"
+				ldd "$i" 2>/dev/null | grep 'not found' >> "$TEMPDIR/raw.txt"
 			fi
 		fi
 	done
 done
-grep '^/' $TEMPDIR/raw.txt | sed -e 's/://g' >> $TEMPDIR/affected-files.txt
+grep '^/' "$TEMPDIR/raw.txt" | sed -e 's/://g' >> "$TEMPDIR/affected-files.txt"
 # invoke pacman
-for i in $(cat $TEMPDIR/affected-files.txt); do
-	pacman -Qo $i | awk '{print $4,$5}' >> $TEMPDIR/pacman.txt
-done
+while read -r i; do
+	pacman -Qo "$i" | awk '{print $4,$5}' >> "$TEMPDIR/pacman.txt"
+done < "$TEMPDIR/affected-files.txt"
 # clean list
-sort -u $TEMPDIR/pacman.txt >> $TEMPDIR/possible-rebuilds.txt
+sort -u "$TEMPDIR/pacman.txt" >> "$TEMPDIR/possible-rebuilds.txt"
 
-msg "Files saved to $TEMPDIR"
+msg "Files saved to %s" "$TEMPDIR"
-- 
cgit v0.12