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

if ! which bzr > /dev/null; then
	echo "Could not locate Bazaar. Is it installed on this system?" >&2
	exit 201
fi

if ! bzr alias ksdiff="diff --using=ksdiff" 2>&1; then
	echo "Could not set the ksdiff alias" >&2
	exit 2
fi

if ! bzr config --scope=bazaar bzr.mergetool.Kaleidoscope="ksdiff --merge --output \"{result}\" --base \"{base}\" -- \"{this}\" \"{other}\"" 2>&1; then
	echo "Could not create a Kaleidoscope merge tool" >&2
	exit 3
fi

if [[ `bzr config bzr.default_mergetool --scope=bazaar` == 'Kaleidoscope' ]]; then
	if ! bzr config --scope=bazaar --remove bzr.default_mergetool 2>&1; then
		echo "Could not remove Kaleidoscope as a default merge tool" >&2
		exit 4
	fi
fi
