#!/usr/bin/env sh
# @file ansible_generate_readme_role.sh
# @description
#
# This script generates a README.md file for Ansible roles. Content is fetched e.g. from
# the information in `meta/main.yml` and the default variables in `default/main.yml`.
#
# By default, the current working directory is used as the source. Via parameters, one or
# more different directories can be specified. Shell globs are supported.
#
# The software ansible-doctor is used for generation. More information can be found here:
#
# - [Software](https://github.com/thegeeklab/ansible-doctor)
# - [Documentation](https://ansible-doctor.geekdocs.de)
# - [DockerHub](https://hub.docker.com/r/thegeeklab/ansible-doctor)
#

set -eu

# shellcheck disable=2068
for i in ${@:-.}; do
  ROLE_DIR="$(readlink -f "$i")"
  ROLE_NAME="Ansible role '$(basename "${ROLE_DIR}" | sed "s/ansible-role-//g")'"
  echo "Generate README.md for ${ROLE_NAME}"
  docker run --rm \
      -e ANSIBLE_DOCTOR_LOGGING__LEVEL=error \
      -e ANSIBLE_DOCTOR_RENDERER__AUTOTRIM=False \
      -e ANSIBLE_DOCTOR_RENDERER__FORCE_OVERWRITE=True \
      -e ANSIBLE_DOCTOR_ROLE__NAME="${ROLE_NAME}" \
      -e PY_COLORS=0 \
      -v "${ROLE_DIR}":/doctor \
      -w /doctor \
      thegeeklab/ansible-doctor:8.1.0
done
