#!/usr/bin/env bash

# Wrapper for Firefox finding it with my flags no matter where it's installed
# to, mainly for use with a desktop entry.

set -x

if [ -f "$HOME/.local/bin/_a-usrlocalbin-functions.bash" ]; then
	. $HOME/.local/bin/_a-usrlocalbin-functions.bash
elif [ -f /usr/local/bin/_a-usrlocalbin-functions.bash ]; then
	. /usr/local/bin/_a-usrlocalbin-functions.bash
else
	echo "Library _a-usrlocalbin-functions.bash not found."
	exit 1
fi

export MOZ_ENABLE_WAYLAND=1
INHIBITWHO="firefox-bin"
FlagsForFirefox="--allow-downgrade $@"

# Firefox (stable, beta) and signatures: https://releases.mozilla.org/pub/firefox/releases/
# Developer edition (and signatures): https://releases.mozilla.org/pub/devedition/releases/
if [ -f ~/.local/firefox/firefox ]; then
	_inhibitshutdowncmd ~/.local/firefox/firefox $FlagsForFirefox
# Fallback to global installation
elif [ -f /usr/bin/firefox ]; then
	_inhibitshutdowncmd /usr/bin/firefox $FlagsForFirefox
# Fallback to global installation
elif [ -f /usr/bin/firefox-esr ]; then
	_inhibitshutdowncmd /usr/bin/firefox-esr $FlagsForFirefox
# Fallback to Snap which apparently follows /etc/policies.json
elif [ -f /var/lib/snapd/snap/bin/firefox ]; then
	_inhibitshutdowncmd snap run firefox $FlagsForFirefox
# Fallback to flatpak, don't care if it doesn't exist
else
	_inhibitshutdowncmd flatpak run org.mozilla.firefox $FlagsForFirefox
fi

set +x
