#!/bin/sh -ef

## usage: girar-nmu-helper-task-add-srpm [-t <task id>] path/to/srpm ...

TASKID=
BEFORE_TASK=
GITERYALTHOST=
GIRARALTHOST=girar
rpmsign=rpm-sign-gpg-agent

while getopts a:b:B:hH:ngrR:p:t: opt; do
	case "$opt" in
		a|t) TASKID="${OPTARG:?}"
			readonly TASKID ;;
		B) BEFORE_TASK="${OPTARG:?}"
			readonly BEFORE_TASK ;;
		n) rpmsign=rpm-sign-no-passphrase ;;
		g) rpmsign=rpm-sign-gpg-agent ;;
		r) rpmsign=rpm ;;
		H) GITERYALTHOST="${OPTARG:?}"
			readonly GITERYALTHOST ;;
		R) GIRARALTHOST="${OPTARG:?}"
			readonly GIRARALTHOST ;;
		h) pod2usage --exit=0 "$0"; exit 0 ;;
		*) pod2usage --exit=2 "$0"; exit 2 ;;
	esac
done

if [ -n "$BEFORE_TASK" ] && [ -z "$TASKID" ]; then
    echo "-B <before_subtask_id> require -t <taskid> option"
    exit 2
fi

shift "$((OPTIND-1))"

for srpm in "$@"; do
    $rpmsign --resign $srpm
    rsync -v -rlt --partial --stats -e ssh $srpm ${GIRARALTHOST}:
    ssh $GIRARALTHOST task add $TASKID $BEFORE_TASK srpm `basename $srpm`
done

: <<'__EOF__'

=head1	NAME

girar-nmu-task-helper-add-srpm - add given srpms to the default or given task.

=head1	SYNOPSIS

B<girar-nmu-task-helper-add-srpm>
[B<-h>] 
[B<-H> I<ssh gitery(git.alt) alias>]
[B<-R> I<ssh girar alias>]
[B<-t> I<task>]|[B<-a> I<task>]
[B<-B> I<before_subtask_id>]
[B<-n> | B<-g> | B<-r>]
I<srpm ...>

=head1	DESCRIPTION

B<girar-nmu-task-helper-add-srpm> 
add given src.rpms to the default or given task in prescribed order.

=head1	OPTIONS

=over

=item	B<-h>

Display this help and exit.

=item	B<-R> [I<girar ssh alias>]

By default, git.altlinux.org account should be configured as girar in ~/.ssh/config.
If you do not follow that convention, use -G <girar your ssh alias> option.

=item	B<-H> [I<gitery(git.alt) ssh alias>]

By default, gitery.altlinux.org account should be configured as git.alt in ~/.ssh/config.
If you do not follow that convention, use -H <git.alt your ssh alias> option.

=item  B<-n> | B<-g> | B<-r>

select a rpm sign command to sign a src.rpm.

=over

=item -g) rpm-sign-gpg-agent - use gpg agent to cache passphrase

=item -n) rpm-sign-no-passphrase - useful whan key has no passphrase.

=item -r) rpm use plain rpm.

=back

=item	B<-t> [I<task ID>] (also B<-a> [I<task ID>])

Id of task to add packages to; git.alt task [I<task ID>] add.

=item	[B<-B> I<before_subtask_id>]

I<before_subtask_id> is used in the extended format of girar-task add coomand:
girar-task add [<task_id> [<before_subtask_id>]] srpm <filename>

=back

=head1	AUTHOR

Written by Igor Vlasenko <viy@altlinux.org>.

=head1	COPYING

Copyright (c) 2010-2015 Igor Vlasenko, ALT Linux Team.

This 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.

=cut

__EOF__
