forked from MiXXiM/miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoexec-installer
265 lines (228 loc) · 6.36 KB
/
autoexec-installer
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
264
265
#!/bin/sh
#cito M:755 O:0 G:0 T:/usr/bin/autoexec-installer
#------------------------------------------------------------------------------
# Project Name - Extra/source/autoexec/autoexec-installer
# Started On - Thu 12 Mar 14:54:26 GMT 2020
# Last Change - Sun 9 Jan 18:05:10 GMT 2022
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
# Simple, but pretty and mindful Bourne Shell installer for AutoExec.
#
# To use this installer, you can either:
#
# Clone the repository, using git(1) command.
#
# OR
#
# Download the archive of it directly from GitHub, then extract its contents.
#
# Since this installer just fetches from GitHub, you needn't actually download
# the entire Extra repository to install it; only the installer.
#
# If you want a HOME-only installation, you'll have to either use Cito, or
# otherwise install it manually, but you may miss out on a lot of features.
#
# NOTE: Due to changes in BASH 4.4, running this therein will spit out errors.
#
# Dependencies:
#
# coreutils (>= 8.25-2)
# curl (>= 7.47.0-1) | wget (>= 1.17.1-1)
#------------------------------------------------------------------------------
Err(){
printf 'Err: %s\n' "$2" 1>&2
[ $1 -gt 0 ] && exit $1
}
if command -v curl 1> /dev/null 2>&1; then
DLCMD='curl -so'
elif command -v wget 1> /dev/null 2>&1; then
DLCMD='wget -qO'
else
Err 1 "Neither 'wget' nor 'curl' are available."
fi
[ `id -u` -ne 0 ] && Err 1 'Root access is required for system-wide changes.'
while :; do
printf 'Install latest version of AutoExec? [Y/N] '
read Input
case $Input in
[Yy]|[Yy][Ee][Ss])
printf 'Continuing with installation.\n'
break ;;
[Nn]|[Nn][Oo])
printf 'Abandoning installation.\n'
exit 0 ;;
'')
Err 0 'Empty response -- try again.' ;;
*)
Err 0 'Invalid response -- try again.' ;;
esac
done
LogFile='/tmp/autoexec-installer.log'
[ -f "$LogFile" ] || 1> "$LogFile"
chmod 644 "$LogFile"
TmpDir=`mktemp -d --suffix '_tfl'`
URL='https://raw.githubusercontent.com/terminalforlife'
URL="$URL/Extra/master/source/autoexec"
SigHandler(){
Sig=$?
printf 'Cleaning up temporary files:\n'
printf '* Removing '%s' ... ' "$TmpDir"
ErrCount=0
if rm -r "$TmpDir" 2>> "$LogFile"; then
printf '\033[32m[OK]\033[0m\n'
else
printf '\033[31m[ERR]\033[0m\n'
fi
if [ -s "$LogFile" ]; then
printf "Done! -- errors logged in: '${LogFile#$HOME}'\n"
else
printf 'Done!\n'
fi
exit $Sig
}
trap SigHandler EXIT INT QUIT ABRT TERM
printf 'Fetching files from GitHub:\n'
ErrCount=0
for File in 'completions' 'autoexec' 'autoexec.1.gz'; do
printf "* Downloading '%s' ... " "$File"
if $DLCMD "$TmpDir/$File" "$URL/$File" 2>> "$LogFile"; then
printf '\033[32m[OK]\033[0m\n'
printf '* Verifying file contents ... '
Contents=`< "$TmpDir/$File"`
if [ "$(head -n 1 "$TmpDir/$File")" = '404: Not Found' ]; then
ErrCount=$((ErrCount + 1))
printf '\033[31m[ERR]\033[0m\n'
rm "$TmpDir/$File" > /dev/null 2>&1
else
printf '\033[32m[OK]\033[0m\n'
fi
else
ErrCount=$((ErrCount + 1))
printf '\033[31m[ERR]\033[0m\n'
fi
done
[ $ErrCount -gt 0 ] && Err 1 'Failed to download one or more files.'
printf 'Setting correct file attributes:\n'
printf '* Assigning owner and group ... '
if chown 0:0 "$TmpDir"/* 2>> "$LogFile"; then
printf '\033[32m[OK]\033[0m\n'
else
printf '\033[31m[ERR]\033[0m\n'
fi
printf '* Assigning modes ... '
ErrCount=0
for File in 'autoexec:755' 'completions:644' 'autoexec.1.gz:644'; do
chmod ${File##*:} "$TmpDir/${File%:*}" 2>> "$LogFile" ||
ErrCount=$((ErrCount + 1))
done
if [ $ErrCount -eq 0 ]; then
printf '\033[32m[OK]\033[0m\n'
else
printf '\033[31m[ERR]\033[0m\n'
fi
printf 'Moving files into place:\n'
ErrCount=0
for File in\
\
'completions:/usr/share/bash-completion/completions/autoexec'\
'autoexec.1.gz:/usr/share/man/man1/autoexec.1.gz'\
'autoexec:/usr/bin/autoexec'
do
if [ -f "${File#*:}" ]; then
Replace='False'
while :; do
printf "File '%s' found.\n" "${File#*:}"
printf 'Replace existing file? [Y/N] '
read Input
case $Input in
[Yy]|[Yy][Ee][Ss])
Replace='True'
break ;;
[Nn]|[Nn][Oo])
Replace='False'
break ;;
'')
Err 0 'Empty response -- try again.' ;;
*)
Err 0 'Invalid response -- try again.' ;;
esac
done
[ "$Replace" = 'True' ] || continue
fi
printf "* Installing '%s' ... " "${File%%:*}"
if mv "$TmpDir/${File%%:*}" "${File#*:}" 2>> "$LogFile"; then
printf '\033[32m[OK]\033[0m\n'
else
printf '\033[31m[ERR]\033[0m\n'
fi
done
printf 'Setting up symbolic links:\n'
BCDir='/usr/share/bash-completion/completions'
if ! [ -L "$BCDir/ae" ]; then
printf "* Creating symlink for 'ae' completions ... "
if ln -s "$BCDir/autoexec" "$BCDir/ae" 2>> "$LogFile"; then
printf '\033[32m[OK]\033[0m\n'
else
printf '\033[31m[ERR]\033[0m\n'
fi
else
printf "Symlink for 'ae' completions already present\n"
fi
if ! [ -L '/usr/bin/ae' ]; then
printf "* Creating symlink for 'ae' executable ... "
if ln -s '/usr/bin/autoexec' '/usr/bin/ae' 2>> "$LogFile"; then
printf '\033[32m[OK]\033[0m\n'
else
printf '\033[31m[ERR]\033[0m\n'
fi
else
printf "Symlink for 'ae' executable already present\n"
fi
printf 'Checking executables work:\n'
printf "* Verifying 'autoexec' ... "
if autoexec -v > /dev/null 2>&1; then
printf '\033[32m[OK]\033[0m\n'
else
printf '\033[31m[ERR]\033[0m\n'
fi
printf 'Checking for dependencies:\n'
for File in '/usr/bin/autoexec'; do
printf "* Looking in '%s' file...\n" "$File"
FileErr=0
if ! [ -f "$File" ]; then
Err 0 "File '$File' missing."
FileErr=$((FileErr + 1))
elif ! [ -r "$File" ]; then
Err 0 "File '$File' unreadable."
FileErr=$((FileErr + 1))
fi
if [ $FileErr -ne 0 ]; then
Err 0 'Unable to determine dependencies.'
continue
fi
DepErr=0
Found='False'
while read Line; do
if [ "$Line" = '#' ]; then
continue
elif [ "$Line" = '# Dependencies:' ]; then
Found='True'
elif [ "$Found" = 'True' ]; then
# Line to help debug parsing on stubborn files.
#printf '\e[2;37m%s\e[0m\n' "$Line"
case $Line in
'#'------*)
break ;;
*)
case $Line in
'# '[!\ ]*)
printf '\033[1;93m%s\e[0m\n' "${Line#\# }" ;;
*)
DepErr=$((DepErr + 1)) ;;
esac ;;
esac
fi
done < "$File"
[ $DepErr -eq 0 ] || Err 0 'One or more invalid dependencies detected.'
done