1#!/bin/bash
2
3# Script to install Homebrew on a Mac.
4# Author: richard at richard - purves dot com
5# Version: 1.0 - 21st May 2017
6
7# Heavily hacked by Tony Williams (honestpuck@gmail.com)
8# Latest version at https://github.com/Honestpuck/homebrew.sh
9# v2.0 - 19th Sept 2019
10# v2.0.1 Fixed global cache error
11# v2.0.2 Fixed brew location error
12# v2.0.3 Added more directories to handle
13
14# v3.0 Catalina version 2020-02-17
15# v3.1 | 2020-03-24 | Fix permissions for /private/tmp
16
17# Set up variables and functions here
18consoleuser="$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
19");')"
20
21# are we in the right group
22check_grp=$(groups ${consoleuser} | grep -c '_developer')
23if [[ $check_grp != 1 ]]; then
24 /usr/sbin/dseditgroup -o edit -a "${consoleuser}" -t user _developer
25fi
26
27# Logging stuff starts here
28LOGFOLDER="/private/var/log/"
29LOG="${LOGFOLDER}Homebrew.log"
30
31if [ ! -d "$LOGFOLDER" ]; then
32 mkdir $LOGFOLDER
33fi
34
35function logme()
36{
37# Check to see if function has been called correctly
38 if [ -z "$1" ] ; then
39 echo "$(date) - logme function call error: no text passed to function! Please recheck code!"
40 echo "$(date) - logme function call error: no text passed to function! Please recheck code!" >> $LOG
41 exit 1
42 fi
43
44# Log the passed details
45 echo -e "$(date) - $1" >> $LOG
46 echo -e "$(date) - $1"
47}
48
49# Check and start logging
50logme "Homebrew Installation"
51
52# Have the xcode command line tools been installed?
53logme "Checking for Xcode Command Line Tools installation"
54check=$( pkgutil --pkgs | grep -c "CLTools_Executables" )
55
56if [[ "$check" != 1 ]]; then
57 logme "Installing Xcode Command Tools"
58 # This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools
59 touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
60 clt=$(softwareupdate -l | grep -B 1 -E "Command Line (Developer|Tools)" | awk -F"*" '/^ +\\*/ {print $2}' | sed 's/^ *//' | tail -n1)
61 # the above don't work in Catalina so ...
62 if [[ -z $clt ]]; then
63 clt=$(softwareupdate -l | grep "Label: Command" | tail -1 | sed 's#* Label: (.*)#1#')
64 fi
65 softwareupdate -i "$clt"
66 rm -f /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
67 /usr/bin/xcode-select --switch /Library/Developer/CommandLineTools
68fi
69
70# Is homebrew already installed?
71if [[ ! -e /usr/local/bin/brew ]]; then
72 # Install Homebrew. This doesn't like being run as root so we must do this manually.
73 logme "Installing Homebrew"
74
75 mkdir -p /usr/local/Homebrew
76 # Curl down the latest tarball and install to /usr/local
77 curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C /usr/local/Homebrew
78
79 # Manually make all the appropriate directories and set permissions
80 mkdir -p /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc
81 mkdir -p /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin
82 mkdir -p /usr/local/share/zsh/site-functions /usr/local/var
83 mkdir -p /usr/local/share/doc /usr/local/man/man1 /usr/local/share/man/man1
84 chown -R "${consoleuser}":_developer /usr/local/*
85 chmod -R g+rwx /usr/local/*
86 chmod 755 /usr/local/share/zsh /usr/local/share/zsh/site-functions
87
88 # Create a system wide cache folder
89 mkdir -p /Library/Caches/Homebrew
90 chmod g+rwx /Library/Caches/Homebrew
91 chown "${consoleuser}:_developer" /Library/Caches/Homebrew
92
93 # put brew where we can find it
94 ln -s /usr/local/Homebrew/bin/brew /usr/local/bin/brew
95
96 # Install the MD5 checker or the recipes will fail
97 su -l "$consoleuser" -c "/usr/local/bin/brew install md5sha1sum"
98 echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' |
99 tee -a /Users/${consoleuser}/.bash_profile /Users/${consoleuser}/.zshrc
100 chown ${consoleuser} /Users/${consoleuser}/.bash_profile /Users/${consoleuser}/.zshrc
101
102 # clean some directory stuff for Catalina
103 chown -R root:wheel /private/tmp
104 chmod 777 /private/tmp
105 chmod +t /private/tmp
106fi
107
108# Make sure everything is up to date
109logme "Updating Homebrew"
110su -l "$consoleuser" -c "/usr/local/bin/brew update" 2>&1 | tee -a ${LOG}
111
112# updating git
113logme "Updating Git"
114su -l "$consoleuser" -c "/usr/local/bin/brew upgrade git"
115
116#forcing git version
117logme "Forcing git version"
118su -l "$consoleuser" -c "/usr/local/bin/brew link --force git"
119
120# logme user that all is completed
121logme "Installation complete"
122
123exit 0