#!/bin/bash
#
# description=Generate files from existing data or templates
#

set -euo pipefail

unset FORCE

# Show usage information on stderr.
Usage_Exit() {
	{
		echo "ansible-boilerplate generate [--force|-f] <subcommand>"
		echo
		echo   "  makefile	Create a toplevel Makefile for this project"
		echo   "  readme <role>	Create a README.md file for a role"
		echo
		echo
	} >&2
	exit "${1:-1}"
}

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

	if [[ -e Makefile && -z ${FORCE-} ]]; then
		echo "A 'Makefile' already exists. Echoing what needs to be added:"
		echo "---"
		local makefile_f="/dev/stdout"
	else
		echo 'Creating "Makefile" ...'
		local makefile_f="Makefile"
	fi

	cat >"${makefile_f}" <<-EOF
		#
		# Makefile
		#

		# Make sure that the Ansible Boilerplate Collection project is set up (its
		# files are "available"), and install it if not!
		_ABC_ := \$(shell test -e ansible_galaxy/ansible_collections/alexbarton/boilerplate/Makefile.boilerplate || bin/abc install >&2)

		# Include the Ansible Boilerplate Collection Makefile fragment:
		include ansible_galaxy/ansible_collections/alexbarton/boilerplate/Makefile.boilerplate

		# Add local additions below ...
	EOF

	[[ "${makefile_f}" = "Makefile" ]] || echo "---"
}

Readme() {
	[[ $# -eq 1 ]] || Usage_Exit

	local roles_d
	[[ -d "roles" ]] && roles_d="roles/$1" || roles_d="./$1"

	local readme_file="${roles_d}/README.md"

	if [[ ! -e ${roles_d} ]]; then
		echo "Role \"$1\" not found in \"${roles_d}\"! Aborting." >&2
		exit 1
	fi

	if [[ -e ${readme_file} && -z ${FORCE-} ]]; then
		echo "The \"${readme_file}\" file already exists! Aborting." >&2
		echo 'You can use "--force" to override this check.' >&2
		exit 1
	fi

	cat >"${readme_file}" <<-EOF
		# $1

		...

		## Role Variables

	EOF

	# Detect role variables.
	if grep -Ec '^[a-z].*:' "${roles_d}/defaults/"*.yml >/dev/null 2>&1; then
		# Some role variables found.
		grep -E '^[a-z].*:' "${roles_d}/defaults/"*.yml |
			cut -d: -f1 | sort -u | while read -r var
		do
			echo "- \`${var}\`:" >>"${readme_file}"
		done
	else
		# No role variables found.
		echo "None." >>"${readme_file}"
	fi

	cat >>"${readme_file}" <<-EOF

		## License

		MIT

		## Author Information
	EOF

	printf "\nCopyright (C) %s %s\n" \
		"$(date +%Y || true)" "${NAME:-${LOGNAME}} <${EMAIL:-${LOGNAME}@...}>." \
		>>"${roles_d}/README.md"

	echo "New \"${readme_file}\" created."
	exit 0
}

if [[ ${1-} == "--force" || ${1-} == "-f" ]]; then
	FORCE=1
	shift
fi

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

case "${cmd}" in
"makefile")
	Makefile "$@"
	;;
"readme")
	Readme "$@"
	;;
"help" | "--help")
	Usage_Exit 0
	;;
*)
	Usage_Exit
	;;
esac
