| 
									
										
										
										
											2022-12-19 17:43:51 +01:00
										 |  |  | #!/bin/bash | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | # $1 Full path to the installer (unused) | 
					
						
							|  |  |  | # $2 Location of the Homebrew installation we may need to move into place | 
					
						
							|  |  |  | # $3 Target install location (unused) | 
					
						
							|  |  |  | # $4 System root directory (unused) | 
					
						
							| 
									
										
										
										
											2023-10-04 10:18:27 +01:00
										 |  |  | set -euo pipefail | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # disable analytics while installing | 
					
						
							|  |  |  | export HOMEBREW_NO_ANALYTICS_THIS_RUN=1 | 
					
						
							|  |  |  | export HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT=1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # verify the installation exists | 
					
						
							|  |  |  | # default to /opt/homebrew to make development/testing easier | 
					
						
							|  |  |  | homebrew_directory="${2:-/opt/homebrew}" | 
					
						
							|  |  |  | if [[ ! -d "${homebrew_directory:?}" ]] | 
					
						
							| 
									
										
										
										
											2022-12-23 09:10:01 +01:00
										 |  |  | then | 
					
						
							| 
									
										
										
										
											2023-10-06 10:27:03 +01:00
										 |  |  |   echo "No directory at ${homebrew_directory}!" >&2 | 
					
						
							| 
									
										
										
										
											2022-12-23 09:10:01 +01:00
										 |  |  |   exit 1 | 
					
						
							| 
									
										
										
										
											2022-12-19 17:43:51 +01:00
										 |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | # add Git to path | 
					
						
							|  |  |  | export PATH="/Library/Developer/CommandLineTools/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:${PATH}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 20:37:37 +08:00
										 |  |  | # avoid writing to user's global config file by overriding HOME | 
					
						
							|  |  |  | # https://git-scm.com/docs/git-config#SCOPES | 
					
						
							|  |  |  | unset XDG_CONFIG_HOME | 
					
						
							|  |  |  | export HOME="${homebrew_directory}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | # reset Git repository | 
					
						
							|  |  |  | cd "${homebrew_directory}" | 
					
						
							| 
									
										
										
										
											2024-04-11 20:37:37 +08:00
										 |  |  | git config --global --add safe.directory "${homebrew_directory}" | 
					
						
							|  |  |  | git reset --hard | 
					
						
							|  |  |  | git checkout --force master | 
					
						
							|  |  |  | git branch | grep -v '\*' | xargs -n 1 git branch --delete --force || true | 
					
						
							| 
									
										
										
										
											2024-04-11 04:15:59 +08:00
										 |  |  | rm "${homebrew_directory}/.gitconfig" | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # move to /usr/local if on x86_64 | 
					
						
							| 
									
										
										
										
											2022-12-23 09:10:01 +01:00
										 |  |  | if [[ $(uname -m) == "x86_64" ]] | 
					
						
							|  |  |  | then | 
					
						
							| 
									
										
										
										
											2023-10-04 10:18:27 +01:00
										 |  |  |   if [[ -f "/usr/local/bin/brew" && -d "/usr/local/Homebrew" ]] | 
					
						
							|  |  |  |   then | 
					
						
							|  |  |  |     cp -pRL "${homebrew_directory}/.git" "/usr/local/Homebrew/" | 
					
						
							|  |  |  |     mv "${homebrew_directory}/cache_api" "/usr/local/Homebrew/" | 
					
						
							| 
									
										
										
										
											2023-10-04 18:14:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 20:37:37 +08:00
										 |  |  |     export HOME="/usr/local/Homebrew" | 
					
						
							|  |  |  |     git config --global --add safe.directory /usr/local/Homebrew | 
					
						
							|  |  |  |     git -C /usr/local/Homebrew reset --hard | 
					
						
							|  |  |  |     git -C /usr/local/Homebrew checkout --force master | 
					
						
							| 
									
										
										
										
											2024-04-11 04:15:59 +08:00
										 |  |  |     rm /usr/local/Homebrew/.gitconfig | 
					
						
							| 
									
										
										
										
											2023-10-04 10:18:27 +01:00
										 |  |  |   else | 
					
						
							|  |  |  |     mkdir -vp /usr/local/bin | 
					
						
							|  |  |  |     mv "${homebrew_directory}" "/usr/local/Homebrew/" | 
					
						
							| 
									
										
										
										
											2023-07-27 15:51:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 10:18:27 +01:00
										 |  |  |     # create symlink to /usr/local/bin/brew | 
					
						
							|  |  |  |     ln -svf "../Homebrew/bin/brew" "/usr/local/bin/brew" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2022-12-19 17:43:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 10:18:27 +01:00
										 |  |  |   rm -rf "${homebrew_directory}" | 
					
						
							| 
									
										
										
										
											2023-07-27 15:51:40 +01:00
										 |  |  |   homebrew_directory="/usr/local/Homebrew" | 
					
						
							|  |  |  |   cd /usr/local | 
					
						
							| 
									
										
										
										
											2022-12-19 17:43:51 +01:00
										 |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | # create missing directories | 
					
						
							| 
									
										
										
										
											2024-06-13 17:32:38 -04:00
										 |  |  | mkdir -vp Caskroom Cellar Frameworks etc include lib opt sbin share var/homebrew/linked | 
					
						
							| 
									
										
										
										
											2022-12-19 17:43:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-06 10:27:03 +01:00
										 |  |  | # optionally define an install user at /var/tmp/.homebrew_pkg_user.plist | 
					
						
							|  |  |  | homebrew_pkg_user_plist="/var/tmp/.homebrew_pkg_user.plist" | 
					
						
							|  |  |  | if [[ -f "${homebrew_pkg_user_plist}" ]] && [[ -n $(defaults read "${homebrew_pkg_user_plist}" HOMEBREW_PKG_USER) ]] | 
					
						
							|  |  |  | then | 
					
						
							|  |  |  |   homebrew_pkg_user=$(defaults read /var/tmp/.homebrew_pkg_user HOMEBREW_PKG_USER) | 
					
						
							|  |  |  | # otherwise, get valid logged in user | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |   homebrew_pkg_user=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }') | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-19 17:43:51 +01:00
										 |  |  | # set permissions | 
					
						
							| 
									
										
										
										
											2024-06-13 17:32:38 -04:00
										 |  |  | chmod ug=rwx Caskroom Cellar Frameworks bin etc include lib opt sbin share var var/homebrew var/homebrew/linked | 
					
						
							| 
									
										
										
										
											2023-07-27 15:51:40 +01:00
										 |  |  | if [[ "${homebrew_directory}" == "/usr/local/Homebrew" ]] | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | then | 
					
						
							| 
									
										
										
										
											2024-06-13 17:32:38 -04:00
										 |  |  |   chown -h "${homebrew_pkg_user}:admin" bin bin/brew etc include lib opt sbin share var | 
					
						
							|  |  |  |   chown -h -R "${homebrew_pkg_user}:admin" Caskroom Cellar Frameworks Homebrew var/homebrew | 
					
						
							|  |  |  |   chown -h -R "${homebrew_pkg_user}" etc include share var | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | else | 
					
						
							| 
									
										
										
										
											2023-10-06 10:27:03 +01:00
										 |  |  |   chown -R "${homebrew_pkg_user}:admin" . | 
					
						
							| 
									
										
										
										
											2023-07-25 15:47:34 +01:00
										 |  |  | fi | 
					
						
							| 
									
										
										
										
											2023-07-26 15:32:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # move API cache to ~/Library/Caches/Homebrew | 
					
						
							| 
									
										
										
										
											2023-10-06 10:27:03 +01:00
										 |  |  | user_home_dir=$(dscl . read /Users/"${homebrew_pkg_user}" NFSHomeDirectory | awk '{ print $2 }') | 
					
						
							|  |  |  | user_cache_dir="${user_home_dir}/Library/Caches/Homebrew" | 
					
						
							|  |  |  | user_api_cache_dir="${user_cache_dir}/api" | 
					
						
							| 
									
										
										
										
											2023-07-26 15:32:14 +01:00
										 |  |  | mkdir -vp "${user_api_cache_dir}" | 
					
						
							|  |  |  | mv -v "${homebrew_directory}/cache_api/"* "${user_api_cache_dir}" | 
					
						
							| 
									
										
										
										
											2023-10-06 10:27:03 +01:00
										 |  |  | chown -R "${homebrew_pkg_user}:staff" "${user_cache_dir}" | 
					
						
							| 
									
										
										
										
											2023-07-26 15:32:14 +01:00
										 |  |  | rm -vrf "${homebrew_directory}/cache_api" |