#!/bin/sh -efu
#
# This script reads /var/lib/apt/lists/*_release files and
# returns the name of the latest available suite.
#
# Copyright (C) 2025  Paul Wolneykien.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

PROG="${0##*/}"
VERSION="0.1.2"

usage()
{
    [ "$1" = 0 ] || exec >&2
    cat <<EOF
Usage: $PROG [ -L | --label ] [ -d | --date ]

Options:

    -L, --label    use release label instead of suite name;
    -d, --date     order releases by the 'Date' tag;
    -a, --all      don't filter out task repositories;
    -h, --help     print this help and exit;
    -V, --version  print copyright and version info and exit.
EOF
    exit "${1:-0}"
}

TEMP="$(getopt -n "$PROG" -o dLhVa -l date,label,all,version,help -- "$@")" || usage 1
eval set -- "$TEMP"

mode='suite'
order='version'
all=
while :; do
    case "$1" in
	-L|--label)
	    mode='label'
	    ;;
	-d|--date)
	    order='date'
	    ;;
	-a|--all)
	    all=yes
	    ;;
        -h|--help)
	    usage 0
            ;;
	-V|--version)
	    cat <<EOF
$VERSION 2025
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
EOF
	    exit 0
	    ;;
        --)
	    shift
	    break
            ;;
        *)
	    message "$PROG: unrecognized option: $1" >&2
	    usage 1
            ;;
    esac
    shift
done

cmp_date() {
    local datetime=
    datetime="$(sed -n '/^Date:[[:space:]]*/ { s///p; q }' "$f")" ||:
    [ -n "$datetime" ] || return 0

    local timestamp=
    timestamp="$(date -d "$datetime" +%s)" || \
	echo "Unparseable date in $f." >&2
    [ -n "$timestamp" ] || return 0

    if [ -z "$2" ] || [ "$2" -le "$timestamp" ]
    then
	echo "$timestamp"
    fi
}

release_name() {
    case "$mode" in
	label)
	    sed -n '/^Label:[[:space:]]*/ { s///p; q }' "$1"
	    ;;
	*)
	    sed -n '/^Suite:[[:space:]]*/ { s///p; q }' "$1"
	    ;;
    esac
}

cmp_version() {
    local name=
    name="$(release_name "$1")" ||:
    [ -n "$name" ] || return 0
    if [ "$(rpmvercmp "$2" "$name")" = '-1' ]; then
	echo "$name"
    fi
}

max_release() {
    find /var/lib/apt/lists -name '*_release' | (
	max_f=
	max_order=
	name=
	while read -r f; do
	    name="$(release_name "$f")" ||:
	    if [ -z "$all" ]; then
		case "$name" in
		    task*)
			continue
			;;
		esac
	    fi

	    case "$order" in
		version)
		    _order="$(cmp_version "$f" "$max_order")" || exit $?
		    ;;
		date)
		    _order="$(cmp_date "$f" "$max_order")" || exit $?
		    ;;
		*)
		    echo "ERROR: Unexpected ordering method: $order" >&2
		    exit 1
		    ;;
	    esac

	    if [ -n "$_order" ]; then
		max_order="$_order"
		max_f="$f"
	    fi
	done

	[ -z "$max_f" ] || echo "$max_f"
    )
}

max="$(max_release)"

[ -n "$max" ] || exit 0

release_name "$max" | tr '[:upper:]' '[:lower:]'
