#!/bin/sh
# 2006, 2008, 2013 (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

set_girar_host $1 && shift

SPEC=$1
# allow run with spec file (obsoleted)
[ -r "$SPEC" ] && shift

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

VER=$1

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

# need for rpmgs
GSSETRELEASE=$2
test -n "$GSSETRELEASE" || GSSETRELEASE=$(get_txtrelease $SPEC)1
export GSSETRELEASE

docmd $ETERBUILDBIN/rpmgs $SPEC $VER || fatal "Error with get source"
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

showcmd $ETERBUILDBIN/rpmbsh -i $SPEC
$ETERBUILDBIN/rpmbsh -i $SPEC || fatal "Error with build in hasher"
docmd $ETERBUILDBIN/rpmbs $GIRARHOST -u $SPEC || fatal "Error with upload"
