-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-template.sh
executable file
·515 lines (441 loc) · 13.9 KB
/
bash-template.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env bash
# ##################################################
#
version="0.1" # Sets version variable
#
# HISTORY:
#
# * 20200211 - Initial Build
#
# ##################################################
function mainScript() {
# invoke verbose usage when set
if ${verbose}; then v="-v" ; fi
# Helper Functions
# ###################
function preflights() {
# Preflight checks that must be successful for the script to run further. Checks are:
# FIXME
true
}
function brewMaintenance () {
# brewMaintenance
# ------------------------------------------------------
# Will run the recommended Homebrew maintenance scripts
# ------------------------------------------------------
seek_confirmation "Run Homebrew maintenance?"
if is_confirmed; then
brew doctor
brew update
brew upgrade
fi
}
function brewCleanup () {
# This function cleans up an initial Homebrew installation
notice "Running Homebrew maintenance..."
# This is where brew stores its binary symlinks
binroot="$(brew --config | awk '/HOMEBREW_PREFIX/ {print $2}')"/bin
if [[ "$(type -P ${binroot}/bash)" && "$(cat /etc/shells | grep -q "$binroot/bash")" ]]; then
info "Adding ${binroot}/bash to the list of acceptable shells"
echo "$binroot/bash" | sudo tee -a /etc/shells >/dev/null
fi
if [[ "$SHELL" != "${binroot}/bash" ]]; then
info "Making ${binroot}/bash your default shell"
sudo chsh -s "${binroot}/bash" "$USER" >/dev/null 2>&1
success "Please exit and restart all your shells."
fi
brew cleanup
# if brew cask > /dev/null; then
# brew cask cleanup
# fi
}
# ###################
function installCommandLineTools() {
notice "Checking for Command Line Tools..."
if [[ ! "$(type -P gcc)" || ! "$(type -P make)" ]]; then
#local osx_vers=$(sw_vers -productVersion | awk -F "." '{print $2}')
local cmdLineToolsTmp="${tmpDir}/.com.apple.dt.CommandLineTools.installondemand.in-progress"
# Create the placeholder file which is checked by the software update tool
# before allowing the installation of the Xcode command line tools.
touch "${cmdLineToolsTmp}"
# Find the last listed update in the Software Update feed with "Command Line Tools" in the name
cmd_line_tools=$(softwareupdate -l | awk '/\*\ Command Line Tools/ { $1=$1;print }' | tail -1 | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 2-)
softwareupdate -i "${cmd_line_tools}" -v
# Remove the temp file
if [[ -f "${cmdLineToolsTmp}" ]]; then
rm ${v} "${cmdLineToolsTmp}"
fi
fi
success "Command Line Tools installed"
}
function installHomebrew () {
# Check for Homebrew
notice "Checking for Homebrew..."
if [[ ! "$(type -P brew)" ]]; then
notice "No Homebrew. Gots to install it..."
# Ensure that we can actually, like, compile anything.
if [[ ! $(type -P gcc) && "$OSTYPE" =~ ^darwin ]]; then
notice "XCode or the Command Line Tools for XCode must be installed first."
installCommandLineTools
fi
# Check for Git
if [ ! "$(type -P git)" ]; then
notice "XCode or the Command Line Tools for XCode must be installed first."
installCommandLineTools
fi
# Install Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
#installHomebrewTaps
fi
success "Homebrew installed"
}
function checkTaps() {
verbose "Confirming we have required Homebrew taps"
if ! brew cask help &>/dev/null; then
installHomebrewTaps
fi
if [ ! "$(type -P mas)" ]; then
installHomebrewTaps
fi
}
function installHomebrewTaps() {
# FIXME - add our taps here
#brew tap homebrew/dupes
#brew tap homebrew/versions
brew tap homebrew/cask-cask
#brew tap caskroom/fonts
#brew tap caskroom/versions # Subversion client for MacOS
}
function installCaskApps() { # Install apps regardless of physical or virtual machine.
unset LISTINSTALLED INSTALLCOMMAND RECIPES
notice "Checking for casks to install..."
checkTaps
LISTINSTALLED="brew cask list"
INSTALLCOMMAND="brew cask install --appdir=/Applications"
for item in "${RECIPES[@]}"; do
info "$item"
done
doInstall
success "Done installing cask apps"
}
function installHomebrewPackages() {
# FIXME - trim this list
unset LISTINSTALLED INSTALLCOMMAND RECIPES
notice "Checking for Homebrew packages to install..."
#checkTaps
LISTINSTALLED="brew list"
INSTALLCOMMAND="brew install"
RECIPES=(
# autoconf
# automake
bash
bash-completion
# colordiff
# coreutils
# ffmpeg
# gifsicle
git
# git-extras
# git-flow
# hub
# hr
# id3tool
# imagemagick
# jpegoptim
jq
# lesspipe
# libksba
# libtool
# libyaml
# mackup
# man2html
mas
mtr
# multimarkdown
nmap
# node
openssl
# optipng
# pkg-config
# pngcrush
# p7zip
# readline
# rename
shellcheck # Bash linter
# sl
# source-highlight
# ssh-copy-id
# sqlite
# tag
terminal-notifier
# tldr # Better man pages
# tree
# unison # Rsynch like tool
wget
awscli
aws-shell
azure-cli
python3
postgresql
pre-commit
terraform
terraform-docs
tflint
wget
)
doInstall
success "Done installing Homebrew packages"
}
function installSundry() {
# FIXME - remove this function if not needed.
true
}
# ###################
# Run the script
# ###################
# Ask for the administrator password upfront
echo "administrator authorisation required. Please enter your administrator password."
sudo -v
# Make sure we are signed into the app store
warning "You must be signed into the App Store for this script to work..."
warning "We will pause here while you go and do the sign in thing..."
read -n 1 -s -r -p "Press enter to continue"
checkTaps
brewCleanup
installHomebrewPackages
installCaskApps
#installDevApps
installSundry
}
## SET SCRIPTNAME VARIABLE ##
scriptName=$(basename "$0")
function trapCleanup() {
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -----------------------------------
echo ""
# Delete temp files, if any
if [[ -d "${tmpDir}" ]] ; then
rm -r "${tmpDir}"
fi
die "Exit trapped."
}
function safeExit() {
# safeExit
# -----------------------------------
# Non destructive exit for when script exits naturally.
# Usage: Add this function at the end of every script.
# -----------------------------------
# Delete temp files, if any
if [[ -d "${tmpDir}" ]] ; then
rm -r "${tmpDir}"
fi
trap - INT TERM EXIT
exit
}
function seek_confirmation() {
# Asks questions of a user and then does something with the answer.
# y/n are the only possible answers.
#
# USAGE:
# seek_confirmation "Ask a question"
# if is_confirmed; then
# some action
# else
# some other action
# fi
#
# Credt: https://github.com/kevva/dotfiles
# ------------------------------------------------------
input "$@"
if ${force}; then
notice "Forcing confirmation with '--force' flag set"
else
read -p " (y/n) " -n 1
echo ""
fi
}
function is_confirmed() {
if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
return 0
fi
return 1
}
function is_not_confirmed() {
if [[ "${REPLY}" =~ ^[Nn]$ ]]; then
return 0
fi
return 1
}
# Set Flags
# -----------------------------------
# Flags which can be overridden by user input.
# Default values are below
# -----------------------------------
quiet=false
printLog=false
verbose=false
force=false
strict=false
debug=false
args=()
# Set Temp Directory
# -----------------------------------
# Create temp directory with three random numbers and the process ID
# in the name. This directory is removed automatically at exit.
# -----------------------------------
tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$"
(umask 077 && mkdir "${tmpDir}") || {
die "Could not create temporary directory! Exiting."
}
# Logging
# -----------------------------------
# Log is only used when the '-l' flag is set.
#
# To never save a logfile change variable to '/dev/null'
# Save to Desktop use: $HOME/Desktop/${scriptName}.log
# Save to standard user log location use: $HOME/Library/Logs/${scriptName}.log
# -----------------------------------
logFile="${HOME}/Desktop/${scriptName}.log"
# Options and Usage
# -----------------------------------
# Print usage
usage() {
echo -n "${scriptName} [OPTION]... [FILE]...
This is a script template. Edit this description to print help to users.
${bold}Options:${reset}
-u, --username Username for script
-p, --password User password
--force Skip all user interaction. Implied 'Yes' to all actions.
-q, --quiet Quiet (no output)
-l, --log Print log to file
-s, --strict Exit script with null variables. i.e 'set -o nounset'
-v, --verbose Output more information. (Items echoed to 'verbose')
-d, --debug Runs script in BASH debug mode (set -x)
-h, --help Display this help and exit
--version Output version information and exit
"
}
# Iterate over options breaking -ab into -a -b when needed and --foo=bar into
# --foo bar
optstring=h
unset options
while (($#)); do
case $1 in
# If option is of type -ab
-[!-]?*)
# Loop over each character starting with the second
for ((i=1; i < ${#1}; i++)); do
c=${1:i:1}
# Add current char to options
options+=("-$c")
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# If option is of type --foo=bar
--?*=*) options+=("${1%%=*}" "${1#*=}") ;;
# add --endopts for --
--) options+=(--endopts) ;;
# Otherwise, nothing special
*) options+=("$1") ;;
esac
shift
done
set -- "${options[@]}"
unset options
# Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script
# -------------------------------------
# [[ $# -eq 0 ]] && set -- "--help"
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; safeExit ;;
--version) echo "$(basename $0) ${version}"; safeExit ;;
-u|--username) shift; username=${1} ;;
-p|--password) shift; echo "Enter Pass: "; stty -echo; read PASS; stty echo;
echo ;;
-v|--verbose) verbose=true ;;
-l|--log) printLog=true ;;
-q|--quiet) quiet=true ;;
-s|--strict) strict=true ;;
-d|--debug) debug=true ;;
--force) force=true ;;
--endopts) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
# Store the remaining part as arguments.
args+=("$@")
# Logging and Colors
# -----------------------------------------------------
# Here we set the colors for our script feedback.
# Example usage: success "sometext"
#------------------------------------------------------
# Set Colors
bold=$(tput bold)
reset=$(tput sgr0)
purple=$(tput setaf 171)
red=$(tput setaf 1)
green=$(tput setaf 76)
tan=$(tput setaf 3)
blue=$(tput setaf 38)
underline=$(tput sgr 0 1)
function _alert() {
if [[ "${1}" = "emergency" ]]; then local color="${bold}${red}"; fi
if [[ "${1}" = "error" ]]; then local color="${bold}${red}"; fi
if [[ "${1}" = "warning" ]]; then local color="${red}"; fi
if [[ "${1}" = "success" ]]; then local color="${green}"; fi
if [[ "${1}" = "header" ]]; then local color="${bold}""${tan}"; fi
if [[ "${1}" = "input" ]]; then local color="${bold}"; printLog="false"; fi
if [[ "${1}" = "info" ]] || [[ "${1}" = "notice" ]]; then local color=""; fi
# Don't use colors on pipes or non-recognized terminals
if [[ "${TERM}" != "xterm"* ]] || [[ -t 1 ]]; then color=""; reset=""; fi
# Print to $logFile
if ${printLog}; then
echo -e "$(date +"%m-%d-%Y %r") $(printf "[%9s]" "${1}") ${_message}" >> "${logFile}";
fi
# Print to console when script is not 'quiet'
if ${quiet}; then
return
else
echo -e "$(date +"%r") ${color}$(printf "[%9s]" "${1}") ${_message}${reset}";
fi
}
function die () { local _message="${*} Exiting."; echo "$(_alert emergency)"; safeExit;}
function error () { local _message="${*}"; echo "$(_alert error)"; }
function warning () { local _message="${*}"; echo "$(_alert warning)"; }
function notice () { local _message="${*}"; echo "$(_alert notice)"; }
function info () { local _message="${*}"; echo "$(_alert info)"; }
function debug () { local _message="${*}"; echo "$(_alert debug)"; }
function success () { local _message="${*}"; echo "$(_alert success)"; }
function input() { local _message="${*}"; echo -n "$(_alert input)"; }
function header() { local _message="${*}"; echo "$(_alert header)"; }
# Log messages when verbose is set to "true"
verbose() { if ${verbose}; then debug "$@"; fi }
# Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM
# Set IFS to preferred implementation
IFS=$' \n\t'
# Exit on error. Append '||true' when you run the script if you expect an error.
set -o errexit
# Run in debug mode, if set
if ${debug}; then set -x ; fi
# Exit on empty variable
if ${strict}; then set -o nounset ; fi
# Bash will remember & return the highest exitcode in a chain of pipes.
# This way you can catch the error in case mysqldump fails in `mysqldump |gzip`, for example.
set -o pipefail
# Run your script
mainScript
# Exit cleanly
safeExit