#!/bin/sh

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

[ -z "$gitbranch" ] && gitbranch="$repobranch"

gear_pkg_name=`gear --command sh -- -c 'printf %s "$gear_pkg_name"'`
if [ -z "$gear_pkg_name" ]; then 
    echo "gear_pkg_name not found: not a gear repository!"
    exit 2;
fi
name=$gear_pkg_name
commit=`git ls-remote -h git://git.altlinux.org/gears/${name:0:1}/${name}.git refs/heads/$repobranch | awk '{print $1}'`
if [ -z "$commit" ]; then
    echo "Error:$name is not a git-built package for $repobranch. commit not found."
    exit 1
fi

git fetch "git://git.altlinux.org/gears/${name:0:1}/${name}.git" refs/heads/$repobranch:$gitbranch

: <<'__EOF__'

=head1	NAME

girar-fetch-build-commit - fetch last build commit from the git.alt:/gears/ repository

=head1	SYNOPSIS

B<girar-fetch-build-commit>
[B<-h>] 
[B<-b> I<repository>]
[B<-d> I<local destination branch>]
I<name>

=head1	DESCRIPTION

B<girar-fetch-build-commit> fetch last build commit from the git.alt:/gears/ repository
into the local destination branch (defauilt is repository name, sisyphus).

=head1	OPTIONS

=over

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

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 <repository name>.

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