#!/bin/sh
#============================================================================#
# Find and clone repo from git.alt                                           #
#============================================================================#
# (C) Denis Smirnov <mithraen@freesource.info>          http://mithraen.ru/  #
#============================================================================#

export LC_ALL=C
repobranch="sisyphus"
gitbranch=master
force_rewrite=
localgitdir=
clone_options="-o girar"
while getopts b:d:fhn:o: opt; do
	case "$opt" in
		b) repobranch="${OPTARG:?}"
			readonly repobranch ;;
		d) gitbranch="${OPTARG:?}" 
			readonly gitbranch ;;
		n) localgitdir="${OPTARG:?}" 
			readonly localgitdir ;;
		f) force_rewrite=-f
			readonly force_rewrite ;;
		o) clone_options="-o ${OPTARG:?}"
			readonly clone_options ;;
		h) pod2usage --exit=0 "$0"; exit 0 ;;
		*) pod2usage --exit=2 "$0"; exit 2 ;;
	esac
done
shift "$((OPTIND-1))"

name="$1"
if [ -z "$name" ]; then 
    pod2usage --exit=2 "$0"; exit 2;
fi
[ -z "$localgitdir" ] && localgitdir="$2"
[ -z "$localgitdir" ] && localgitdir="$name.git"

girar-clone-build-commit \ 
    $clone_options $force_rewrite \
    ${repobranch:+-b $repobranch} \
    ${gitbranch:+-d $gitbranch} \
    "$name" "$localgitdir"

pushd  "$localgitdir" || exit -1
git --git-dir=".git" config 'remote.origin.url' "git.alt:packages/$name"
git --git-dir=".git" config 'remote.origin.push' "refs/heads/*:refs/heads/*"
git --git-dir=".git" config 'remote.origin.fetch' "refs/heads/*:refs/remotes/origin/heads/*"
gear-rules-restore-branches

: <<'__EOF__'

=head1	NAME

girar-nmu-helper-clone-and-setup-build-commit - clone and setup git.alt:/gears/ repository

=head1	SYNOPSIS

B<girar-nmu-helper-clone-and-setup-build-commit>
[B<-h>] 
[B<-f>] 
[B<-b> I<repository>]
[B<-d> I<local destination branch>]
[B<-n> I<local repository name>]
[B<-o> I<name>]
I<name> [I<local repository name>]

=head1	DESCRIPTION

B<girar-nmu-helper-clone-and-setup-build-commit> clone git.alt:/gears/../name.git repository.
place last build commit into local destination branch. sets up remote.origin.*
and calls gear-rules-restore-branches

=head1	OPTIONS

=over

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

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

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

Name of the checked out branch. Default is master.

=item	B<-f>

Forcefully remove previous <name>.git and clone over even if <name>.git already exists.

=item	B<-h>

Display this help and exit.

=item	B<-n> I<local repository name>

Name of the local repository. Default is I<name.git>.
Can also be specified as the second argument.

=item	B<-o> I<remote name>

Option passed to git-clone.
Instead of using the remote name origin to keep
track of the upstream repository, use I<remote name>.

=back

=head1	AUTHOR

Written by Denis Smirnov <mithraen@freesource.info>,
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__
