From 8c11e15a6cd9b9124d30d05e065e7a4c6a84bfce Mon Sep 17 00:00:00 2001
From: "Kenneth Benzie (Benie)" <k.benzie83@gmail.com>
Date: Fri, 1 Sep 2017 00:11:38 +0100
Subject: [PATCH] Add bootstrap-macOS.sh

usage: bootstrap-macOS.sh [-h] [-y]

Bootstrap a macOS instance with:

* Xcode command line developer tools
* Homebrew - package manager
* python - from Homebrew
* virtualenv - from pip
* SSH key - from ssh-keygen
* GitHub public key - with SSH key
* GitLab public key - with SSH key
* BitBucket Cloud public key - with SSH key
* Gogs Cloud public key - with SSH key
* conduit - configuration manager

If any already exist they will not be reinstalled.

optional arguments:
        -h              show this help message and exit
        -y              assume yes when prompted
---
 bootstrap-macOS.sh | 110 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 110 insertions(+)
 create mode 100755 bootstrap-macOS.sh

diff --git a/bootstrap-macOS.sh b/bootstrap-macOS.sh
new file mode 100755
index 0000000..cb63d30
--- /dev/null
+++ b/bootstrap-macOS.sh
@@ -0,0 +1,110 @@
+#!/bin/bash
+
+set -e
+
+show_usage() {
+  echo "usage: $0 [-h] [-y]"
+}
+
+show_help() {
+  show_usage
+  echo
+  echo "Bootstrap a macOS instance with:"
+  echo
+  echo "* Xcode command line developer tools"
+  echo "* Homebrew - package manager"
+  echo "* python - from Homebrew"
+  echo "* virtualenv - from pip"
+  echo "* SSH key - from ssh-keygen"
+  echo "* GitHub public key - with SSH key"
+  echo "* GitLab public key - with SSH key"
+  echo "* BitBucket Cloud public key - with SSH key"
+  echo "* Gogs Cloud public key - with SSH key"
+  echo "* conduit - configuration manager"
+  echo
+  echo "If any already exist they will not be reinstalled."
+  echo
+  echo "optional arguments:"
+  echo "        -h              show this help message and exit"
+  echo "        -y              assume yes when prompted"
+}
+
+yes=0
+
+while getopts 'hy' opt; do
+  case $opt in
+    h) show_help; exit 0 ;;
+    y) yes=1 ;;
+    *) show_usage; exit 1 ;;
+  esac
+done
+
+missing() {
+  which $1 &> /dev/null && return 1 || return 0
+}
+
+agree() {
+  local check=^[Nn]$
+  [ $yes -eq 1 ] && [[ ! "$2" =~ $check ]] && return 0
+  [[ "$2" =~ $check ]] && local default="[y/N]" || local default="[Y/n]"
+  read -p "$1 $default? " answer
+  case "$answer" in
+    y|Y|yes) return 0 ;;
+    n|N|no) return 1 ;;
+    '') [[ "$2" =~ $check ]] && return 1 || return 0 ;;
+    *) echo "invalid input: $answer" && return `agree "$1"` ;;
+  esac
+}
+
+brew_install() {
+  brew install $1 > /dev/null
+}
+
+pip_install() {
+  pip install --user $1 > /dev/null
+}
+
+export PATH=~/.local/bin:$PATH
+export PYTHONUSERBASE=~/.local
+
+if ! xcode-select --print-path &> /dev/null; then
+  agree "Install Xcode command line developer tools" && xcode-select --install
+fi
+
+missing brew && agree "Intalll Homebrew" && /usr/bin/ruby -e \
+  "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
+
+agree "Update Homebrew packages" && brew update > /dev/null
+
+if missing /usr/local/bin/python2 && agree "Install Homebrew python"; then
+  brew_install python
+  export PATH=/usr/local/opt/python/libexec/bin:$PATH
+  echo "To use Homebrew python update your PATH:"
+  echo 'export PATH=/usr/local/opt/python/libexec/bin:$PATH'
+fi
+
+missing virtualenv && agree "Install virtualenv" && pip_install virtualenv
+
+if [ ! -f ~/.ssh/id_rsa ] && agree "Generate SSH key"; then
+  read -rp "SSH email: " email
+  [ ! -d ~/.ssh ] && mkdir -p ~/.ssh
+  ssh-keygen -t rsa -b 4096 -C "$email" -N "" -f ~/.ssh/id_rsa
+fi
+
+if ! missing virtualenv && agree "Set SSH keys on remote Git servers"; then
+  env=$(mktemp -d)
+  virtualenv $env &> /dev/null
+  source $env/bin/activate
+  pip install git+https://code.infektor.net/config/bootstrap.git > /dev/null
+  python -c 'import bootstrap; bootstrap.set_ssh_keys()'
+  deactivate
+  rm -r $env
+fi
+
+! missing pip && missing conduit && agree "Install conduit" && \
+  pip_install git+ssh://git@github.com/kbenzie/conduit.git
+
+echo "To use installed pip packages update your PATH and PYTHONUSERBASE:"
+echo 'export PATH=~/.local/bin:$PATH && export PYTHONUSERBASE=~/.local'
+
+[ -f $0 ] && agree "Remove $0" "N" && rm $0 || exit 0