# Function to open the default browser with a specified profile
open_browser() {
    local profile="$1"
    shift
    firefox -P "$profile" "$@"
}

# Go up the parent chain of this process to determine if the link was clicked on
# in Slack. If so, open the `work` profile in the browser.
parent=$PPID
while [ "$parent" -ne 1 ]; do
    cmd=$(ps -o comm= -p "$parent")
    if [[ "$cmd" == "slack" ]]; then
        open_browser "work" "$@"
        exit 0
    fi
    parent=$(ps -o ppid= -p "$parent" | tail -n 1 | awk '{print $1}')
done

if [[ ${1:-} == "https://github.com/hausgold"* ]]; then
  # Also use the `work` profile for hausgold Github links
  open_browser "work" "$@"
else
  # Otherwise just use the default profile
  open_browser "default" "$@"
fi
