if [ -z "$REG_DIR" ]; then
	echo "ERROR: You cannot run tests by hand" >&2
	exit 2
fi

set -e

export PATH=$PWD/..:$PATH

export TZ="UTC"
export GIT_AUTHOR_DATE="2007-01-01 00:00:00 UTC"
export GIT_COMMITTER_DATE="2007-01-01 00:00:00 UTC"
export GIT_AUTHOR_NAME="Author Name"
export GIT_AUTHOR_EMAIL="author@email"
export GIT_COMMITTER_NAME="Commiter Name"
export GIT_COMMITTER_EMAIL="commiter@email"

# usage: die <msg>
function die
{
	echo "Error: $@" >&2
	exit 1
}

# usage: begin <msg>
function begin
{
	echo "% $1"
}

# usage: shouldfail <cmd>..
function shouldfail
{
	"$@" || return 0
	return 1
}

# usage: list_files
function list_files
{
	(
		find .git/patches -type f -printf "f " -exec sha1sum {} \;
		find .git/patches -type d -printf "d %p\n"
	) | sort
}

function setup_git_repo
{
	# set up the repo so we have something interesting to run guilt on
	echo "abc" > def
	git-add def
	git-commit -s -m "initial" 2> /dev/null > /dev/null
}

function setup_repo
{
	setup_git_repo

	mkdir -p .git/patches/master

	# patch to modify a file
	cp "$REG_DIR/data/modify.patch" .git/patches/master/modify

	# patch to add a new file
	cp "$REG_DIR/data/add.patch" .git/patches/master/add

	# patch to remove an existing file
	cp "$REG_DIR/data/remove.patch" .git/patches/master/remove

	# patch to change a mode
	cp "$REG_DIR/data/mode.patch" .git/patches/master/mode

	# the series file of all the things
	cp "$REG_DIR/data/series" .git/patches/master/series

	# the status file
	touch .git/patches/master/status

	# the status file
	touch .git/patches/master/status

	touch -d "$GIT_COMMITTER_DATE" .git/patches/master/modify
	touch -d "$GIT_COMMITTER_DATE" .git/patches/master/add
	touch -d "$GIT_COMMITTER_DATE" .git/patches/master/remove
	touch -d "$GIT_COMMITTER_DATE" .git/patches/master/mode
}

