#! /bin/sh
# check-release: check that the source tree is ready to be released.
#
# Usage: check-release [-c] [-u] [-t TAG] [-d DIR] [-y YEAR]
#
#   -c      also check the Flex Project copyright years: COPYING,
#           doc/flex.texi and src/FlexLexer.h.in must give the same
#           range, and it must end in the year of the release (the year of the NEWS date, or with
#           -u the current year)
#   -d DIR  the top of the source tree, default the current directory
#   -t TAG  also check that TAG is v followed by the version
#   -y YEAR the year the release will be made in, for -c
#   -u      check the tree before the release commit is made: the top
#           section of NEWS must still have the placeholder header, and
#           have entries
#
# Without -u, the top section of NEWS must be the release: a real version
# and date in its header, the same version as AC_INIT in configure.ac, a
# date that is not in the future, and at least one entry.
#
# Prints each problem found on standard error. Exits 0 if there are none,
# 1 if there are.

set -eu

dir=.
tag=
unreleased=0
copyright=0
year=

while getopts :cd:t:uy:h OPTION; do
    case $OPTION in
        c) copyright=1 ;;
        d) dir=$OPTARG ;;
        t) tag=$OPTARG ;;
        u) unreleased=1 ;;
        y) year=$OPTARG ;;
        h) sed -n '2,/^$/p' "$0" | sed -e 's/^# \{0,1\}//'
           exit 0 ;;
        *) echo "Usage: $0 [-c] [-u] [-t TAG] [-d DIR] [-y YEAR]" >&2
           exit 1 ;;
    esac
done

problems=0
problem() {
    echo "check-release: $*" >&2
    problems=1
}

news=$dir/NEWS
header=$(grep -m 1 '^\* Noteworthy changes in release' "$news" || true)

if [ -z "$header" ]; then
    echo "check-release: no release section found in $news" >&2
    exit 1
fi

# The entries in the top section: the lines that start a "***" item
# between the first header and the next one.
entries=$(awk '
    /^\* Noteworthy changes in release/ { if (seen) exit; seen = 1; next }
    seen && /^\*\*\* / { n++ }
    END { print n + 0 }' "$news")

if [ "$entries" -eq 0 ]; then
    problem "the top section of NEWS has no entries"
fi

# The Flex Project years in FILE: the text between "Copyright" and
# "The Flex Project", with the line breaks and the (c) or @copyright{}
# mark removed.
flex_years() {
    tr '\n' ' ' <"$1" |
        grep -oE 'Copyright (\(c\)|@copyright\{\}) [0-9, -]+ The Flex Project' |
        head -n 1 | sed -E 's/^Copyright [^0-9]*//; s/ The Flex Project$//; s/ +$//'
}

# check_copyright YEAR: COPYING, the manual and the installed header
# agree, and end in YEAR.
check_copyright() {
    c=$(flex_years "$dir/COPYING" || true)
    m=$(flex_years "$dir/doc/flex.texi" || true)
    h=$(flex_years "$dir/src/FlexLexer.h.in" || true)
    if [ -z "$c" ]; then
        problem "no Flex Project copyright years found in COPYING"
    elif [ "$c" != "$m" ]; then
        problem "COPYING says ${c}, but doc/flex.texi says ${m:-nothing}"
    elif [ "$c" != "$h" ]; then
        problem "COPYING says ${c}, but src/FlexLexer.h.in says ${h:-nothing}"
    elif [ "${c##*[!0-9]}" != "$1" ]; then
        problem "the Flex Project copyright years in COPYING, $c, do not end in $1"
    fi
}

placeholder='^\* Noteworthy changes in release +\?\.\? \(\?\?\?\?-\?\?-\?\?\) \[\?\]$'

if [ "$unreleased" -eq 1 ]; then
    if ! printf '%s\n' "$header" | grep -Eq "$placeholder"; then
        problem "the top section of NEWS should have the placeholder header, but has: $header"
    fi
    if [ "$copyright" -eq 1 ]; then
        check_copyright "${year:-$(date -u +%Y)}"
    fi
    exit $problems
fi

pattern='^\* Noteworthy changes in release +([0-9]+\.[0-9]+\.[0-9]+(-rc[0-9]+)?) \(([0-9]{4}-[0-9]{2}-[0-9]{2})\) \[[a-z-]+\]$'

if ! printf '%s\n' "$header" | grep -Eq "$pattern"; then
    problem "the top section of NEWS does not have a release header with a version and date: $header"
    exit 1
fi

version=$(printf '%s\n' "$header" | sed -E "s/$pattern/\\1/")
date=$(printf '%s\n' "$header" | sed -E "s/$pattern/\\3/")

today=$(date -u +%Y-%m-%d)
latest=$(printf '%s\n%s\n' "$date" "$today" | sort | tail -n 1)
if [ "$latest" != "$today" ]; then
    problem "the date in the NEWS header, $date, is in the future"
fi

configured=$(sed -n 's/^AC_INIT(\[[^]]*\],\[\([^]]*\)\].*/\1/p' "$dir/configure.ac")
if [ "$configured" != "$version" ]; then
    problem "NEWS says $version, but AC_INIT in configure.ac says ${configured:-nothing}"
fi

if [ "$copyright" -eq 1 ]; then
    check_copyright "${year:-${date%%-*}}"
fi

if [ -n "$tag" ] && [ "$tag" != "v$version" ]; then
    problem "the tag is $tag, but the version is $version"
fi

exit $problems
