-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_environment.sh
More file actions
executable file
·175 lines (142 loc) · 4.15 KB
/
Copy pathinstall_environment.sh
File metadata and controls
executable file
·175 lines (142 loc) · 4.15 KB
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
#!/usr/bin/env bash
set -Eeuo pipefail
shopt -s inherit_errexit extglob nullglob globstar lastpipe shift_verbose
MY_SCRIPT_ROOT="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
cd "${MY_SCRIPT_ROOT}" || die "internal error: can't get script folder"
declare -xr MY_SCRIPT_ROOT
declare -x HERE="$MY_SCRIPT_ROOT"
source "./common/shared-library/include.sh"
function pad() {
die "pad is removed, use indent/indent- instead"
}
if [[ " $* " != *' --user '* ]] && is_root; then
MY_LIBEXEC=/usr/local/libexec/linux-toolbox
declare -r INSTALL_TARGET_FILE="${MY_LIBEXEC}/.BASHPROFILE"
ENTRY_FILE=/etc/profile.d/51-linux-toolbox.sh
unlink "${ENTRY_FILE}"
declare -xr INSTALL_TYPE='system'
error " 🐧🐧🐧🐧 linux-toolbox @ ${INSTALL_TYPE}"
else
MY_LIBEXEC="$HOME/.local/lib/linux-toolbox"
declare -r INSTALL_TARGET_FILE="${MY_LIBEXEC}/.BASHPROFILE"
ENTRY_FILE="$HOME/.bashrc"
declare -xr INSTALL_TYPE='user'
success " 🐧🐧🐧🐧 linux-toolbox @ ${INSTALL_TYPE}"
fi
if [[ " $* " == *' --isolate '* ]]; then
ENTRY_CODE=$'if [[ -n "${USERNAME-}" ]] ; then\n'
ENTRY_CODE+=" source '$INSTALL_TARGET_FILE'"
ENTRY_CODE+=$'\nfi'
else
ENTRY_CODE="source '$INSTALL_TARGET_FILE'"
fi
my_call file-section "$ENTRY_FILE" "MY LINUX TOOLBOX" "$ENTRY_CODE"
export GEN_BIN_PATH="${MY_LIBEXEC}/bin"
export GEN_HELPERS_PATH="${MY_LIBEXEC}/helpers"
writeln " * installing scripts into \e[38;5;14m${MY_LIBEXEC}\e[0m"
writeln " * entrypoint is \e[38;5;14m${INSTALL_TARGET_FILE}\e[0m"
filecopy_prepare
writeln
__INDENT=''
function install_script() {
local FOLDER="${1}" PWD HERE
PWD=$(pwd)
writeln "installing \e[38;5;11m.${PWD/"$MY_SCRIPT_ROOT"/}/${FOLDER}/${2-install}.sh\e[0m"
indent
pushd "${FOLDER}" >/dev/null ||
die "can't run install script: $(pwd)/${FOLDER}"
HERE=$(pwd)
# echo -e "\e[2mHERE=$HERE\e[0m"
# echo -e "\e[2mVAR_HERE=$VAR_HERE\e[0m"
local SRC="${HERE}/${2-install}.sh"
emit "### === script: ${SRC#"${MY_SCRIPT_ROOT}"/} ==="
# shellcheck source=/dev/null
source "${SRC}"
ok "${FOLDER}" "$([[ ${2+found} == found ]] && echo "-> $2")"
popd >/dev/null
indent-
HERE=$(pwd)
}
### start
if [[ -e ${INSTALL_TARGET_FILE} ]]; then
unlink "${INSTALL_TARGET_FILE}"
fi
header "preparing"
if [[ ${USER_DISPLAYNAME+found} == found ]]; then
: # is correct
elif command_exists git && git config --get user.name &>/dev/null; then
USER_DISPLAYNAME=$(git config --get user.name || true)
fi
if [[ -z ${USER_DISPLAYNAME-} ]]; then
read -r -p "输入你的显示名称 (Display Name): " USER_DISPLAYNAME
fi
export USER_DISPLAYNAME
if is_root; then
if [[ -e "${MY_LIBEXEC}/user-mode" ]]; then
SINGLEUSER_MODE=$(<"${MY_LIBEXEC}/user-mode")
else
while true; do
read -r -p "当前为root用户,是否以单用户模式安装?(y/n): " RESP
case $RESP in
[Yy]*)
SINGLEUSER_MODE=true
break
;;
[Nn]*)
SINGLEUSER_MODE=false
break
;;
esac
done
printf "$SINGLEUSER_MODE" >"${MY_LIBEXEC}/user-mode"
fi
if [[ $SINGLEUSER_MODE != "false" ]]; then
echo "will installed as single user mode"
fi
else
SINGLEUSER_MODE=false
fi
export SINGLEUSER_MODE
is_single_user_mode() {
[[ $SINGLEUSER_MODE != "false" ]]
}
header "common tools"
install_script common
header "system-spec tools"
install_script system
header "quick-alias"
install_script quick-alias
header "interactive"
emit 'if [[ $- == *i* ]]; then'
__INDENT=$'\t' install_script interactive
emit 'fi'
header "bash source"
install_script bash_source
header "applications"
for FILE in "${MY_SCRIPT_ROOT}/applications/"*.sh; do
install_script applications "$(basename "$FILE" .sh)"
done
header "user apps"
install_script user
emit_sudo_part
header "ssh rc file"
if [[ -e ~/.bashrc ]]; then
sed -i "/LINUX_TOOLBOX_INITED/d" ~/.bashrc
fi
install_script rc
debug "removing comments"
sed -Ei "/^\s*#.*$/d" "$INSTALL_TARGET_FILE"
if command_exists shfmt; then
debug "reformat it with shfmt"
shfmt -s -ln=bash -bn -w "$INSTALL_TARGET_FILE"
else
warning "shfmt not found, best to install it"
fi
debug "try source it"
# shellcheck source=/dev/null
source "${INSTALL_TARGET_FILE}" ||
{
unlink "${INSTALL_TARGET_FILE}"
die "can't start scripts, install failed."
}
printf "\n\e[38;5;2;7;1m 好 耶 \e[0m\n\n"