summaryrefslogtreecommitdiff
path: root/.local/bin/config-path
blob: 7cd1fcd7a9bffc21e964591d894618b9ab8af742 (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
#!/bin/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() {
	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
	# The `sed` bit here is the only time we call an external program
	{
		var_done PATH
		var_done MANPATH
		var_done LD_LIBRARY_PATH
		var_done RUBYLIB
		var_done PERL5LIB
	} | sed 's/^declare \(-\S* \)*//'
}

main "$@"