#!/usr/bin/env bash
#
# A script which checks that an appropriate news file has been added on this
# branch.

pr="$PULL_REQUEST_NUMBER"

echo -e "+++ \033[32mChecking newsfragments for PR #$pr\033[m"


failed=0
matched=0

# assume that anything in the changelogs directory which starts with a number
# is intended as a newsfile.
#
# (we use the < <(...) syntax rather than a regular pipe to avoid spawning a
# subshell for the while loop, which would mean that changes to shell variables
# are not propagated)
#
while read f; do
    basename=$(basename $f)
    dirname=$(dirname $f)
    extension="${f##*.}"

    # check that each changelog file has a known extension
    if ! [[ "$extension" == "new" || "$extension" == "feature" || "$extension" == "clarification" || "$extension" == "breaking" || "$extension" == "deprecation" ]]; then
        echo -e "\e[31mERROR: newsfragment $f does not have one of the required file extensions for a changelog entry: .new, .feature, .clarification, .breaking, .deprecation\e[39m" >&2
    fi

    # check that there is nothing that looks like a newsfile outside one of the
    # expected directories.
    if [[ "$dirname" != changelogs/*/newsfragments ]]; then
        echo -e "\e[31mERROR: newsfragment $f appears to be in the wrong directory: should be in changelogs/*/newsfragments\e[39m" >&2
        failed=1
    else
        # see if this newsfile corresponds to the right PR
        [[ -n "$pr" && "$basename" == "$pr".* ]] && matched=1
    fi
# find all files in the 'changelogs/*/' dirs that are in the form `<digits>.<text>`
done < <(find changelogs -regex '.*/[0-9]+\.[^/]+$')

if [[ -n "$pr" && "$matched" -eq 0 ]]; then
    echo -e "\e[31mERROR: Did not find a news fragment with the right number: expected changelogs/*/newsfragments/$pr.*\e[39m" >&2
    failed=1
fi

exit $failed
