#!/bin/bash XDG_CONFIG_HOME="${HOME}/.config" PATH="/opt/homebrew/bin:$PATH" PROJECTS_HOME="${HOME}/Projects" DOTFILES_REPO="https://github.com/pvande/dotfiles" DOTFILES_HOME="${PROJECTS_HOME}/dotfiles" abort() { echo '‼️ ' "$@" "See $LOGFILE for details." exit 1 } announce() { echo '📢 ' "$@" } warn() { echo '⛔️ ' "$@" } info() { echo '💡 ' "$@" } good() { echo '✅ ' "$@" } installed() { $BINDIR/brew ls $@ &>/dev/null } postinstall() { local var="postinstall_$1" [ "${!var}" != '-' ] && { echo "❗️ " "$1 Caveats" { if [ -z "${!var}" ]; then brew info $1 | sed '/==> Caveats/,/==>/!d;//d' else echo -ne "${!var}" fi } | sed -e 's/^/ /' } } ensure-line-in-file() { local file="$1" shift local line="$*" grep -qs "$line" "$file" || echo "$line" >> "$file" } global-gitignore() { ensure-line-in-file "${XDG_CONFIG_HOME}/git/ignore" $* } TMPDIR=$(mktemp -d) announce "Configuring $(hostname -s)" announce "Please make sure you're signed in to the App Store!" echo -n "Press Enter to continue..." read sudo_xcode_tools="[ ! -x $('/usr/bin/xcode-select' -print-path 2>/dev/null || echo '/dev/null')/usr/bin/git ]" sudo_homebrew="[ ! $(which brew) ]" if ($sudo_xcode_tools) || ($sudo_homebrew); then warn "Super-user privileges are required for the following changes:" $sudo_xcode_tools && warn " Installing the Xcode Command Line Tools" $sudo_homebrew && warn " Installing Homebrew" sudo -k sudo -v if $sudo_xcode_tools; then info "Installing the Xcode Command Line Tools..." sudo touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress package="$(softwareupdate -l | grep -E "[*] Label: Command Line (Developer|Tools)" | awk -F": " '{print $2}' | tail -n1)" if [ -z "$package" ]; then abort "Could not find XCode Tools package!" else sudo /usr/sbin/softwareupdate -i "$package" &>/dev/null fi sudo rm -f /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress sudo '/usr/bin/xcode-select' --switch /Library/Developer/CommandLineTools &>/dev/null fi if $sudo_homebrew; then info "Installing Homebrew..." /bin/bash -c "NONINTERACTIVE=1 $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" fi info "Releasing super-user privileges." sudo -k fi good "Xcode Command Line Tools are present..." good "Homebrew is installed and present..." eval "$(brew shellenv)" LOGFILE="${TMPDIR}/dotfiles.log" if [ ! -d $PROJECTS_HOME/dotfiles ]; then info "Cloning $DOTFILES_REPO to $DOTFILES_HOME..." { git clone $DOTFILES_REPO $DOTFILES_HOME } &>$LOGFILE || abort "Could not clone $DOTFILES_REPO!" elif !(cd $DOTFILES_HOME && git fetch -q && git status | grep -q "Your branch is up to date with 'origin/master'"); then info "Updating $DOTFILES_HOME..." { cd $DOTFILES_HOME && git merge origin/master } &>$LOGFILE || abort "Could not update $DOTFILES_REPO!" fi good "dotfiles repository is up-to-date..." for sh in $DOTFILES_HOME/config/*.sh ; do [ -r "${sh}" ] && LOGFILE="${TMPDIR}/$(basename "$sh" .sh).log" source "${sh}" done unset sh for cmd in $(brew list --formulae); do if (brew info $cmd | grep -q Caveats); then postinstall $cmd fi done for cmd in $(brew list --casks); do if (brew info homebrew/cask/$cmd | grep -q Caveats); then postinstall homebrew/cask/$cmd fi done rm -r $TMPDIR announce "All set!"