#!/bin/sh
# commands used:
# - column
# - git

init_variables() {
  COMMAND=${0##*/}

  TAGS='BUG'              # known bug that we live with
  TAGS="${TAGS}:IDEA"     # idea for improvement
  TAGS="${TAGS}:FIXME"    # should be refactored
  TAGS="${TAGS}:HACK"     # workaround around a bug or limitation
  TAGS="${TAGS}:OPTIMIZE" # should be optimized
  TAGS="${TAGS}:TODO"     # something else needs to be done
}

show_usage() {
  cat <<- EOF
	usage: ${COMMAND} [<options>]

	Report all code tags present in config files

	Options:
	  -h    show this message only
	EOF
}

parse_command_line() {
  while getopts h OPT; do
    case "${OPT}" in
      h) show_usage; exit 0 ;;
      ?) exit_error ;;
    esac
  done

  shift "$((OPTIND - 1))"
}

# shellcheck disable=SC2120
exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  echo "Try '${COMMAND} -h' for more information."
  exit 1
} >&2

report_tags() {
  git grep -E "#($( echo "${TAGS}" | tr ':' '|' )):" . \
    | sed 's|\(.*\)/\([^:]*\):.*\(#[A-Z]*\): \(.*\)|\3@\1@\2@\4|' \
    | sort \
    | column -t -s@
}

init_variables
parse_command_line "$@"

report_tags
