#!/bin/sh -e

first=$1
second=$2

if [ -z "$first" ]; then
	echo "Usage: compare <branch name A> [<branch name B>]"
	echo ""
	echo "The second argument defaults to the current branch if not specified"
	exit 1
fi

# Remember the branch we're on
currentbranch=`git symbolic-ref --short HEAD`

if [ -z "$second" ]; then
	second=$currentbranch
fi

firstdir=comparison/first
seconddir=comparison/second

# Make sure the output directory exists
mkdir -p comparison

# Remove any previous builds
rm -fr $firstdir
rm -fr $seconddir

export BREATHE_COMPARE=True

# Checkout the first target
echo git checkout $first
git checkout $first
# Run doxygen for this state
(cd ../examples/specific; make)
(cd ../examples/tinyxml; make)
(cd ../examples/doxygen; make)
# Clean current sphinx build directory
make clean
# Make sure the BUILDDIR variable can be overridden in the Makefile. Required for older commits
sed -i 's/BUILDDIR      = build/BUILDDIR     ?= build/g' Makefile
# Build into our first comparison directory
make html BUILDDIR=$firstdir || true
# Reset the Makefile to its state for this commit
git checkout Makefile


# Checkout the second target and repeat
echo git checkout $second
git checkout $second
(cd ../examples/specific; make)
(cd ../examples/tinyxml; make)
(cd ../examples/doxygen; make)
make clean
sed -i 's/BUILDDIR      = build/BUILDDIR     ?= build/g' Makefile
make html BUILDDIR=$seconddir || true
git checkout Makefile

# Return to our current branch
echo git checkout $currentbranch
git checkout $currentbranch

# Launch meld to compare the two html directories
echo meld $firstdir/html $seconddir/html
meld $firstdir/html $seconddir/html

