# Mac Setup Script

Borrowed and customized from nickytonline@github.

Does this work? They've done studies, you know. 60% of the time, it works every time.

#!/bin/sh

# log setup
timestamp=$(date +%Y-%m-%d)
logFile="./mac-setup-$timestamp.log"

# clear out log file
> $logFile

beginStep() {
    echo "DEBUG: Starting installation for $1..." | tee -a $logFile
}

endStep() {
    echo "DEBUG: Completed installation for $1.\n" | tee -a $logFile
}

isCmdInstalled() {
    command -v $1 >/dev/null 2>&1 || return 1

    return 0
}

function joinBy() {
    local IFS="$1";
    shift;
    echo "$*";
}

didBrewInstall() {
    local pkg=${1:-""}
    local checkCask=${2:-""}

    if [ -n "$checkCask" ]; then
        cmd="brew cask"
    else
        cmd="brew"
    fi

    if ! isCmdInstalled $1; then
        return 1
    fi

    local pgkPath=$($cmd ls --versions $pkg)

    if [ -n "$pgkPath" ]; then
        return 0
    else
        return 1
    fi
}

function installComposer() {
    local EXPECTED_SIGNATURE=$(wget -q -O - https://composer.github.io/installer.sig)
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    local ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');")

    if [ "$EXPECTED_SIGNATURE" != "$ACTUAL_SIGNATURE" ]
    then
        >&2 echo 'ERROR: Invalid installer signature'
        rm composer-setup.php
        return 1
    fi

    php composer-setup.php --quiet --install-dir=/usr/local/bin --filename=composer
    RESULT=$?
    rm composer-setup.php

    echo "Composer install script returned $RESULT"

    return $RESULT
}

brewPkgs=(
    node
    z
    zsh
)

brewCaskPkgs=(
    alfred
    boom-3d
    docker
    filezilla
    firefox
    font-input
    google-chrome
    iterm2
    ngrok
    sequel-pro
    sizeup
    slack
    spotify
    visual-studio-code
)

# TODO: implement
ohMyZshPlugins=(
    zsh-completions
    zsh-autosuggestions
)

# TODO: implement
yarnPkgs=(

)

vsCodeExtensions=(
    Dart-Code.dart-code
    KTamas.laravel-blade
    esbenp.prettier-vscode
    felixfbecker.php-intellisense
    marcostazi.VS-code-drupal
    mikestead.dotenv
    mrmlnc.vscode-apache
    octref.vetur
    pnp.polacode
    sysoev.language-stylus
    taniarascia.new-moon-vscode
    whatwedo.twig
)

customPath=(
    $HOME/.config/yarn/global/node_modules/.bin
    $HOME/.composer/vendor/bin
    /Applications/Visual Studio Code.app/Contents/Resources/app/bin
)

echo "Installing applications. You may be prompted at certain points to provide your password.\n" | tee -a $logFile

xcodeCLDTPath=$(xcode-select -p)

if [ -z "$xcodeCLDTPath" ]; then
    beginStep "Command Line Developer Tools"
    xcode-select --install | tee -a $logFile
    endStep "Command Line Developer Tools"
fi

# check for brew; install if not found
command -v brew >/dev/null 2>&1 || {
    beginStep "Homebrew"
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | tee -a $logFile
    endStep "Homebrew"

    beginStep "Homebrew Cask"
    brew tap caskroom/cask | tee -a $logFile
    endStep

    beginStep "Homebrew Cask Fonts"
    brew tap caskroom/fonts | tee -a $logFile
    endStep
} | tee -a $logFile

if ! didBrewInstall "ruby"; then
    beginStep "Moving Ruby to Brew"
    brew install ruby --force --overwrite | tee -a $logFile
    endStep "Moving Ruby to Brew"
fi

# check for composer; install if not found
command -v composer >/dev/null 2>&1 || {
    beginStep "Composer"
    installComposer | tee -a $logFile
    endStep "Composer"
}

# install homebrew packages
for pkg in "${brewPkgs[@]}"; do
    beginStep $pkg
    brew install $pkg | tee -a $logFile
    endStep $pkg
done

# oh my zsh
if ! didBrewInstall "zsh"; then
    sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" | tee -a $logFile
fi

# update path
customPathJoined=$(joinBy : "${customPath[@]}")

# install brew cask apps
for pkg in "${brewCaskPkgs[@]}"; do
    beginStep $pkg
    brew cask install $pkg | tee -a $logFile
    endStep $pkg
done

# install vscode extensions
for ext in "${vsCodeExtensions[@]}"; do
    beginStep $ext
    code --install-extension $ext | tee -a $logFile
    endStep $ext
done

# cleanup
brew upgrade
brew cleanup
brew cask upgrade
brew cask cleanup

echo "A setup log is available at $logFile."
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