#!/bin/sh -e

gitbranch=
verbose=
quiet=
while :; do
	case "$1" in
	    "") break
		;;
	    --) shift; break
		;;
	    -d|--gitbranch) shift; gitbranch="$1"
		;;
	    -h|--help) pod2usage --exit=0 "$0"; exit 0
		;;
	    -q|--quiet) quiet=-q; verbose=
		;;
	    -v|--verbose) verbose=-v; quiet=
		;;
	    *) pod2usage --exit=2 "$0"; exit 2
		;;
	esac
	shift
done

[ -n "$verbose" ] && set $verbose

if ! [ -d .git ]; then
    echo `pwd`" is not a .git repository"
    exit 2
fi

eval `hsh --printenv "$@"`
hasher_priv_getconf_args=
[ -n "$number" ] && hasher_priv_getconf_args="-$number"

hasher_priv_conf=`/usr/libexec/hasher-priv/getconf.sh $hasher_priv_getconf_args`
eval `grep '^user2=' "$hasher_priv_conf"`
gid2=`id -g $user2`

hsh --initroot-only "$@"
cronbuild_requires=
[ -e ./.gear/cronbuild-options ] && . ./.gear/cronbuild-options ||:
# skip git-arch git-svn git-cvs mc 
hsh-install wget curl git-core vim-console less rpm-utils unzip xz gear-cronbuild libshell $cronbuild_requires

if [ -e ~/.gitconfig ]; then
    cp ~/.gitconfig "$workdir"/chroot/.in/
else
    cat <<EOF
file ~/.gitconfig is not found. please, configure your git using
    git config --global user.name "Your Name Comes Here"
    git config --global user.email you@yourdomain.example.com
EOF
    exit 1
fi

current_gitbranch="$(git branch | awk '/^\*/{print $2}')" ||:
[ -z "$gitbranch" ] && gitbranch="$current_gitbranch" ||:
[ -z "$gitbranch" ] && gitbranch=cronbuild ||:

if [ "$gitbranch" != "$current_gitbranch" ]; then
    git branch -D $gitbranch ||:
    git checkout -b $gitbranch
fi

cp -a . "$workdir"/chroot/.in/cronbuild.git
chgrp -R $gid2 "$workdir"/chroot/.in/cronbuild.git
chmod -R g+w "$workdir"/chroot/.in/cronbuild.git
pushd "$workdir"/chroot/.in/cronbuild.git >/dev/null
git init --shared=664
popd >/dev/null

if [ -x .gear/cronbuild-prepare-chroot-as-rooter ]; then
    install -m755 .gear/cronbuild-prepare-chroot-as-rooter "$workdir"/chroot/.in/
    hsh-run "$@" --rooter /.in/cronbuild-prepare-chroot-as-rooter
fi

cat <<EOF > "$workdir"/chroot/.in/cronbuild.sh
#!/bin/bash -e
. shell-quote
set -v
cp /.in/.gitconfig ~/
cd /.in/cronbuild.git
gear-cronbuild-apply-hooks
commitmsg=
[ -x .gear/cronbuild-commitmsg ] && commitmsg="\$(.gear/cronbuild-commitmsg)"
[ -n "\$commitmsg" ] && commitmsg="\"\$(quote_shell "\$commitmsg")\""
gear-commit \${commitmsg:+-m "\$commitmsg"} --no-edit
EOF
chmod 755 "$workdir"/chroot/.in/cronbuild.sh
hsh-run "$@" /.in/cronbuild.sh
git pull "$workdir"/chroot/.in/cronbuild.git $gitbranch

: <<'__EOF__'

=head1	NAME

gear-cronbuild-apply-hooks-in-hsh-chroot

=head1	SYNOPSIS

B<gear-cronbuild-apply-hooks-in-hsh-chroot>
[B<-h>] 
[B<-d> I<local branch>]
[B<-v>] 
[B<-q>] 
[-- <hsh-initroot arguments>]


=head1	DESCRIPTION

B<gear-cronbuild-apply-hooks-in-hsh-chroot> 
The safe variant of the gear-cronbuild-apply-hooks.  It use hasher chroot 
to apply gear-cronbuild hooks. Separate this utility options 
and hsh-initroot/hsh-run arguments and options with --.

Example:
gear-cronbuild-apply-hooks-in-hsh-chroot -v -- $TMP/hasher --mountpoints=/proc --number=1
or just
gear-cronbuild-apply-hooks-in-hsh-chroot
to run hasher with default settings.

=head1	OPTIONS

=over

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

Name of the local branch to apply gear-cronbuild hooks. Default is current branch.

=item	B<-h>

Display this help and exit.

=item	B<-v>

verbose

=item	B<-q>

quiet

=back

= head1 FILES

=over

=item B<.gear/cronbuild-options>

By default, gear-cronbuild-apply-hooks-in-hsh-chroot will install in hasher chroot 
the following packages:
wget curl git-core vim-console less rpm-utils unzip xz gear-cronbuild.

Any other package to be installed in hasher chroot should be explicitly
listed in I<cronbuild_requires> variable.

Example:
cronbuild_requires="git-svn rpm-macros-mingw32"

=item B<.gear/cronbuild-prepare-chroot-as-rooter>

Optional script that will be run in the hasher chroot with hasher's 
rooter user privileges before the main gear-cronbuild scripts.

Should be used for chroot setup, like editing configs in /etc and so on.

The script should be executable.

=item B<.gear/cronbuild-commitmsg>

Optional script to provide commit message. By default, the default message
of gear-commit is used.

The script should be executable.

=back

=head1 SEE ALSO

http://www.altlinux.org/Gear/cronbuild

=head1	AUTHOR

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

=head1	COPYING

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