-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnu
executable file
·191 lines (165 loc) · 5.48 KB
/
gnu
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
#!/usr/bin/env bash
_gnu_fail() {
export _gnu_err="${1:-$_gnu_action failed}"
export _gnu_exit="${2:-1}"
}
export _gnu_err=''
export _gnu_exit=0
export _gnu_action=load
export _gnu_verbose=0
export _gnu_shell=''
while [ $# -gt 0 ]; do
case "$1" in
load|unload|on|off|status|env|rcfile|help)
_gnu_action="$1";;
--verbose|-v) _gnu_verbose=1;;
--help|-h) _gnu_action='help';;
*) _gnu_action='help'
_gnu_fail "Unknown option: $1"
break;;
esac
shift
done
_gnu_log() {
[ "$_gnu_verbose" -eq 0 ] && return
for arg in "$@"; do
echo "$arg" | sed -e 's/^/gnu: /' >&2
done
}
_gnu_eval() { return 1; }
export _gnu_source="$0"
if [ -n "$ZSH_VERSION" ]; then
case "$ZSH_EVAL_CONTEXT" in *:file) _gnu_eval() { return 0; }; ;; esac
_gnu_source="${(%):-%x}"
_gnu_shell=zsh
elif [ -n "$BASH_VERSION" ]; then
if (return 0 2>/dev/null); then _gnu_eval() { return 0; }; fi
_gnu_source="${BASH_SOURCE[0]}"
_gnu_shell=bash
else
echo "Unsupported shell. Please use bash or zsh" >&2
sleep 5 # If we're sourced (no way to tell) give the user time to see mesage
exit 1
fi
_gnu_log "action: $_gnu_action"
_gnu_log "eval: $_gnu_eval"
_gnu_log "source: $_gnu_source"
export _gnu_url="https://githubraw.com/kilna/gnu-on/main/install.sh"
export _gnu_script="$(realpath "$_gnu_source")" # Canonical script location
export _gnu_path="$(echo "$_gnu_script" | sed -e "s;^$HOME/;~/;")" # Pretty
export _gnu_base="$(brew --prefix)/gnu" # Set to /usr/local or /opt by arch
_gnu_log "script: $_gnu_script"
_gnu_log "path: $_gnu_path"
_gnu_log "base: $_gnu_base"
_gnu_help() {
found=0
while IFS='' read line; do
[ "$found" -eq 1 ] && echo "$line"
[ "$line" = '__USAGE__' ] && found=1
done < "$_gnu_script"
unset found
}
_gnu_status() {
if typeset -pf gnu >/dev/null 2>&1; then
echo "$_gnu_script shell extension function is loaded"
else
echo "$_gnu_script shell extension function is not loaded"
fi
if echo ":$PATH:" | grep -Fq ":$_gnu_base/bin:"; then
echo "$_gnu_base/bin is in PATH (gnu is on)"
else
echo "$_gnu_base/bin is not in PATH (gnu is off)"
fi
if echo ":$MANPATH:" | grep -Fq ":$_gnu_base/share/man:"; then
echo "$_gnu_base/share/man is in MANPATH (gnu man pages are on)"
else
echo "$_gnu_base/share/man is not in MANPATH (gnu man pages are off)"
fi
}
_gnu_warn() {
echo '# If you are seeing this then you probably meant to eval this like so:'
echo '# eval "$(gnu '$_gnu_action')"'
}
_gnu_load() {
# Load the gnu function into the shell, which in turn sources this file
_gnu_warn
echo "gnu() { . '$_gnu_script' \"\$@\"; };"
}
_gnu_unload() {
_gnu_warn
echo 'typeset -pf gnu >/dev/null 2>&1 && unset -f gnu'
}
# Used to remove or prepend entries from PATH / MANPATH
_gnu_pathspec() {
local pathvar="$1" # PATH or MANPATH
local action="$2" # + to prepend and cleanup, - to cleanup (remove) only
local exclude="$3" # path to be added / cleaned from the env var
local add=''; if [ "$action" == '+' ]; then add="$3:"; fi
echo -n 'export '$pathvar'="'$add'$('
echo -n 'echo "$'$pathvar'"' # Gets PATH / MANPATH in : format
echo -n "|tr : '\n'" # Turns : into newlines
echo -n "|grep -vxF '$exclude'" # Removes the path from entries
echo -n '|uniq' # Removes duplicate entries
echo -n '|awk NF' # Removes blank entries
echo -n "|tr '\n' :" # Turns newlines back to :
echo -n "|sed -e 's/:\$//'" # Removes trailing :
echo ')"'
}
_gnu_on() {
_gnu_load
_gnu_env
}
_gnu_off() {
_gnu_warn
_gnu_pathspec PATH - $_gnu_base/bin
_gnu_pathspec MANPATH - $_gnu_base/share/man
echo 'if [ "$MANPATH" = "" ]; then unset MANPATH; fi'
}
_gnu_env() {
_gnu_pathspec PATH + $_gnu_base/bin
_gnu_pathspec MANPATH + $_gnu_base/share/man
}
_gnu_rcfile() {
local rcfile="${1:-$HOME/.${_gnu_shell}rc}"
if grep -Eq 'gnu (on|load|env)' "$rcfile"; then
echo "The gnu shell extension is already in $rcfile"
return
fi
echo "Adding gnu shell extension loader to $rcfile"
echo 'eval "$(gnu on)"' >>"$rcfile"
}
case "$_gnu_action" in
help) _gnu_help;;
install) /bin/bash -c "$(curl -fsSL $_gnu_url)";;
rcfile) _gnu_rcfile;;
load) if _gnu_eval; then eval "$(_gnu_load)" || _gnu_fail
else _gnu_load; fi;;
unload) if _gnu_eval; then eval "$(_gnu_unload)" || _gnu_fail
else _gnu_unload; fi;;
on) if _gnu_eval; then eval "$(_gnu_on)" || _gnu_fail
else _gnu_on; fi;;
off) if _gnu_eval; then eval "$(_gnu_off)" || _gnu_fail
else _gnu_off; fi;;
status) _gnu_status;;
env) _gnu_env;;
esac
if _gnu_eval; then
_gnu_log "$(echo "$PATH" | tr : '\n' | sed 's/^/PATH: /')"
_gnu_log "$(typeset -pf gnu 2>/dev/null || echo 'no gnu function')"
_gnu_log "$(_gnu_status)"
fi
[ -n "$_gnu_err" ] && echo "gnu error: $_gnu_err" >&2
_gnu_eval || exit $_gnu_exit
[ "$_gnu_exit" -gt 0 ] && gnu_exit=$_gnu_exit
# Clean up all _gnu functions
funcs=($(typeset -pf | grep -e '^_gnu.* ()' | sed -e 's/ ().*//'))
for func in "${funcs[@]}"; do unset -f $func; done
unset func funcs
# Clean up all _gnu vars
vars=($( typeset -px|cut -f2- -d' '|sed -e 's/-x //; s/=.*//'|grep -e ^_gnu ))
for var in "${vars[@]}"; do unset $var; done
unset var vars
return ${gnu_exit:-0}
# Usage is appended below by install.sh from contents of Usage section
# of README.md
__USAGE__