-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_lib.sh
128 lines (102 loc) · 2.71 KB
/
path_lib.sh
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
# (C) 2017 by Argonne National Laboratory.
# See COPYRIGHT in top-level directory.
readonly BASE_PATH=${LR:-$HOME/.local}
export BASE_PATH
readonly ENV_BACKUP_FILE="$HOME/.path_lib.env.bak"
# Utilities
function init() {
:
}
function backup_env() {
if [ -f $ENV_BACKUP_FILE ]; then
rm $ENV_BACKUP_FILE
fi
touch $ENV_BACKUP_FILE
echo "export PATH=$PATH" >> $ENV_BACKUP_FILE
echo "export CPATH=$CPATH" >> $ENV_BACKUP_FILE
echo "export INCLUDE=$INCLUDE" >> $ENV_BACKUP_FILE
echo "export C_INCLUDE_PATH=$C_INCLUDE_PATH" >> $ENV_BACKUP_FILE
echo "export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH" >> $ENV_BACKUP_FILE
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" >> $ENV_BACKUP_FILE
echo "export LIBRARY_PATH=$LIBRARY_PATH" >> $ENV_BACKUP_FILE
echo "export LBAK_PKG_CONFIG_PATH=$PKG_CONFIG_PATH" >> $ENV_BACKUP_FILE
}
function restore_env() {
source $ENV_BACKUP_FILE
}
function path_exists() {
if [ -d "$1" ] || [ -h "$1" ]; then
return 0
else
return 1
fi
}
function clean_path() {
local _tmp_p=${(P)1}
export $1="${_tmp_p%:}"
}
function check_export_path() {
local _tmp_p=${(P)1}
if path_exists "$2"; then
if [[ ! ":$_tmp_p:" = *":$2:"* ]]; then
export $1="$2:$_tmp_p"
clean_path $1
fi
else
return 0
fi
}
function abs_or_local_path() {
if path_exists "$1" ; then
NEW_PATH="$1"
return 0
fi
if path_exists "$BASE_PATH/$1" ; then
NEW_PATH="$BASE_PATH/$1"
return 0
fi
echo "path_lib: $1 does not exist"
return 1
}
# Basic path functions
function lib_path() {
abs_or_local_path "$1"
check_export_path CPATH "$NEW_PATH/include"
export INCLUDE=$CPATH
export C_INCLUDE_PATH=$CPATH
export CPLUS_INCLUDE_PATH=$CPATH
check_export_path LIBRARY_PATH "$NEW_PATH/lib"
check_export_path LIBRARY_PATH "$NEW_PATH/lib64"
export LD_LIBRARY_PATH=$LIBRARY_PATH
check_export_path PKG_CONFIG_PATH "$NEW_PATH/lib/pkgconfig"
check_export_path PKG_CONFIG_PATH "$NEW_PATH/lib64/pkgconfig"
}
function bin_path() {
abs_or_local_path "$1"
check_export_path PATH "$NEW_PATH/bin"
}
# Convenient functions
function get_prefix() {
echo "$BASE_PATH/$1"
}
function application_path() {
bin_path "$1"
}
function library_path() {
lib_path "library/$1"
}
function compiler_path() {
bin_path "$1"
lib_path "$1"
}
function default() {
if [ -n "$LOCAL_DEF_PATH" ]; then return; fi
check_export_path PATH "$LR/usr/bin"
check_export_path PATH "$LR/bin"
bin_path "bin"
bin_path "usr/bin"
lib_path "usr"
lib_path "$BASE_PATH"
export LOCAL_DEF_PATH=1
}
# vim: syn=zsh