#!/bin/sh
# 2006, 2008, 2013, 2016 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
# 
# Обновляет версию пакета, получает исходники,
# собирает, устанавливает на тестирование и выкладывает новую версию пакета.
# версия может быть указана как 2.6.1 (полная), 2.6 (major) или .1 (minor)

# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/functions/common
load_mod spec git

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
	echog "rpmrb (Re Build) - update spec to vermajor.verminor version, build %rel release"
	echog "Usage: rpmrb [spec] [vermajor][.verminor] [rel]"
	echog "Example:"
	echog "          $ rpmrb [name.spec] 22.6 alt2"
	echog "          $ rpmrb 1.2.3"
	exit 0
fi

parse_r()
{
	VER=$1

	case "$2" in
		-*)
			fatal "No options are supported. See --help."
			;;
		*)
	esac

	# need for rpmgs
	GSSETRELEASE=$2
}

set_girar_host $1 && shift

parse_cmd_pre_spec "$@"

parse_r $LISTARGS

# assure we have only specs or src.rpm
LISTNAMES=$(repodirs_to_specs $LISTNAMES)

#[ -s "$SPEC" ] || fatal "Can't find spec file. Run $0 --help for help."

for SPEC in $LISTNAMES ; do
	set_specdir $SPEC
	docmd cd $SPECDIR || fatal

	test -n "$GSSETRELEASE" || GSSETRELEASE=$(get_txtrelease $SPEC)1
	export GSSETRELEASE

	docmd $ETERBUILDBIN/rpmgs $SPEC $VER || fatal "Error within rpmgs"
	add_changelog_helper "- new version $(get_version $SPEC) (with rpmrb script)" $SPEC || echog "Changelog entry already exists"

	if is_gear ; then
		#git diff $SPEC
		git_commit_ignore_nothing $SPEC -m "update spec to new build $(get_version $SPEC) (with rpmrb script)" || exit 1
	fi

	cd - >/dev/null
done

docmd $ETERBUILDBIN/rpmbsh $GIRARHOST -i -u $LISTNAMES
