#!/bin/sh

# always capture the current branch so we can go back to it :)
current_branch="$(git branch --show-current)"

_default() {
	if [ -n "$1" ]; then
		git checkout -q "$1"
	fi
	branch_name="$(git branch --show-current)"
	update_list=""
	for p in $(common/travis/changed_templates.sh 2>&1 >/dev/null); do
		if [ -z "$update_list" ]; then
			update_list="$p"
		else
			appendee="$p"
			if [ -n "$appendee" ]; then
				# yay explicit newline :(
				update_list="${update_list}
${appendee}"
			fi
		fi
	done
	if [ -n "$update_list" ]; then
		printf "branch: %s\n%s\n\n" "$branch_name" "$update_list"
	fi
}

_maint() {
	git checkout -q master
	for b in $(git branch --list 'maint*'); do
		_default "$b"
	done
}

_update() {
	git checkout -q master
	for b in $(git branch --list 'update*'); do
		_default "$b"
	done
}

if [ $# = 0 ]; then
	_default
else
	while test $# != 0
	do
		case "$1" in
			-m|--maint) _maint;;
			-u|--update) _update;;
			--) shift; break;;
			*)  _default;;
		esac
		shift
	done
fi

# return to branch we were on before running script
git checkout "$current_branch" >/dev/null 2>&1