2023-12-21 12:28:13 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Save this file as ~/.config/yadm/bootstrap and make it executable. It expects
|
2023-12-21 21:05:50 -05:00
|
|
|
# environment variable `file` with a name of shell script to execute.
|
|
|
|
# `file` can be relative to ~/.config/yadm/ or has full path.
|
2023-12-21 12:28:13 -05:00
|
|
|
#
|
|
|
|
# Usage:
|
2023-12-21 21:05:50 -05:00
|
|
|
# file=install yadm bootstrap
|
2023-12-21 12:28:13 -05:00
|
|
|
# or
|
2023-12-21 21:05:50 -05:00
|
|
|
# file=~/.config/yadm/install yadm bootstrap
|
2023-12-21 12:28:13 -05:00
|
|
|
#
|
|
|
|
# where `~/.config/yadm/install` can be like this:
|
|
|
|
#
|
2023-12-21 21:05:50 -05:00
|
|
|
# [[ ! $(type -t install) = 'function' ]] && echo "Usage: file=$(basename "$0") yadm bootstrap" && exit 1
|
2023-12-21 12:28:13 -05:00
|
|
|
#
|
|
|
|
# confirm yes 'softwareupdate --agree-to-license --install --all' "$(info 'Install ' warning 'OSX updates')"
|
|
|
|
# install 'brew' '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"'
|
|
|
|
#
|
|
|
|
# brew bundle --global
|
|
|
|
#
|
|
|
|
|
|
|
|
TEXT_COLOR_RED=$(tput setaf 1)
|
|
|
|
TEXT_COLOR_GREEN=$(tput setaf 2)
|
|
|
|
TEXT_COLOR_BLUE=$(tput setaf 4)
|
|
|
|
TEXT_COLOR_YELLOW=$(tput setaf 3)
|
|
|
|
TEXT_DEFAULT=$(tput sgr0)
|
|
|
|
|
|
|
|
info() {
|
2023-12-21 12:40:46 -05:00
|
|
|
printf "${TEXT_COLOR_BLUE}%s${TEXT_DEFAULT}" "$1"; "${@:2}"
|
2023-12-21 12:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
error() {
|
2023-12-21 12:40:46 -05:00
|
|
|
printf "${TEXT_COLOR_RED}%s${TEXT_DEFAULT}" "$1"; "${@:2}"
|
2023-12-21 12:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
success() {
|
2023-12-21 12:40:46 -05:00
|
|
|
printf "${TEXT_COLOR_GREEN}%s${TEXT_DEFAULT}" "$1"; "${@:2}"
|
2023-12-21 12:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
warning() {
|
2023-12-21 12:40:46 -05:00
|
|
|
printf "${TEXT_COLOR_YELLOW}%s${TEXT_DEFAULT}" "$1"; "${@:2}"
|
2023-12-21 12:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
source_if_exists() {
|
2023-12-21 21:05:50 -05:00
|
|
|
local src=$1
|
|
|
|
local verbose="${2:-no}"
|
|
|
|
|
|
|
|
[[ ! -f $src ]] && return 1;
|
|
|
|
[[ "${verbose:0:1}" == v* ]] && printf "%s\n\n" "$(info "-> $src")";
|
|
|
|
source "$src"
|
2023-12-21 12:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
command_exists () {
|
2023-12-21 12:40:46 -05:00
|
|
|
command -v "$1" &> /dev/null
|
2023-12-21 12:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# install a 'command' via 'code' if it was not previously installed yet
|
|
|
|
install() {
|
|
|
|
local command=$1
|
|
|
|
local code=$2
|
|
|
|
local message=$(info 'Installing ' warning "$command")
|
|
|
|
|
|
|
|
if command_exists "$command"; then
|
|
|
|
echo "$message...[$(success 'already')]"
|
|
|
|
else
|
|
|
|
echo "$message..."
|
|
|
|
eval "$code"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# execute a 'code' after confirmation from user
|
|
|
|
confirm() {
|
|
|
|
local default_answer="${1:-no}"
|
|
|
|
local code=$2
|
|
|
|
local message=$3
|
2023-12-21 12:40:46 -05:00
|
|
|
local options answer
|
2023-12-21 12:28:13 -05:00
|
|
|
|
|
|
|
if [[ "${default_answer:0:1}" == y* ]]; then
|
2023-12-21 12:40:46 -05:00
|
|
|
options="$(success 'YES')/no"
|
|
|
|
else
|
|
|
|
options="yes/$(success 'NO')"
|
|
|
|
fi
|
2023-12-21 12:28:13 -05:00
|
|
|
|
|
|
|
read -r -p "${message} [${options}]:" answer
|
|
|
|
answer=${answer:-$default_answer}
|
|
|
|
|
|
|
|
if [[ "${answer:0:1}" == y* ]]; then
|
|
|
|
eval "$code"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-12-21 21:05:50 -05:00
|
|
|
source_if_exists "$(dirname "${0}")/$file" v ||
|
|
|
|
source_if_exists $file v ||
|
|
|
|
([[ -z $file ]] && echo "Usage: file=FILENAME yadm bootstrap" || echo "Can't locate file '$file' for bootstrapping")
|