summaryrefslogtreecommitdiff
path: root/.local/bin/config-path
blob: e81a9868592d3ec7b56780afed0416a28cdfa719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash

# All the prefixes to consider
prefixes=(
	"$HOME"
	"$HOME/.local.$(uname -m)"
	"$HOME/.local"
	"$HOME/.prefix.$(uname -m)"
	"$HOME/.prefix"
	"$HOME"/.gem/ruby/*
)

in_array() {
	local needle=$1; shift
	local haystack=("$@")
	local straw
	for straw in "${haystack[@]}"; do
		[[ "$needle" == "$straw" ]] && return 0
	done
	return 1
}

var_init() {
	eval "ary_$1=(\$$1)"
}

var_add() {
	local varname=ary_$1; shift
	local var_all="${varname}[@]"
	local dirs=("$@")

	local dir
	for dir in "${dirs[@]}"; do
		if [[ -d "$dir" ]] && ! in_array "$dir" "${!var_all}"; then
			eval "$varname=(\"\$dir\" \"\${$var_all}\")"
		fi
	done
}

var_done() {
	eval "$1=\"\${ary_$1[*]}\""
	declare -p $1
}

main() {
	export LC_ALL=C # Work around brokenly slow Unicode handling
			# in Bash

	IFS=:
	# Import existing values
	var_init PATH
	var_init MANPATH
	var_init LD_LIBRARY_PATH
	var_init RUBYLIB
	var_init PERL5LIB

	# Scan through prefixes
	for prefix in "${prefixes[@]}"; do
		var_add PATH "$prefix/bin" "$prefix/sbin"
		var_add MANPATH "$prefix/share/man"
		var_add LD_LIBRARY_PATH "$prefix"/lib{,32,64}
		var_add RUBYLIB         "$prefix"/lib{,32,64}
		var_add PERL5LIB        "$prefix"/lib{,32,64}/perl5
	done

	# Finally, print the values
	lines=()
	for var in PATH MANPATH LD_LIBRARY_PATH RUBYLIB PERL5LIB; do
		lines+=("$(var_done "$var")")
	done
	shopt -s extglob
	printf -- '%s\n' "${lines[@]##declare *(-+([[:graph:]]) )}"
}

main "$@"