#!/bin/bash # vim: ft=bash # # The purpose of this script is to bootstrap Bashmatic on a # new system. We are supporting OS-X and linux, although on # OS-X we do a lot more initialization to ensure Bashmatic # can properly function. # # Bootstrap script is required to run at least once on a new # system, which happens automatically if you install it via # # bash -c "$(curl -fsSL https://bashmatic.re1.re); bashmatic-install -v" # set +e # Configuration export HOME=${HOME:-"/"} declare -a CHOWN_DIRS declare -a BREW_PACKAGES export CHOWN_DIRS=(/usr/local/Cellar /usr/local/Homebrew /usr/local/Caskroom /opt/homebrew) export BREW_PACKAGES=(coreutils gnu-sed) # Constants export clr='\e[0m' # Text Reset export bldblk='\e[1;30m' # Black - Bold export bldred='\e[1;31m' # Red export bldgrn='\e[1;32m' # Green export bldylw='\e[1;33m' # Yellow export bldblu='\e[1;34m' # Blue export bldpur='\e[1;35m' # Purple export bldcyn='\e[1;36m' # Cyan export bldwht='\e[1;37m' # White export txtblk='\e[0;30m' # Black - Regular export txtred='\e[0;31m' # Red export txtgrn='\e[0;32m' # Green export txtylw='\e[0;33m' # Yellow export txtblu='\e[0;34m' # Blue export txtpur='\e[0;35m' # Purple export txtcyn='\e[0;36m' # Cyan export txtwht='\e[0;37m' # White # Output Helpers function .ts() { date '+%Y/%m/%d %I:%M:%S%p' } function .puts() { for str in "$@"; do printf "${clr}${bldwht}${bakblu} INFORMATION ➜ ${clr} ${txtblu}${str}${clr}\n" done } function .err() { for str in "$@"; do printf "${clr}${bldwht}${bakred} ERROR ➜ ${clr} ${bldred}${str}${clr}\n" done } function .wrn() { for str in "$@"; do printf "${clr}${txtblk}${bakylw} WARNING ➜ ${clr} ${bldylw}${str}${clr}\n" done } #—————————————————————————————————————————————————————————————————————————————————————— # Sudo Helpers #—————————————————————————————————————————————————————————————————————————————————————— function .sudo-ask() { sudo -n true 2>/dev/null || { .puts "Please enter your SUDO password: " sudo -v } } function .sudo-enable() { sudo mkdir -p "${bootstrap__sudoers_dir}" [[ -s ${bootstrap__sudoers_dir}/${USER} ]] || { echo "${USER}" 'ALL=(ALL) NOPASSWD: ALL' | sudo tee -a "${bootstrap__sudoers_dir}/${USER}" >/dev/null trap "sudo-disable" EXIT } } function .sudo-disable() { sudo rm -f "${bootstrap__sudoers_dir}/${USER}" } #—————————————————————————————————————————————————————————————————————————————————————— # BASH Upgrade Functionality (OS-X/linux) #—————————————————————————————————————————————————————————————————————————————————————— function .install.bash-major-version() { local bash="${1:-"bash"}" ${bash} --version | head -1 | sed 's/[^0-9.]//g; s/\..*$//g' } function .install.bash-latest-installed-version() { local version=$(bash-major-version "$(command -v bash)") if [[ -x /usr/local/bin/bash ]]; then local v=$(bash-major-version /usr/local/bin/bash) [[ ${v} -gt ${version} ]] && version="${v}" fi echo "${version}" } function .install.bash-install-required() { [[ $(bash-latest-installed-version) -lt 4 ]] } # Bash Installer Helpers function .install.bash-install() { .puts "Installing BASH v${bootstrap__bash_version} into ${bldylw}${bootstrap__bash_prefix}" .puts "Please wait..." .install.bash-from-sources "${bootstrap__bash_version}" "${bootstrap__bash_prefix}" } # Install BASH from sources function .install.bash-from-sources() { local version="$1" local prefix="${2:-"/usr/local"}" local bash_path="${prefix}/bin/bash" if [[ -x ${bash_path} ]]; then .err "BASH is already installed in ${bash_path}, version $(${bash_path} --version | head 1)" return 1 fi local bash_tar="bash-${version}.tar.gz" local bash_url="http://ftp.gnu.org/gnu/bash/${bash_tar}" local temp=/tmp/bash-sources set -exo pipefail rm -rf ${temp} mkdir -p ${temp} trap "rm -rf '${temp}'" EXIT # shellcheck disable=SC2064 trap "cd ${PWD} || true" EXIT cd "${temp}" || return 1 is-verbose &&.puts "Downloading BASH from ${bldylw}${bash_url}..." curl -sO "${bash_url}" tar xzf "${bash_tar}" cd "bash-${version}" is-verbose &&.puts "Configuring BASH in ${temp}, please wait..." ./configure --prefix="${prefix}" >/dev/null is-verbose &&.puts "Building BASH, please wait..." make -j 12 --silent set +e is-verbose &&.puts "Installing BASH into ${prefix}, please wait..." (make --silent install || sudo make --silent install) 2>/dev/null if [[ -x "${bash_path}" ]]; then export PATH="${prefix}/bin:${PATH}" grep -q "${bash_path}" /private/etc/shells || { is-verbose &&.puts "Appending ${bash_path} to /etc/shells..." echo "${bash_path}" | sudo tee -a /private/etc/shells >/dev/null } fi is-quiet ||.puts " ✅ BASH version ${version} is ${bldgrn}installed in ${bldylw}${prefix}." return 0 } function .install.bash-install-if-needed() { is-verbose &&.puts "Checking if I need to install modern BASH version..." bash-install-required || return .sudo-ask && .sudo-enable bash-install } #—————————————————————————————————————————————————————————————————————————————————————— # Check for locally modiifed files # Complain if bashmatic folder has locally modified files. #—————————————————————————————————————————————————————————————————————————————————————— function .install.git-sync() { is-verbose &&.puts "Checking if existing bashmatic folder is locally modified..." ((bootstrap__skip_git_check)) && return 0 cd "${BASHMATIC_HOME}" >/dev/null [[ -d ".git" ]] || return if [[ -n $(git status -s) ]]; then .wrn "It appears you already installed Bashmatic into ${BASHMATIC_HOME}." \ "Normally we would pull the latest changes during this operation," \ "but we detected locally modified files under ${bldwht}${BASHMATIC_HOME}." .puts "Please commit, stash or remove those files, and try again." .puts "For your information, here are the modifications:" echo git status -s || true echo return 1 else git checkout -q "${bootstrap__git_branch}" && git pull -q --rebase >/dev/null git checkout -q "${bootstrap__git_branch}" && git pull -q --rebase >/dev/null fi } #—————————————————————————————————————————————————————————————————————————————————————— # LINUX #—————————————————————————————————————————————————————————————————————————————————————— function .install.linux-requirements() { for package in unzip git curl; do command -v "${command}" >/dev/null || { apt-get install "${command}" -yqq || sudo apt-get install "${command}" -yqq } done } #—————————————————————————————————————————————————————————————————————————————————————— # OS-X #—————————————————————————————————————————————————————————————————————————————————————— function .install.osx-permissions() { local -a dirs for dir in "${CHOWN_DIRS[@]}"; do is-verbose &&.puts "Checking (and possibly changing) permissions of ${bldylw}${dir}..." [[ -d "${dir}" ]] || continue [[ $(stat -f %u "${dir}") -eq $(id -u) ]] && continue dirs+=( "${dir}" ) done [[ ${#dirs[@]} -eq 0 ]] && return 0 .sudo-ask && .sudo-enable for d in "${dirs[@]}"; do set -x sudo chown -R "${USER}" "${d}" set +x done } # Brew Installer Helpers # Install Brew function .install.brew-install() { is-verbose &&.puts "Installing HomeBrew if not already there..." command -v brew >/dev/null || { .puts "Installing Homebrew, please wait..." /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" } } function .install.brew-deps() { set +e is-verbose &&.puts "Installing required brew packages..." local brew_cache=/tmp/brew_cache.$$ trap "rm -f '${brew_cache}'" EXIT brew list --formulae -1 >"${brew_cache}" for package in "${BREW_PACKAGES[@]}"; do is-verbose &&.puts "Checking/installing brew package ${bldylw}${package}..." grep -q "${package}" "${brew_cache}" || brew install "${package}" 1>/dev/null 2>&1 done } # Uninstall Brew (just for completeness, we never actually use this) function .install.brew-uninstall() { echo y | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)" } function .install.darwin-requirements-xcode() { is-verbose && .puts "Verifying and installing XCode Dependencies..." command -v git >/dev/null && return 0 local xcode_app="/Applications/Xcode.app" local xcode_tools="${xcode_app}/Contents/Developer" local cli_tools="/Library/Developer/CommandLineTools" if [[ -d "${xcode_app}" && -d "${xcode_tools}" ]]; then [[ $(xcode-select -p) == "${xcode_tools}" ]] || sudo xcode-select --switch "${xcode_tools}" else command -v xcodebuild >/dev/null && command -v xcode-select >/dev/null && { xcode-select --install 2>&1 || { .err "Error running xcode-select --install" "You may need to download and install XCode Command Line Tools Manually." .puts "We'll open the URL where you can download the tools in 5 seconds." .puts "You will need to login with your Apple ID." sleep 1 .puts "When you are finished installing the tools, re-run Bashmatic Install script." sleep 4 .puts "Opening Apple website..." sleep 0.5 open "https://developer.apple.com/download/more/?=command%20line%20tools" exit 1 } } [[ -d "${cli_tools}" && $(xcode-select -p) == "${cli_tools}" ]] || sudo xcode-select --switch ${cli_tools} 2>/dev/null fi is-verbose &&.puts "Accepting XCode license..." bash -c 'command -v xcodebuild >/dev/null && sudo xcodebuild -license accept 2>&1 1>/dev/null; true ' >/dev/null } function .install.darwin-requirements() { .install.darwin-requirements-xcode .install.osx-permissions .install.brew-install .install.brew-deps } #—————————————————————————————————————————————————————————————————————————————————————— # Ensure OS-specific reqquirements are satisfied #—————————————————————————————————————————————————————————————————————————————————————— function .install.os-requirements() { case ${BASHMATIC_OS} in darwin) .install.darwin-requirements ;; linux) .install.linux-requirements ;; *) .err "Operating system ${BASHMATIC_OS} is not supported." exit 1 ;; esac } #—————————————————————————————————————————————————————————————————————————————————————— # Main Body #—————————————————————————————————————————————————————————————————————————————————————— unset BASHMATIC_DEBUG export bootstrap__skip_git_check=0 export bootstrap__skip_install=0 export bootstrap__verbose=0 export bootstrap__quiet=0 export bootstrap__force=0 export bootstrap__print=0 export bootstrap__skip_on_login=0 export bootstrap__bashmatic_home=${HOME}/.bashmatic export bootstrap__bash_version="5.1-rc2" export bootstrap__bash_prefix="/usr/local" export bootstrap__sudoers_dir="/private/etc/sudoers.d" export bootstrap__git_method="https" export bootstrap__git_branch="main" export BASHMATIC_OS="$(uname -s | tr '[:upper:]' '[:lower:]')" function is-debug() { ((BASHMASTIC_DEBUG)) } function is-verbose() { ((bootstrap__verbose)) } function is-quiet() { ((bootstrap__quiet)) } function is-force() { ((bootstrap__force)) } function .install.usage() { printf " ${bldylw}USAGE: ${bldgrn}bin/bashmatic-install [ flags ] ${bldylw}DESCRIPTION: ${bldblu}Install Bashmatic, and on OSX also installs build tools, brew and latest bash into /usr/local/bin/bash. ${bldylw}FLAGS:${clr} -m, --git-method [git|https] The default is 'https' unless your username is 'kig'. -b, --git-branch [branch|tag] Use a concrete branch or a tag when installing, defaults to the 'main' branch. -H, --bashmatic-home PATH Install bashmatic into PATH (default: ${bootstrap__bashmatic_home/${HOME}/\~}) -V, --bash-version VERSION Install BASH VERSION (default: ${bootstrap__bash_version}) -P, --bash-prefix PATH Install BASH into PATH (default: ${bootstrap__bash_prefix}) -l, --skip-on-login Do not install Bashmatic Hook into your dotfiles, which it does by the default. If you skip it, you can always change your mind later and add it to your shell dot files by running the following on the command line: You can always do so later with the following: ${bldgrn}$ ~/.bashmatic/bin/bashmatic load-at-login ${clr} This above will install the Bashmatic hook into your shell dotfile, eg ${txtcyn}~/.bash_profile.${clr} if you are on BASH, or ${txtcyn}~/.zshrc${clr} if you are on ZSH.. -g, --skip-git Do not abort if the destination has local changes -i, --skip-install Only install/verify prerequisites, skip install. -p, --print-home Print the identied canonical folder. -v, --verbose See additional output as bootstrap is running. -f, --force Force a reinstall of any existing target. -q, --quiet See only.error output. -d, --debug Print the values of configuration variables for debugging. -h, --help Show this help message. ${clr}\n\n" } function .install.parse-opts() { # Parse additional flags while :; do case $1 in -V | --bash-version) shift export bootstrap__bash_version=$1 shift ;; -P | --bash-prefix) shift export bootstrap__bash_prefix="$1" [[ -d ${bootstrap__bash_prefix} ]] && { .err "Prefix PATH ${bootstrap__bash_prefix} does not exist." exit 1 } shift ;; -H | --bashmatic-home) shift export bootstrap__bashmatic_home="$1" [[ -z ${bootstrap__bashmatic_home} ]] && { .err "-H requires a valid argument." exit 1 } shift ;; -m | --git-method) shift export bootstrap__git_method="$1" [[ ${bootstrap__git_method} == "https" || ${bootstrap__git_method} == "git" ]] || { .err "Invalid usage of -m, expected either https or git." exit 1 } [[ ${bootstrap__git_method} == "https" ]] && export bootstrap__skip_git_check=1 shift ;; -b | --git-branch) shift export bootstrap__git_branch="$1" [[ -z ${bootstrap__git_branch} ]] && { .err "-b flag requires either a tag or a branch name." exit 1 } shift ;; -p | --print) shift export bootstrap__print=1 ;; -d | --debug) shift export DEBUG=1 export BASHMATIC_DEBUG=1 export BASHMATIC_PATH_DEBUG=1 ;; -v | --verbose) shift export bootstrap__verbose=1 export bootstrap__quiet=0 ;; -q | --quiet) shift export bootstrap__verbose=0 export bootstrap__quiet=1 ;; -g | --skip-git) shift export bootstrap__skip_git_check=1 ;; -i | --skip-install) shift export bootstrap__skip_install=1 ;; -l | --skip-on-login) shift export bootstrap__skip_on_login=1 ;; -f | --force) shift export bootstrap__force=1 ;; -h | -\? | --help) shift .install.usage return 1 ;; --) # End of all options; anything after will be passed to the action function shift break ;; -?*) printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 exit 127 shift ;; *) [[ -z "$1" ]] && break shift ;; esac done if [[ "${bootstrap__git_method}" == "git" ]]; then export BASHMATIC_URL="git@github.com:kigster/bashmatic" elif [[ "${bootstrap__git_method}" == "https" ]]; then export BASHMATIC_URL="https://github.com/kigster/bashmatic" else .err "Invalid git method, please set to either git or https." exit 1 fi } function .install.download-and-install() { export BASHMATIC_HOME="${bootstrap__bashmatic_home}" local code=0 if [[ -d "${BASHMATIC_HOME}" && -f "${BASHMATIC_HOME}/init.sh" && ${bootstrap__force} -eq 0 ]]; then .install.git-sync || return 1 else ((bootstrap__force)) && [[ ${PWD} != "${BASHMATIC_HOME}" && -d ${BASHMATIC_HOME} ]] && { .wrn "Warning: removing existing ${BASHMATIC_HOME}..." rm -rf "${BASHMATIC_HOME}" } local cmd="git clone --depth 1 --branch \"${bootstrap__git_branch}\" -q \"${BASHMATIC_URL}\" \"${BASHMATIC_HOME}\"" ((bootstrap__verbose)) && eval "set -ex; ${cmd}; set +ex" ((bootstrap__verbose)) || eval "${cmd}" >/dev/null code=$? set +x fi if [[ ${code} -ne 0 ]]; then .err "Bashmatic could not install or update correctly." .err "Please run this manualy:\n\n\t${bldgrn}cd \$(dirname ${BASHMATIC_HOME} && git clone ${BASHMATIC_URL} ${BASHMATIC_HOME})\n\n" return 1 else export BASHMATIC_INIT="${BASHMATIC_HOME}/init.sh" [[ -s "${BASHMATIC_INIT}" ]] || { .err "Unexpected.error: ${BASHMATIC_INIT} was not found." return 1 } # shellcheck disable=SC1090 ((bootstrap_debug)) && source "${BASHMATIC_HOME}/.envrc.debug" source "${BASHMATIC_INIT}" ((bootstrap__skip_on_login)) || bashmatic.load-at-login is-quiet || success "Your BashMatic has been successfully installed." return 0 fi } function .install.run-not-source() { .err "This script is meant to be run, not sourced in." .puts "However, since you've already loaded it you can fun functions:" } function .install.current-shell() { if [[ -z $(which grep) || -z $(which sed) || -z $(which cut) ]]; then # something is seriosly screwy with the system's PATH. # Perhaps we are in a Docker container. export PATH="${PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/opt/local/bin:/opt/bin" fi [[ -z ${LC_CTYPE} ]] && export LC_CTYPE=C [[ -z ${LANG} ]] && export LANG=C [[ -n ${current_shell} ]] && /bin/ps -p $$ -o args | grep -q "${current_shell}" && { printf -- '%s' "${current_shell/ /}" | tr -d ' ' return 0 } ( # deterministicaly figure out our currently loaded shell. local bash_process="$(/bin/ps -p $$ -o args | grep -v -E 'ARGS|COMMAND' | cut -d ' ' -f 1 | sed -E 's/-//g')" current_shell="$(basename "${bash_process:-bash}")" export current_shell if [[ "${current_shell}" =~ zsh ]]; then command -v zsh elif [[ "${current_shell}" =~ bash ]]; then command -v bash else .wrn "Detected an unsupported shell type: ${current_shell}" >&2 command -v "${current_shell}" fi ) | tr -d ' ' } function .install.canonical-source() { current_shell=$(.install.current-shell) if [[ "${current_shell}" =~ zsh ]]; then printf "%s" "$0:A" elif [[ "${current_shell}" =~ bash ]]; then printf "%s" "${BASH_SOURCE[0]}" else printf "%s" "$0" fi } function canonical-dir() { local dir="$1" [[ -d "${dir}" ]] || { .err "Unable to determine canonical dir form source: ${dir}" return 1 } (cd "${dir}" || exit 1; pwd -P) } function .install.canonical-dir() { local source=$(.install.canonical-source) canonical-dir "$(dirname "${source}")" } function .install.print-directories() { local dir="$(.install.canonical-dir)" local source="$(.install.canonical-source)" local source_script [[ -n "${source}" ]] && source_script="$(basename "${source}")" printf -- "BASH-install's directory : ${bldgrn}%-40s${clr}\n" "${dir}" printf -- "BASH-install's full path : ${bldgrn}%-40s${clr}\n" "${dir}/${source_script}" printf -- "Active shell right now : ${bldgrn}%-20s${clr}\n" "$(.install.current-shell | sed -E 's/[ ]//g' | tr -d ' ')" } function .install.header() { printf -- "${bldwht}${bakred} ➜ Bashmatic Installer (verbose mode) ${clr}\n" } function .install.bashmatic-install() { .install.parse-opts "$@" || return ((bootstrap__verbose)) && .install.header ((bootstrap__print)) && .install.print-directories .install.os-requirements ((bootstrap__skip_install)) || .install.download-and-install } function bashmatic-install() { .install.bashmatic-install "$@" } function .install.abort() { echo .err 'Aborting due to user action.' trap '' INT exit 127 } len=${#BASH_SOURCE[@]} last_index=$((len - 1)) [[ ${last_index} -lt 0 ]] && last_index=0 current_shell=$(.install.current-shell) set +e # Allow sourcing in bin/bashmatic-install and using it's shell functions, eg: # source bin/bashmatic-install init if [[ ${current_shell} =~ "bash" ]] && [[ -n ${BASH_VERSION} && "$0" != "${BASH_SOURCE[${last_index}]}" ]]; then echo >/dev/null elif [[ ${current_shell} =~ "zsh" ]] && [[ -n ${ZSH_EVAL_CONEXT} && ${ZSH_EVAL_CONTEXT} =~ :file$ ]]; then echo >/dev/null else .install.bashmatic-install "$@" fi