-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdotbash_path
205 lines (182 loc) · 4.77 KB
/
dotbash_path
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
#
# PATH manipulations
#
# This was getting hairy enough it was worth splitting out into its own fragement.
#
# I like to quickly setup my local LD_LIBRARY_PATH and PATH for
# non-root installs and testing. This will break if you attempt to do
# things like suid in code your testing as LD_LIBRARY_PATH isn't
# usually exported to suid binaries for obvious security reasons.
#
# Save the PATH/LD_LIBRARY_PATH
: ${ORIG_PATH:=${PATH}}
: ${ORIG_LD_PATH:=${LD_LIBRARY_PATH}}
export ORIG_PATH
export ORIG_LD_PATH
function clear_world
{
export PATH=$ORIG_PATH
export LD_LIBRARY_PATH=$ORIG_LD_PATH
echo "Reset to"
echo "PATH=$PATH"
echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
}
alias cw="clear_world"
# Add a ../install world into our path, overwriting any PATH/LD_LIBRARY_PATH fiddles
function switch_world
{
# Save the old state
OLD_WORLD_ROOT=$CURRENT_WORLD_ROOT
OLD_WORLD_BIN=$CURRENT_WORLD_BIN
OLD_WORLD_LIB=$CURRENT_WORLD_LIB
OLD_WORLD_SRC=$CURRENT_WORLD_SRC
if [ "$1" ]
then
# Reset
if [ "$1" = "-" ]
then
PATH=$ORIG_PATH
LD_LIBRARY_PATH=$ORIG_LD_PATH
echo "PATH=$PATH"
echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
return
elif [ "$1" = "." ]
then
CURRENT_WORLD_ROOT=`pwd`
else
CURRENT_WORLD_ROOT=$1
fi
else
CURRENT_WORLD_ROOT=`pwd`
fi
# Check the paths are valid
CURRENT_WORLD_BIN=${CURRENT_WORLD_ROOT}/install/bin
if [ -d "$CURRENT_WORLD_BIN" ]
then
echo "Setting CURRENT_WORLD_BIN=$CURRENT_WORLD_BIN"
export CURRENT_WORLD_BIN
export PATH=$CURRENT_WORLD_BIN:$ORIG_PATH
else
echo "No directory at $CURRENT_WORLD_BIN, giving up"
CURRENT_WORLD_ROOT=$OLD_WORLD_ROOT
CURRENT_WORLD_BIN=$OLD_WORLD_BIN
return
fi
# The lib path needs to point at the libs we use, we overide LD_LIBRARY_PATH
# so the correct libs are loaded. This *will not work* with suid binaries as it opens
# a massive security hole
CURRENT_WORLD_LIB=${CURRENT_WORLD_ROOT}/install/lib
if [ -d "$CURRENT_WORLD_LIB" ]
then
echo "Setting CURRENT_WORLD_LIB=$CURRENT_WORLD_LIB"
export CURRENT_WORLD_LIB
export LD_LIBRARY_PATH=$CURRENT_WORLD_LIB:$ORIG_LD_PATH
else
echo "No directory at $CURRENT_WORLD_LIB, not tweaking LD_LIBRARY_PATH"
CURRENT_WORLD_LIB=$OLD_WORLD_LIB
fi
}
#
# Bash version comparison routines
# from: http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
#
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
#
# Unlike switch_world this assumes you want to permanently add something
# to your search paths. It's also a lot quieter about it.
#
function do_add_path
{
UPDATED="none"
if [ -d ${1}/bin ]
then
UPDATED="Added ${1}/bin to PATH"
export PATH=${1}/bin:$PATH
# This *will not work* with suid binaries as it would
# open a massive security hole.
if [ -d ${1}/lib ]
then
UPDATED="${UPDATED} and ${1}/lib to LD_LIBRARY_PATH"
export LD_LIBRARY_PATH=${1}/lib:$LD_LIBRARY_PATH
fi
else
UPDATED="Just adding $1 to PATH"
export PATH=$1:$PATH
fi
echo "$UPDATED"
}
function add_to_world
{
if [ "$1" ]
then
if [ "$1" = "." ]
then
CURRENT_WORLD_ROOT=`pwd`
else
CURRENT_WORLD_ROOT=`realpath $1`
fi
else
CURRENT_WORLD_ROOT=`pwd`
fi
if [ -d ${CURRENT_WORLD_ROOT}/install ]
then
do_add_path ${CURRENT_WORLD_ROOT}/install
RET=0
else
do_add_path ${CURRENT_WORLD_ROOT}
RET=0
fi
return $RET
}
# Clean PATH (http://unix.stackexchange.com/questions/14895/duplicate-entries-in-path-a-problem)
function clean_path {
PATH=$(echo "$PATH" | awk -v RS=':' -v ORS=":" '!a[$1]++{if (NR > 1) printf ORS; printf $a[$1]}')
}
# Check for active paths
PATH_STATUS=""
[[ $PATH == *"lava"* ]] && PATH_STATUS="lava"
[[ $PATH == *"/usr/local"* ]] && PATH_STATUS="${PATH_STATUS} local"
# I have stuff in ~/bin
if [ -d $HOME/bin ]; then
export PATH=${HOME}/bin:$PATH
PATH_STATUS="${PATH_STATUS} ~/bin"
fi
clean_path
# Alises
alias sw="switch_world"
alias aw="add_to_world"
# Alias for reloading
alias .path=". $DOTFILES_DIR/dotbash_path"
if [ ! -z "$PATH_STATUS" ]; then
echo "loading .bash_path (${PATH_STATUS##[[:space:]]})"
fi