diff options
author | Case Duckworth | 2022-04-13 17:04:46 -0500 |
---|---|---|
committer | Case Duckworth | 2022-04-13 17:04:46 -0500 |
commit | 6f30ac188d687522f56b41ebafe7e49c322af038 (patch) | |
tree | 9e2253e0883cad1a8b12ce340a19e3b4abf9b0e9 /profile | |
parent | Check for existence of dircolors (diff) | |
download | etc-6f30ac188d687522f56b41ebafe7e49c322af038.tar.gz etc-6f30ac188d687522f56b41ebafe7e49c322af038.zip |
Add path_add_unsafe and change path munging
Diffstat (limited to 'profile')
-rw-r--r-- | profile/00_functions.sh | 62 | ||||
-rw-r--r-- | profile/01_path.sh | 6 | ||||
-rw-r--r-- | profile/go.sh | 2 | ||||
-rw-r--r-- | profile/infopath.sh | 8 | ||||
-rw-r--r-- | profile/manpath.sh | 16 | ||||
-rw-r--r-- | profile/path.sh | 30 |
6 files changed, 88 insertions, 36 deletions
diff --git a/profile/00_functions.sh b/profile/00_functions.sh new file mode 100644 index 0000000..a94eaee --- /dev/null +++ b/profile/00_functions.sh | |||
@@ -0,0 +1,62 @@ | |||
1 | # utility functions for all shells | ||
2 | # these should be POSIX-compatible. | ||
3 | |||
4 | # add a path to PATH, but only if it's not already there | ||
5 | path_add_to_PATH() { # path_add [-a] PATH... | ||
6 | # -a appends (default is prepend) | ||
7 | APPEND=false | ||
8 | if [ "x$1" = "x-a" ]; then | ||
9 | APPEND=true | ||
10 | shift | ||
11 | fi | ||
12 | |||
13 | for p; do | ||
14 | case ":$PATH:" in | ||
15 | *:"$p":*) ;; | ||
16 | *) | ||
17 | if $APPEND; then | ||
18 | PATH="$PATH:$p" | ||
19 | else | ||
20 | PATH="$p:$PATH" | ||
21 | fi | ||
22 | ;; | ||
23 | esac | ||
24 | done | ||
25 | |||
26 | unset APPEND | ||
27 | } | ||
28 | |||
29 | # Generalization of `path_add_to_PATH' for any variable. | ||
30 | path_add_unsafe() { #path_add_unsafe [-a] [-d DELIM] VAR PATH... | ||
31 | ## Add PATH... to VAR, delimiting with DELIM (default :). | ||
32 | # By default, the VAR will be prepended to; passing -a will append the | ||
33 | # variable. -d DELIM defines the delimiter. | ||
34 | # | ||
35 | # This function has the _unsafe suffix because it uses `eval' to set | ||
36 | # variables. | ||
37 | APPEND=false; DELIM=: | ||
38 | while getopts ad: opt; do | ||
39 | case "$opt" in | ||
40 | a) APPEND=true ;; | ||
41 | d) DELIM="$OPTARG" ;; | ||
42 | *) return 1 ;; | ||
43 | esac | ||
44 | done | ||
45 | shift $(expr $OPTIND - 1) | ||
46 | |||
47 | var="$1"; shift | ||
48 | |||
49 | for path; do | ||
50 | case ":$(eval "echo \$$var"):" in | ||
51 | *:"$path":*) ;; | ||
52 | *) | ||
53 | if $APPEND; then | ||
54 | eval "$var=\$$var${var:+$DELIM}$path" | ||
55 | else | ||
56 | eval "$var=$path${var:+$DELIM}\$$var" | ||
57 | fi | ||
58 | ;; | ||
59 | esac | ||
60 | done | ||
61 | unset -v APPEND DELIM var | ||
62 | } | ||
diff --git a/profile/01_path.sh b/profile/01_path.sh new file mode 100644 index 0000000..8082cc6 --- /dev/null +++ b/profile/01_path.sh | |||
@@ -0,0 +1,6 @@ | |||
1 | # PATH | ||
2 | |||
3 | # see 00_functions.sh for `path_add' | ||
4 | path_add_to_PATH "$HOME/bin" "$HOME/usr/bin" | ||
5 | |||
6 | command -v luarocks >/dev/null 2>&1 && path_add "$HOME/.luarocks/bin" | ||
diff --git a/profile/go.sh b/profile/go.sh new file mode 100644 index 0000000..077833e --- /dev/null +++ b/profile/go.sh | |||
@@ -0,0 +1,2 @@ | |||
1 | export GOPATH="${XDG_DATA_HOME:=$HOME/.local/share}/go" | ||
2 | command -v go >/dev/null 2>&1 && path_add_to_PATH "$(go env GOPATH)/bin" | ||
diff --git a/profile/infopath.sh b/profile/infopath.sh index ad1ec3f..80080b8 100644 --- a/profile/infopath.sh +++ b/profile/infopath.sh | |||
@@ -1 +1,7 @@ | |||
1 | export INFOPATH="/usr/share/info:${XDG_DATA_HOME:-$HOME/.local/share}/info" | 1 | # See 00_functions.sh for `path_add_unsafe'. |
2 | |||
3 | path_add_unsafe INFOPATH \ | ||
4 | /usr/share/info \ | ||
5 | "${XDG_DATA_HOME:-$HOME/.local/share}/info" | ||
6 | |||
7 | export INFOPATH | ||
diff --git a/profile/manpath.sh b/profile/manpath.sh index def1963..944059c 100644 --- a/profile/manpath.sh +++ b/profile/manpath.sh | |||
@@ -1,9 +1,15 @@ | |||
1 | # $MANPATH shouldn't be too extra complicated (as opposed to $PATH), | 1 | # See 00_functions.sh for `path_add_unsafe'. |
2 | # so I'm not going to include a whole other function. AT SOME POINT, | 2 | |
3 | # I suppose I should generalize that function to set /any/ path-type | 3 | path_add_unsafe MANPATH \ |
4 | # variable, not just $PATH. | 4 | "${XDG_DATA_HOME:-$HOME/.local/share}/man" \ |
5 | "$HOME/.local/local/man" \ | ||
6 | "$HOME/usr/local/man" | ||
7 | |||
5 | # $MANPATH ends with `:' so that manpath(1) will prepend it to its | 8 | # $MANPATH ends with `:' so that manpath(1) will prepend it to its |
6 | # special thing. | 9 | # special thing. |
10 | case "$MANPATH" in | ||
11 | *:) ;; | ||
12 | *) MANPATH="$MANPATH:" ;; | ||
13 | esac | ||
7 | 14 | ||
8 | MANPATH="${XDG_DATA_HOME:-$HOME/.local/share}/man:" | ||
9 | export MANPATH | 15 | export MANPATH |
diff --git a/profile/path.sh b/profile/path.sh deleted file mode 100644 index 56bdc39..0000000 --- a/profile/path.sh +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | # PATH | ||
2 | |||
3 | # add a path to PATH, but only if it's not already there | ||
4 | path_add() { # path_add [-a] PATH... | ||
5 | # -a appends (default is prepend) | ||
6 | APPEND=false | ||
7 | if [ "x$1" = "x-a" ]; then | ||
8 | APPEND=true | ||
9 | shift | ||
10 | fi | ||
11 | |||
12 | for p; do | ||
13 | case ":$PATH:" in | ||
14 | *:"$p":*) ;; | ||
15 | *) | ||
16 | if $APPEND; then | ||
17 | PATH="$PATH:$p" | ||
18 | else | ||
19 | PATH="$p:$PATH" | ||
20 | fi | ||
21 | ;; | ||
22 | esac | ||
23 | done | ||
24 | |||
25 | unset APPEND | ||
26 | } | ||
27 | |||
28 | path_add "$HOME/bin" "$HOME/usr/bin" | ||
29 | |||
30 | command -v luarocks >/dev/null 2>&1 && path_add "$HOME/.luarocks/bin" | ||