#!/usr/bin/env bash --login 

GITEXEC=""
if ! which git > /dev/null; then
	# Gits official installer puts it at /usr/local/git/bin/git so we'll check there 
	# since MAS can't use the profile
	if ! [ -e "/usr/local/git/bin/git" ] ; then
		if which xcrun > /dev/null ; then
			if ! xcrun -find git > /dev/null; then
				echo "Could not locate git. Is it installed?" >&2
				exit 201
			else
				GITEXEC="xcrun git"
			fi
		else
			exit 201
		fi
	else
		GITEXEC="/usr/local/git/bin/git"
	fi
else
	GITEXEC="git"
fi

($GITEXEC config --global --remove-section difftool.Kaleidoscope;
$GITEXEC config --global --unset difftool.prompt;
$GITEXEC config --global --unset diff.tool;
$GITEXEC config --global --remove-section mergetool.Kaleidoscope;
$GITEXEC config --global --unset mergetool.prompt;
$GITEXEC config --global --unset merge.tool) 2>&1
# We don't really care if this fails or not

#Clean up after git since it doesn't see empty configuration entries
if ! $GITEXEC config --global --get-all merge 2>&1; then
	$GITEXEC config --global --remove-section merge; 2>&1
fi

if ! $GITEXEC config --global --get-all mergetool 2>&1; then
	$GITEXEC config --global --remove-section mergetool; 2>&1
fi

if ! $GITEXEC config --global --get-all diff 2>&1; then
	$GITEXEC config --global --remove-section diff; 2>&1
fi

if ! $GITEXEC config --global --get-all difftool 2>&1; then
	$GITEXEC config --global --remove-section difftool; 2>&1
fi

exit 0
