-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·233 lines (194 loc) · 5.76 KB
/
bootstrap.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
#!/usr/bin/env zsh
# vi: set ft=zsh tw=80 ts=2
# =================
# Utility Functions
# =================
function isDebug() {
[[ ${DEBUG} == true || ${DEBUG} == 1 ]]
}
function printSuccess() {
printOrLog "${colors[green]}${*}${colors[reset]}"
}
function printError() {
printOrLog "${errColors[red]}${*}${errColors[reset]}" >&2
}
function printOrLog() {
if [[ -t 1 ]]; then
print "$@"
else
logger -t zshlib_bootstrap_sh "$@"
fi
}
# =================
# ZSH Lib Downloads
# =================
function getZSHLibArchive() {
local fileUrl=${ZSHLIB_ZIP_URL:-https://github.com/astzweig/zshlib/archive/refs/heads/main.zip}
local zipPath=./zshlib.zip
printOrLog 'Downloading zshlib from astzweig/zshlib on GitHub.'
curl --output ${zipPath} -fsSL "${fileUrl}" || return 10
[[ -f ${zipPath} ]] && unzip ${zipPath} &> /dev/null && rm ${zipPath}
printOrLog 'Done.'
}
function compileZSHLib() {
printOrLog 'Compile zshlib to zsh word code.'
zcompile -z -U ${zshlibTempPath} $(find ./zshlib-main/functions -type f -perm +u=x -maxdepth 1)
printOrLog 'Done.'
}
function buildZSHLibWordCodeFromSource() {
getZSHLibArchive
compileZSHLib
}
function downloadZSHLibWordCodeFromGithub() {
local apiURL=${ZSHLIB_RELEASE_API_URL:-https://api.github.com/repos/astzweig/zshlib/releases/latest}
local zwcURL=$(curl -s $apiURL | python3 -c "import json, sys;zsh_version='${ZSH_VERSION}'; result=[asset['browser_download_url'] for asset in json.load(sys.stdin)['assets'] if asset['name'] == f'zshlib-{zsh_version}.zwc']; print(result[0]) if len(result) > 0 else exit(5)" 2> /dev/null)
[[ $? -eq 0 ]] || return 10
printOrLog 'Downloading latest zshlib word code from astzweig/zshlib on GitHub: '"$zwcURL"
curl --output ${zshlibTempPath} -fsSL "${zwcURL}" || return 10
printOrLog 'Done.'
}
# ==================
# ZSH Lib Installers
# ==================
function moveDownloadToLibDir() {
mv ${zshlibTempPath} ${libDir}
zshlibPath=${libDir}/${zshlibTempPath:t}
chown $user ${zshlibPath}
chmod 'ugo=r' ${zshlibPath}
}
function installZSHLibWordCodeForRoot() {
libDir='/usr/local/share/zsh/site-functions'
local user=root
ensureDirectory /usr/local/share root 775
ensureDirectory /usr/local/share/zsh root 755
ensureDirectory $libDir root 755
moveDownloadToLibDir
}
function checkHomeFolderExists() {
[[ -d $HOME ]] || return 10
}
function installZSHLibWordCodeForUser() {
checkHomeFolderExists || { printError 'Home variable not set. Aborting.'; return 10 }
libDir="$HOME/.zshfn"
local user=$(id -un)
ensureDirectory $libDir $user u=rwx
moveDownloadToLibDir
}
function ensureDirectory() {
local dirPath=$1
[[ ! -d $dirPath ]] && mkdir $dirPath && ensureOwnerAndPermission "$@"
return 0
}
function ensureOwnerAndPermission() {
local dirPath=$1 owner=$2 permission=$3
chown $owner $dirPath
chmod $permission $dirPath
}
# =====================
# ZSH Lib Env Installer
# =====================
function createEnvFileIfNotExists() {
local envFile=$1 owner=$2 permission=$3
[[ -f $envFile ]] && return
touch $envFile
ensureOwnerAndPermission $envFile $owner $permission
}
function extendPathInEnvFile() {
createEnvFileIfNotExists $envFile $owner $permission
cat $envFile 2> /dev/null | grep $libDir >&! /dev/null && return
print -- "fpath+=($zshlibPath)" >> $envFile
}
function modifyFpathForRoot() {
local envFile=/etc/zshenv owner=root permission='u=rw,go=r'
extendPathInEnvFile
}
function modifyFpathForUser() {
local envFile=$HOME/.zshenv owner=$((id -un)):staff permission='u=r'
extendPathInEnvFile
}
# ====
# Main
# ====
function defineColors() {
local -A colorCodes=(red "`tput setaf 9`" green "`tput setaf 10`" reset "`tput sgr0`")
[ -t 1 ] && colors=(${(kv)colorCodes})
[ -t 2 ] && errColors=(${(kv)colorCodes})
}
function configureTerminal() {
defineColors
if [ -t 0 ]; then
traps+=("stty $(stty -g)")
stty -echo
fi
if [ -t 1 ]; then
traps+=('tput cnorm')
tput civis
fi
}
function parseArgs() {
zmodload zsh/zutil
zparseopts -D -- {h,-help}=help_option -from-source=from_source_option
}
function printUsage() {
[[ ! -t 1 ]] && return
cat <<- USAGE
Usage: $_CMD_NAME [options]
Install Astzweig Zsh Lib on current system.
Options:
-h, --help Show this message.
--from-source Compile library from source. Default is to use the latest
release version.
----
$_CMD_NAME 0.1.0
Copyright (C) 2022 Thomas Bernstein, Astzweig GmbH & Co. KG
License EUPL-1.2. There is NO WARRANTY, to the extent permitted by law.
USAGE
}
function showHelpIfRequested() {
[[ -z ${help_option} ]] && return
printUsage "$@"
exit 0
}
function checkForCurl() {
whence curl &> /dev/null || { printError 'This script needs curl. Aborting.'; return 11 }
}
function changeToTemporaryFolder() {
tmpdir="`mktemp -d -t 'zshlib'`"
isDebug || traps+=("rm -fr -- '${tmpdir}'")
pushd -q "${tmpdir}"
print -l "Working directory is: ${tmpdir}"
}
function switchInstallationVariantByUser() {
local username=$(id -un)
[[ $username != root ]] && installVariant=User
}
function installZSHLib() {
local libDir zshlibPath= zshlibTempPath=${tmpdir}/zshlib.zwc
if [[ -z $from_source_option ]]; then
downloadZSHLibWordCodeFromGithub || buildZSHLibWordCodeFromSource
else
buildZSHLibWordCodeFromSource
fi
installZSHLibWordCodeFor$installVariant
modifyFpathFor$installVariant
}
function main() {
local traps=('[ -t 1 ] && tput cnorm') tmpdir=
local help_option=() from_source_option=()
local installVariant=Root
local -A colors=() errColors=()
configureTerminal
parseArgs "$@"
showHelpIfRequested
checkForCurl || return $?
changeToTemporaryFolder
trap ${(j.;.)traps} INT TERM EXIT
switchInstallationVariantByUser
installZSHLib || return $?
printSuccess 'All Done.'
}
if [[ "${ZSH_EVAL_CONTEXT}" == toplevel || "${ZSH_EVAL_CONTEXT}" == cmdarg ]]; then
_DIR="${0:A:h}"
_CMD_NAME=${0:t}
main "$@"
fi