#!/bin/sh -efu

# Copyright (C) 2025 Paul Wolneykien <manowar@altlinux.org>
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

PROG=${0##*/}
VERSION='0.4.2'

usage()
{
    [ "$1" = 0 ] || exec >&2
    cat <<EOF
Usage: $PROG [ options ] [ *.chksum ]

Options:

  -o OUTPUT, --out=OUTPUT    write the resulting checksums to the given
     	     		     file OUTPUT (the default is stdout);

  -f, --force    overwrite existing checksum files;

  -v, --verbose    be verbose;

  -V, --version    print program version and exit;

  -h, --help    show this text and exit.

Report bugs to https://bugzilla.altlinux.org/.
EOF
    exit "${1:-0}"
}

TEMP="$(getopt -n "$PROG" -o o:fvVh -l out:,force,verbose,version,help -- "$@")" || usage 1
eval set -- "$TEMP"

output=
force=
verbose=
while :; do
    case "$1" in
	-o|--output)
	    shift
	    output="$1"
	    ;;
	-f|--force)
	    force=y
	    ;;
	-v|--verbose)
	    verbose=y
	    ;;
        -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
            ;;
        *)
	    echo "$PROG: unrecognized option: $1" >&2
	    usage 1
            ;;
    esac
    shift
done

if [ $# -eq 0 ]; then
    [ -z "$verbose" ] || echo "Reading checksum data from stdin..." >&2
fi

if [ -n "$output" ] && [ -e "$output" ]; then
    if [ -z "$force" ]; then
	echo "ERROR: The specified output checksum file $output already exists!" >&2
	echo "Use -f option to overwrite it." >&2
	exit 1
    fi
fi

workdir=
cleanup()
{
    if [ "${DEBUG:-0}" -eq 0 ]; then
	[ -z "$workdir" ] || rm -rf "$workdir"
    else
	echo "DEBUG: Workdir: $workdir" >&2
    fi
}
trap 'cleanup' EXIT
workdir="$(mktemp -d --tmpdir "$PROG.XXXX")"

cat <<EOF >"$workdir"/dedup.awk
#QF='%{name}-%{version}-%{release}.%{arch}.rpm/%{disttag}@%{buildtime}\n'
/^[^#[:space:]]+\\.rpm\\/[^@/]+@[0-9]+\$/ {
    skip_print = pkgs[\$0]
    pkgs[\$0] = 1
    if (!skip_print) {
        print \$0
    }
    while (getline > 0) {
        if (!skip_print) {
            print \$0
        }
        if (\$0 ~ /^\$/) {
            next
        }
    }
    print("Unexpected EOF or error:", ERRNO) > "/dev/stderr"
    exit 1
}
EOF

if [ -n "$output" ]; then
    awk -f "$workdir"/dedup.awk "$@" >"$output"
else
    awk -f "$workdir"/dedup.awk "$@"
fi
