#!/bin/bash
#
# description=List hosts, tags, etc. used in the project.
#

set -euo pipefail

# Show usage information on stderr.
#
Usage_Exit() {
	{
		echo "ansible-boilerplate list <subcommand>"
		echo
		echo   "  hosts		Show all hosts defined in the inventory"
		echo   "  plays		Show all available playbooks"
		echo   "  tags		Show all tags used in the tasks"
		echo
	} >&2
	exit "${1:-1}"
}

Hosts() {
	[[ $# -eq 0 ]] || Usage_Exit

	./bin/aps --list-hosts \
		| sed -n -e 's/^      //p' \
		| sort -u
}

Plays() {
	[[ $# -eq 0 ]] || Usage_Exit
	./bin/ap --list
}

Tags() {
	[[ $# -eq 0 ]] || Usage_Exit

	(
		find ./ -maxdepth 2 -name 'site.yml' -print0 2>/dev/null || true;
		find ./play*/ -maxdepth 2 -name '*.yml' -a -not -name 'site.yml' 2>/dev/null | grep -Fv '/site/' | tr '\n' '\0' || true;
		find ./ansible_galaxy/ -maxdepth 4 -name 'play*' -type d 2>/dev/null | xargs -I _ find _ -maxdepth 1 -name '*.yml' -print0 2>/dev/null;
	) \
		| xargs -0 -I _ ./bin/ap _ --list-tags \
		| sed -En -e 's/.*TAGS: \[(.*)\].*/\1/gp' \
		| sed -e 's/, /\n/g' -e '/^$/d' \
		| sort -u
}

cmd="${1:-}"
[[ $# -gt 0 ]] && shift

case "${cmd}" in
	"hosts")
		Hosts "$@"
		;;
	"plays"|"playbooks")
		Plays "$@"
		;;
	"tags")
		Tags "$@"
		;;
	"help"|"--help")
		Usage_Exit 0
		;;
	*)
		Usage_Exit
esac
exit $?
