#!/usr/bin/env bash
#
# Forbid scripts containing a un underscore in their filename,
# (except where this is required, for example Python modules).
#
# This is a just a matter of style and taste; there are no correctness
# implications.
#
# Exceptions are supported, by passing !<FILENAME> or !<GLOB> as argument(s).

set -euo pipefail

# this include stanza is automatically maintained by update-shell-includes
common_dir=$(realpath "$0")
common_dir=$(dirname "$common_dir")
# shellcheck source=maint/common/bash-utils.sh
. "$common_dir"/bash-utils.sh

reject_options

exceptions=()
for arg in "$@"; do
    case "$arg" in
	!*) exceptions+=("${arg#!}") ;;
	*)
	    fail 'non-option arguments msut be !FILE to allow FILE as an exception'
	    ;;
    esac
done

shopt -s nullglob

wrong=()

# git shell escapes things by default, see git-ls-files(1) and git-config(1) core.quotepath
files=$(git ls-files ':maint/*_*')

for f in $files; do
    case "$f" in
	*.py) ;;
	*)
	    excepted=false
	    for exception in "${exceptions[@]}"; do
		# shellcheck disable=SC2254
		case "$f" in
		    $exception) excepted=true ;;
		esac
	    done
	    if ! $excepted; then
		wrong+=("$f")
	    fi
	    ;;
    esac
done

if [ "${wrong[*]}" != '' ]; then
    fail "We would like script names to have hyphens, not underscores

Please rename these files:
  ${wrong[*]}
"
fi
