-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
247 lines (204 loc) · 6.16 KB
/
install
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
#!/bin/sh
INSTALL_DIR="${HOME}/.shell"
BACKUP_DIR="${INSTALL_DIR}/backup"
TARBALL_URL='https://api.github.com/repos/djlambert/shell-profile/tarball/master'
TARBALL_FILE=".shell-profile_${RANDOM}${RANDOM}.tgz"
PATH_FILE_BASE='paths.local'
PATH_FILE_LEGACY="${INSTALL_DIR}/${PATH_FILE_BASE}"
PATH_FILE_PREPEND="${PATH_FILE_LEGACY}.prepend"
VAR_FILE_LOCAL="${INSTALL_DIR}/vars.local"
BOOTSTRAP_FILES='profile bashrc zprofile'
INSTALL_FILES='dir_colors vimrc'
INSTALL_FORCE=0
INSTALL_LOCAL=0
INSTALL_EXISTING=0
INSTALL_SAVE_PATH=1
TIMESTAMP=$(date +%Y%m%d%H%M%S)
DEBUG=1
RESULT=0
cancel() {
printf 'Cancelling...\n'
}
ioOpen() {
exec 3>/dev/null
exec 4>/dev/null
}
ioClose() {
3>&-
4>&-
}
ioCopyStd() {
exec 3>&1
exec 4>&2
}
checkDebug() {
if [ $((${DEBUG:-0})) -eq 1 ]; then
ioCopyStd
printf 'Debugging enabled\n'
fi
}
ioCloseAndExit() {
LAST_RESULT=$?
if [ -z $RESULT ]; then
RESULT=$LAST_RESULT
fi
ioClose
exit $RESULT
}
findSetting() {
if [ ! -f "${2}" ]; then
return 0
fi
while read -r LINE; do
if expr "${LINE}" : "${1}=" >/dev/null; then
MATCH="${LINE}"
fi
done < "${2}"
if [ -n "${MATCH}" ]; then
echo "${MATCH}"
return 1
else
return 0
fi
}
cleanUp() {
RESULT=$?
if [ $RESULT -ne 0 ] && [ ${INSTALL_EXISTING:-0} -ne 1 ]; then
printf 'Cleaning up...\n'
if [ -d "${INSTALL_DIR}" ]; then
printf 'Removing install directory\n'
rm -rf "${INSTALL_DIR}" 1>&3 2>&4
fi
fi
if [ -f "${TARBALL_FILE}" ]; then
printf 'Removing tarball\n'
rm "${TARBALL_FILE}"
fi
ioCloseAndExit
}
trap cancel INT
ioOpen
# Set exit handler
trap ioCloseAndExit TERM EXIT
checkDebug
# Process command line options
while getopts i:flp opt; do
case $opt in
i) INSTALL_DIR="${OPTARG}";; # Specify install directory
f) INSTALL_FORCE=1;; # Force install over existing files
l) INSTALL_LOCAL=1;; # Use current directory as install source
p) INSTALL_SAVE_PATH=0;; # Save existing $PATH
?)
printf 'Usage: %s: [-f][-l][-p][-i install dir]\n' "$0"
exit 2
;;
esac
done
# Create install directory
# Exit with error on failure or directory exists and not force install
if [ -d "${INSTALL_DIR}" ]; then
if [ ${INSTALL_FORCE:-0} -ne 1 ]; then
printf "Aborting install, directory %s already exists\n" "${INSTALL_DIR}"
exit 1
else
INSTALL_EXISTING=1
printf 'Installing over existing files in %s\n' "${INSTALL_DIR}"
fi
else
if ! mkdir "${INSTALL_DIR}" 1>&3 2>&4; then
printf 'Aborting install, error creating directory %s\n' "${INSTALL_DIR}"
exit 1
fi
fi
# Set exit handler
# Changes may now have been made
trap cleanUp TERM EXIT
# Create backup directory
# Continue if exists (force install)
# Exit with error on failure
if [ ! -d "${BACKUP_DIR}" ]; then
mkdir "${BACKUP_DIR}" 1>&3 2>&4
if [ $? -ne 0 ]; then
printf 'Aborting install, error creating backup directory %s\n' "${INSTALL_DIR}"
exit 1
fi
fi
# Determine tar executable
if [ "$(uname)" = 'SunOS' ]; then
printf 'Detected SunOS\n'
TAR_CMD="gtar"
else
TAR_CMD="tar"
fi
# Download package files
# Continue on local install source
if [ ${INSTALL_LOCAL:-0} -ne 1 ]; then
printf 'Downloading package files...\n'
if command -v wget >/dev/null 2>&1; then
getCommand="wget --no-verbose --no-check-certificate --output-document=${TARBALL_FILE} ${TARBALL_URL}"
elif command -v curl >/dev/null 2>&1; then
getCommand="curl --silent --fail --insecure --location --output ${TARBALL_FILE} ${TARBALL_URL}"
else
printf 'Aborting install, wget or curl not found\n'
exit 1
fi
if ! ${getCommand} 2>&4; then
printf 'Aborting install, error downloading installation files\n'
exit 1
fi
if ! ${TAR_CMD} -xzC "${INSTALL_DIR}" --strip-components=1 -f "${TARBALL_FILE}" 1>&3 2>&4; then
printf 'Aborting install, error extracting installation files\n'
exit 1
fi
else
printf 'Installing from local source\n'
cp ./* "${INSTALL_DIR}"
fi
# Install bootstrap files
# Create backup if non-bootstrap exists
for basename in $BOOTSTRAP_FILES; do
filename=".${basename}"
destFile="${HOME}/${filename}"
bakFile="${BACKUP_DIR}/${basename}.${TIMESTAMP}"
if [ -f "${destFile}" ] && ! grep -q 'shell-profile-bootstrap' "${destFile}"; then
printf 'Saving %s to %s\n' "${destFile}" "${bakFile}"
mv "${destFile}" "${bakFile}" 1>&3 2>&4
fi
printf 'Bootstrapping %s\n' "${basename}"
sed "s|##INSTALL##|$INSTALL_DIR|;s|##SCRIPT##|${basename}|" "${INSTALL_DIR}/bootstrap" > "${destFile}"
done
# Copy additional files
# Create backup if not package file
# TODO error handling
for basename in $INSTALL_FILES; do
# TODO track file operations to allow for error recovery in cleanup
filename=".${basename}"
destFile="${HOME}/${filename}"
bakFile="${BACKUP_DIR}/${basename}.${TIMESTAMP}"
if [ -f "${destFile}" ] && ! grep -q 'shell-profile-bootstrap' "${destFile}"; then
printf 'Saving %s to %s\n' "${destFile}" "${bakFile}"
mv "${destFile}" "${bakFile}" 1>&3 2>&4
fi
printf 'Installing %s\n' "${basename}"
cp "${INSTALL_DIR}/${basename}" "${destFile}"
done
# Migrate old paths.local file
if [ -f "${PATH_FILE_LEGACY}" ]; then
bakFile="${BACKUP_DIR}/${PATH_FILE_BASE}.${TIMESTAMP}"
printf 'Moving legacy paths from %s to %s\n' "${PATH_FILE_LEGACY}" "${PATH_FILE_PREPEND}"
cat "${PATH_FILE_LEGACY}" >> "${PATH_FILE_PREPEND}"
printf 'Moving legacy paths file to %s\n' "${bakFile}"
mv "${PATH_FILE_LEGACY}" "${bakFile}"
fi
if [ "${INSTALL_SAVE_PATH}" -ne 1 ]; then
printf 'Skipping saving existing path\n'
else
findSetting 'PATH' "${VAR_FILE_LOCAL}" >/dev/null
if [ $? -ne 0 ]; then
printf 'PATH already in %s\n' "${VAR_FILE_LOCAL}"
else
printf 'Saving current path in %s\n' "${VAR_FILE_LOCAL}"
printf 'PATH=%s\n' "${PATH}" >> "${VAR_FILE_LOCAL}"
fi
fi
# vim: syntax=sh:ts=4:sw=4