#!/usr/bin/env bash

# Homebrew workaround particularly for Fedora Atomic.
# Based on the syncplay script in this folder and the patching idea is from
# https://github.com/orgs/Homebrew/discussions/1282#discussioncomment-1281288

if [ -d /var/roothome ]; then
	set -x
	export LC_ALL=C.utf8

	if ! hash xdg-user-dir 2> /dev/null; then
		echo "xdg-user-dir is required for my laziness"
		exit 1
	fi

	if [ -f "$(xdg-user-dir RUNTIME)/brew.$(date -I)" ]; then
		echo "Homebrew has been patched today. If this is wrong, rm $(xdg-user-dir RUNTIME)/brew.$(date -I)"
	else
		if [ ! -d "/home/linuxbrew/.linuxbrew" ]; then
			echo "Homebrew not found."
			exit 1
		fi

		cd /home/linuxbrew/.linuxbrew/bin/
		cp /home/linuxbrew/.linuxbrew/bin/brew /home/linuxbrew/.linuxbrew/bin/brew2
		sed -i 's/pwd\ \-P/pwd/g' /home/linuxbrew/.linuxbrew/bin/brew2
		touch "$(xdg-user-dir RUNTIME)/brew.$(date -I)"
	fi

	/home/linuxbrew/.linuxbrew/bin/brew2 $@
	set +x
else
	/home/linuxbrew/.linuxbrew/bin/brew $@
fi
