#!/bin/sh -e

. gear-sh-functions

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

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

Usage: $PROG -a <packager> <spec>
EOF
	[ -n "$1" ] && exit "$1" || exit
}

[ -n "$*" ] || USAGE

if [ "$1" = "-r" ]; then
    shift; shift
fi
if [ "$1" != "-a" ]; then
	fatal 'argument packager'
fi
shift
p="$1"; shift
spec="$1"; shift

spec_serial=
spec_epoch=
get_serial_from_spec()
{
	local spec="$1" && shift

	spec_serial="$(sed '/^serial:[[:space:]]*/I!d;s///;q' "$spec" |sed 's/[[:space:]]\+$//')"
	spec_epoch="$(sed '/^epoch:[[:space:]]*/I!d;s///;q' "$spec" |sed 's/[[:space:]]\+$//')"

	expand_rpm_macros "$spec" spec_serial
	expand_rpm_macros "$spec" spec_epoch
}

get_NVR_from_spec "$spec"
get_serial_from_spec "$spec"

n="$spec_version-$spec_release"
if [ -n "$spec_serial" ]; then
	n="$spec_serial:$spec_version-$spec_release"
elif [ -n "$spec_epoch" ]; then
	n="$spec_epoch:$spec_version-$spec_release"
fi

d=`LC_TIME=C date '+%a %b %d %Y'`

printf '* %s %s %s\n' "$d" "$p" "$n"
