#!/bin/sh

. shell-quote
. gear-sh-functions

RPM=rpm
RPMARG=
FORMAT=

USAGE()
{
        [ "$1" = 0 ] || exec >&2
        cat <<EOF
ubt-stampspec - generates timestamp for rpm specfile changelog entry.

ubt-stampspec is free software, covered by the GNU General Public License.
ubt-stampspec comes with ABSOLUTELY NO WARRANTY, see license for details.

Usage: $PROG [options] <specfile> [ <specfile>]*

Valid options are:
-r RPM, --rpm=RPM                       RPM executable;
-a args, --args=args                    unused;
-f format, --format=format              unused;
-h, --help                              show this text.

Configured RPM executable is $RPM.
EOF
        [ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o a:f:r:h -l args:,format:,rpm:,help -- "$@"` || USAGE
eval set -- "$TEMP"

while :; do
        case "$1" in
                -a|--args) shift; RPMARG="$1"; shift
                        ;;
                -f|--format) shift; FORMAT="$1"; shift
                        ;;
                -r|--rpm) shift; RPM="$1"; shift
                        ;;
                -h|--help) USAGE 0
                        ;;
                --) shift; break
                        ;;
                *) echo "$PROG: unrecognized option: $1" >&2; exit 1
                        ;;
        esac
done

[ -n "$*" ] || USAGE

packager="$(eval "\"$RPM\"" --eval %packager)"

for spec; do
        quote_shell_variable spec "$spec"
        get_NVR_from_spec "$spec"
        spec_release=${spec_release//%/%%}
        "$RPM" -q --qf "* `LC_TIME=C date '+%a %b %d %Y'` $packager %|SERIAL?{%{SERIAL}:}|%{VERSION}-${spec_release}\n" --specfile "$spec" | head -n1
done
