#!/bin/sh -e

TASKID=
GITALTHOST=
repobranch=
OUTGITS=OUT.gits
OUTSRPMS=OUT.SRPMS
SRPMOPTS=
GITOPTS=
while getopts b:d:gG:hH:nrS:t: opt; do
	case "$opt" in
		G) OUTGITS="${OPTARG:?}" 
			 ;;
		S) OUTSRPMS="${OPTARG:?}"
			 ;;
		t) TASKID="${OPTARG:?}"
			readonly TASKID ;;
		b) repobranch="${OPTARG:?}"
			readonly repobranch ;;
		n) SRPMOPTS="$SRPMOPTS -n" ;;
		g) SRPMOPTS="$SRPMOPTS -g" ;;
		r) SRPMOPTS="$SRPMOPTS -r" ;;
		d) GITOPTS="$GITOPTS -d ${OPTARG:?}" ;;
		p) GITOPTS="$GITOPTS -p ${OPTARG:?}" ;;
		H) GITALTHOST="${OPTARG:?}"
			readonly GITALTHOST ;;
		h) pod2usage --exit=0 "$0"; exit 0 ;;
		*) pod2usage --exit=2 "$0"; exit 2 ;;
	esac
done
shift "$((OPTIND-1))"

OPTSMISC=
[ -z "$TASKID" ] || OPTSMISC="$OPTSMISC -t $TASKID"
[ -n "$GITALTHOST" ] && OPTSMISC="$OPTSMISC -H $GITALTHOST"
[ -n "$repobranch" ] && OPTSMISC="$OPTSMISC -b $repobranch"


for name in "$@"; do
    if [ -d "$OUTGITS/$name.git" ]; then
	girar-nmu-helper-task-add-git $GITOPTS $OPTSMISC "$OUTGITS/$name.git"
    elif [ -d "$OUTSRPMS/$name" ]; then
	girar-nmu-helper-task-add-srpm $SRPMOPTS $OPTSMISC "$OUTSRPMS/$name/"*.src.rpm
    else
	echo "ERROR: NMU package not found for $name. run girar-nmu-prepare first."
	exit 3;
    fi
done


: <<'__EOF__'

=head1	NAME

girar-nmu-task-add - add NMU generated packages to the given task in prescribed order.

=head1	SYNOPSIS

B<girar-nmu-task-add>
[B<-h>] 
[B<-b> I<repository>]
[B<-d> I<local branch>]
[B<-H> I<ssh git.alt alias>]
[B<-G> I<path/to/OUT.gits dir>]
[B<-S> I<path/to/OUT.SRPMS dir>]
[B<-n> | B<-g> | B<-r>]
[B<-p> I<prefix>]
[B<-t> I<task>]
I<name ...>

=head1	DESCRIPTION

B<girar-nmu-task-add> 
add NMU generated packages to the given task in prescribed order.
It takes a list of names as an argument. 

=head1	OPTIONS

=over

=item	B<-b> I<repository name>

Name of the repository branch. Values are: sisyphus|5.1|p5|..
Default is sisyphus.

=item	B<-d> I<local branch name>

Git repository option.
Name of the local git branch to build from. Default is nmu.

=item	B<-p> I<prefix>

Git repository option.
Prefix to add before git repository name.
Useful to distinguish among automated and own repositories.
By default prefix is "00-tmp-".

=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<-G> I<dir>

name of output directory with Git repositories.
Default is "OUT.gits".

=item	B<-S> I<dir>

name of output directory with src.rpms.
Default is "OUT.SRPMS".

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

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

=item	B<-t> [I<task ID>]

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

=item	B<-h>

Display this help and exit.

=back

=head1	AUTHOR

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

=head1	COPYING

Copyright (c) 2010 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__
