summaryrefslogtreecommitdiff
path: root/.local/bin/config-path
blob: 6b9019cd310e23ba22f6206387e2a6cb3d2a001b (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
#!/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
}

# Import existing values
IFS=: paths=($PATH)
IFS=: rubylibs=($RUBYLIB)

# Scan through prefixes
for prefix in "${prefixes[@]}"; do
	# PATH
	dir="$prefix/bin"
	if [[ -d "$dir" ]] && ! in_array "$dir" "${paths[@]}"; then
		paths=("$dir" "${paths[@]}")
	fi
	# RUBYLIB
	dir="$prefix/lib"
	if [[ -d "$dir" ]] && ! in_array "$dir" "${rubylibs[@]}"; then
		rubylibs=("$dir" "${rubylibs[@]}")
	fi
done

# Finally, print our values
IFS=: PATH="${paths[*]}"
IFS=: RUBYLIB="${rubylibs[*]}"

# The sed bit here is the only time we call an external program
{
	declare -p PATH
	declare -p RUBYLIB
} | sed 's/^declare \(-\S* \)*//'