#!/bin/sh -ef

GITERYALTHOST=git://git.altlinux.org
GIRARALTHOST=girar
SUBTASK=100

while getopts hH:R:s:t: opt; do
	case "$opt" in
		t) TASK="${OPTARG:?}"
			readonly TASK ;;
		s) SUBTASK="${OPTARG:?}"
			readonly SUBTASK ;;
		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
shift "$((OPTIND-1))"

[ -n "$1" ] && TASK=$1
[ -n "$2" ] && SUBTASK=$2
DIR=unknown.git
dirvar=`ssh $GIRARALTHOST task show $TASK | grep '^ *'$SUBTASK':dir='`
if [ -z "dirvar" ]; then
    echo "Oops: $GITERYALTHOST/tasks/$TASK/gears/$SUBTASK/git not found"
    exit 1
fi
DIR=`basename $dirvar`
		    
git clone $GITERYALTHOST/tasks/$TASK/gears/$SUBTASK/git $DIR


: <<'__EOF__'

=head1	NAME

girar-clone-task-git - clone a git from a girar task

=head1	SYNOPSIS

B<girar-nmu-helper-task-for-each-srpm>
[B<-h>] 
[B<-H> I<ssh gitery(git.alt) alias>]
[B<-R> I<ssh girar alias>]
[B<-t> task]
[B<-s> subtask]
[<task> [<subtask>]]

=head1	DESCRIPTION

B<girar-clone-task-git> [<task> [<subtask>]] -
clone a git from a girar task. Default subtask is 100.

=head1	OPTIONS

=over

=item	B<-t> I<task>

Task number. Mandatory. By default, the first arg is considered a task number.

=item	B<-s> I<subtask>

Subtask number.
Default is 100.

=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<-h>

Display this help and exit.

=back

=head1	AUTHOR

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

=head1	COPYING

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