brew/package/scripts/postinstall

72 lines
2.4 KiB
Plaintext
Raw Normal View History

2022-12-19 17:43:51 +01:00
#!/bin/bash
# $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)
set -euo pipefail
# 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
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
# add Git to path
export PATH="/Library/Developer/CommandLineTools/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:${PATH}"
# reset Git repository
cd "${homebrew_directory}"
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
git config --global --unset safe.directory "${homebrew_directory}"
# move to /usr/local if on x86_64
2022-12-23 09:10:01 +01:00
if [[ $(uname -m) == "x86_64" ]]
then
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/"
git -C /usr/local/Homebrew reset --hard
git -C /usr/local/Homebrew checkout --force master
else
mkdir -vp /usr/local/bin
mv "${homebrew_directory}" "/usr/local/Homebrew/"
# 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
rm -rf "${homebrew_directory}"
homebrew_directory="/usr/local/Homebrew"
cd /usr/local
2022-12-19 17:43:51 +01:00
fi
# create missing directories
mkdir -vp Cellar Frameworks etc include lib opt sbin share var/homebrew/linked
2022-12-19 17:43:51 +01:00
# set permissions
logged_in_user=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }')
group=$(id -gn "${logged_in_user}")
if [[ "${homebrew_directory}" == "/usr/local/Homebrew" ]]
then
chown -R "${logged_in_user}:${group}" Cellar Frameworks Homebrew bin etc include lib sbin share var/homebrew/linked
else
chown -R "${logged_in_user}:${group}" .
fi
# move API cache to ~/Library/Caches/Homebrew
user_api_cache_dir=~"${logged_in_user}"/Library/Caches/Homebrew/api
mkdir -vp "${user_api_cache_dir}"
mv -v "${homebrew_directory}/cache_api/"* "${user_api_cache_dir}"
rm -vrf "${homebrew_directory}/cache_api"