brew/package/scripts/postinstall
Sean Molenaar f6a9980e49
fix: cleanup macOS packaging
Don't allow specifying target
Add `set -e` to shell scripts
Don't try and package on every PR

Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
2022-12-21 13:18:09 +01:00

43 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
set -e
# verify the files exist
tmp_brew="$2"
if [[ ! -d "${tmp_brew:?}" ]]; then
echo "no directory at $tmp_brew, exiting"
exit 1
fi
# pick the correct target
if [[ $(uname -m) == "x86_64" ]];then
target="/usr/local"
else
target="/opt/Homebrew"
fi
loggedInUser=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }')
if [[ -f "$target/bin/brew" ]]; then
if [[ $(sudo -u"$loggedInUser" "$target/bin/brew" developer) =~ "enabled" ]]; then
echo "developer install, exiting"
exit 0
fi
if [[ $("$tmp_brew/bin/brew" --version | head -n1) != $("$target/bin/brew" --version | head -n1) ]]; then
echo "already an outdated install at $target, updating"
sudo -u"$loggedInUser" "$target/bin/brew" update --auto-update
else
echo "already an up-to-date install at $target, exiting"
fi
exit 0
fi
group=$(id -gn "$loggedInUser")
install -d -o "$loggedInUser" -g "$group" "$target"
cp -RX "$tmp_brew/" "$target"
# set permissions
chown -R "$loggedInUser:$group" "$target/*"
# cleanup
rm -rf "${tmp_brew:?}/*"