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

the_guts() {
	# Add generic prefixes
	add_prefix "$HOME"
	add_prefix "$HOME/.local.$(uname -m)"
	add_prefix "$HOME/.local"
	add_prefix "$HOME/.prefix.$(uname -m)"
	add_prefix "$HOME/.prefix"

	# Add rubygem prefixes
	local dir
	for dir in "$HOME"/.gem/*; do
		# Only add it if we have that type of ruby
		if type "${dir##*/}" &>/dev/null; then
			add_prefix "$dir"/*
		fi
	done
}

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

add_prefix() {
	local prefix=$1
	local dir

	# 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
}

main() {
	# Import existing values
	declare -ga paths rubylibs
	IFS=: paths=($PATH)
	IFS=: rubylibs=($RUBYLIB)

	the_guts

	# Put our values into the env variables
	IFS=: PATH="${paths[*]}"
	IFS=: RUBYLIB="${rubylibs[*]}"

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

main