-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_environment.sh
executable file
·263 lines (225 loc) · 5.51 KB
/
install_environment.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/bash
set -Eeuo pipefail
shopt -s lastpipe
function callstack() {
local -i SKIP=${1-1}
local -i i
for i in $(seq $SKIP $((${#FUNCNAME[@]} - 1))); do
if [[ ${BASH_SOURCE[$((i + 1))]+found} == "found" ]]; then
echo " $i: ${BASH_SOURCE[$((i + 1))]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}()"
else
echo " $i: ${FUNCNAME[$i]}()"
fi
done
}
function _exit_handle() {
RET=$?
echo -ne "\e[0m"
if [[ $RET -ne 0 ]]; then
callstack 1
fi
exit $RET
}
trap _exit_handle EXIT
echo "starting installer...."
function die() {
if [[ -t 2 ]]; then
{
tput oc
tput rs2
printf '\e[s'
tput rmcup
printf '\e[u'
printf "\r\e[K\e[38;5;9m"
} >&2
fi
printf "%s\n" "$*" >&2
if [[ -t 2 ]]; then
printf "\e[0m" >&2
fi
if [[ $- == *i* ]]; then
return 1
else
exit 1
fi
}
function pad() {
local i
for ((i = 0; i < $1; i++)); do echo -n ' '; done
}
cd $(dirname ${BASH_SOURCE}) ||
die "internal error: can't get script folder"
export MY_SCRIPT_ROOT=$(pwd)
export _INSTALLING_=$(pwd)
export GEN_BIN_PATH="${MY_SCRIPT_ROOT}/.bin"
export PATH+=":${GEN_BIN_PATH}:${MY_SCRIPT_ROOT}/bin"
mkdir -p "${GEN_BIN_PATH}"
echo -e "installing scripts into \e[38;5;14m${MY_SCRIPT_ROOT}\e[0m."
if [[ -e /etc/profile.d/linux-toolbox.sh ]]; then
rm -f /etc/profile.d/linux-toolbox.sh
fi
if [[ -e /etc/profile.d/01-linux-toolbox.sh ]]; then
rm -f /etc/profile.d/01-linux-toolbox.sh
fi
if [[ -e /etc/profile.d/00-environment.sh ]]; then
mv /etc/profile.d/00-environment.sh /etc/profile.d/50-environment.sh
fi
if touch /etc/profile.d/51-linux-toolbox.sh; then
declare -r TARGET=/etc/profile.d/51-linux-toolbox.sh
MY_LIBEXEC=/usr/local/libexec/linux-toolbox
declare -xr SUDO=""
else
declare -r TARGET=$GEN_BIN_PATH/.BASHPROFILE
MY_LIBEXEC="$HOME/.local/lib/linux-toolbox"
ENTRY_FILE="$HOME/.bashrc"
file-section "$ENTRY_FILE" "MY LINUX TOOLBOX" "source '$TARGET'"
declare -xr SUDO="sudo"
fi
function emit() {
echo "$@" >>"${TARGET}"
}
function emitf() {
printf "$@" >>"${TARGET}"
}
function emit_stdin() {
cat >>"${TARGET}"
}
function emit_file() {
cat "${_INSTALLING_}/$1" >>"${TARGET}"
}
function emit_source() {
local CMD=$1
shift
echo -n "source ${VAR_HERE}/$CMD.sh" >>"${TARGET}"
if [[ $# -eq 0 ]]; then
echo -n ' ""' >>"${TARGET}"
else
for i in "$@"; do
echo -n " '$i'" >>"${TARGET}"
done
fi
echo "" >>"${TARGET}"
}
SUDOLIST=()
function emit_alias_sudo() { # command line ...
SUDOLIST+=("alias $1='sudo --preserve-env $@'")
}
function emit_alias_sudo2() { # command line ...
local NAME=$1
shift
SUDOLIST+=("alias $NAME='sudo --preserve-env $@'")
}
function copy_bin_with_env() {
local ENV="$1"
local SRC="$2"
local DST="${3-$(basename "$SRC")}"
local F="${_INSTALLING_}/$SRC"
local T="${GEN_BIN_PATH}/$DST"
rm -f "$T"
{
head -n 1 "$F"
echo
echo "$ENV"
echo
tail -n +2 "$F"
} >"$T"
chmod a+x "$T"
}
function copy_bin() {
local SRC="$1"
local DST="${2-$(basename "$SRC")}"
local F="${_INSTALLING_}/$SRC"
local T="${GEN_BIN_PATH}/$DST"
if [[ -e $T ]] && [[ "$(readlink "$T")" == "$F" ]]; then
return
fi
local DIR=$(dirname "${T}")
F=$(realpath "--relative-to=${DIR}" "--relative-base=${MY_SCRIPT_ROOT}" "${F}")
rm -f "$T"
echo ln -s "${F}" "$T"
ln -s "${F}" "$T"
chmod a+x "$F"
}
function copy_libexec() {
local F="${_INSTALLING_}/$1"
local TN="${2-$(basename "${F}")}"
local T="$MY_LIBEXEC/$TN"
mkdir -p "$(dirname "$T")"
cat "$F" >"$T"
chmod a+x "$T"
echo "$T"
}
function emit_path() {
local PA="${MY_SCRIPT_ROOT}/$1"
if [[ ! -d $PA ]]; then
die "required folder not exists: ${PA}"
fi
chmod a+rx "$PA"
emitf 'path-var prepend "$MY_SCRIPT_ROOT/"%q\n' "$1"
path-var prepend "${PA}"
}
function install_script() {
local FOLDER="${1}"
local PWD=$(pwd)
echo -e "$(pad ${_INSTALL_LEVEL_-0})installing \e[38;5;11m.${PWD/"$MY_SCRIPT_ROOT"/}/${FOLDER}/${2-install}.sh\e[0m ..."
pushd "${FOLDER}" >/dev/null ||
die "can't run install script: $(pwd)/${FOLDER}"
local _INSTALLING_=$(pwd) HERE=$(pwd)
local VAR_HERE="\$MY_SCRIPT_ROOT${HERE/"$MY_SCRIPT_ROOT"/}"
# echo -e "\e[2mHERE=$HERE\e[0m"
# echo -e "\e[2mVAR_HERE=$VAR_HERE\e[0m"
local -i _INSTALL_LEVEL_="${_INSTALL_LEVEL_+$_INSTALL_LEVEL_} + 1"
source "${_INSTALLING_}/${2-install}.sh"
_INSTALL_LEVEL_="${_INSTALL_LEVEL_} - 1"
echo -ne "$(pad ${_INSTALL_LEVEL_})${FOLDER}"
[[ ${2+found} == found ]] && echo -n " -> $2"
echo -e " - \e[38;5;10mOK!\e[0m"
popd >/dev/null
_INSTALLING_=$(pwd) HERE=$(pwd)
VAR_HERE="\$MY_SCRIPT_ROOT${HERE/"$MY_SCRIPT_ROOT"/}"
}
### start
echo "create ${TARGET}"
[ -e "${TARGET}" ] && rm ${TARGET} || true
emit "MY_LIBEXEC=$MY_LIBEXEC"
echo ": common tools..."
install_script common
echo ": system-spec tools..."
install_script system
echo ": quick-alias..."
install_script quick-alias
echo ": interactive..."
emit 'if [[ $- == *i* ]]; then'
install_script interactive
emit 'fi'
echo ": bash source..."
install_script bash_source
echo ": applications..."
for FILE in "${MY_SCRIPT_ROOT}/applications/"*.sh; do
install_script applications $(basename "$FILE" .sh)
done
echo ": user apps..."
install_script user
if [[ ${#SUDOLIST[@]} -gt 0 ]]; then
emit "if ! is_root ; then"
for L in "${SUDOLIST[@]}"; do
emit $'\t'"$L"
done
emit "fi"
fi
echo ": ssh rc file..."
if [[ -e ~/.bashrc ]]; then
sed -i "/LINUX_TOOLBOX_INITED/d" ~/.bashrc
fi
install_script rc
echo -n "complete, try start it - "
# shellcheck source=01-linux-toolbox.sh
source "${TARGET}" ||
{
unlink "${TARGET}"
die "can't start scripts, install failed."
}
if command_exists shfmt; then
shfmt -s -ln=bash -bn -w "$TARGET" "$TARGET"
fi
echo "ok."