#!/bin/sh -e

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

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

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

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

# getting hasher variables: workdir
number=
workdir=
eval `hsh --printenv "$@"`
if [ -z "$workdir" ]; then
    cat <<EOF
FATAL: hasher configuration error: workdir variable is not set.
please, check ~/.hasher/config or specify hasher arguments.
Example:
gear-cronbuild-apply-hooks-in-hsh-chroot -v -- $TMP/hasher1 --mountpoints=/proc --number=1
EOF
    exit 1
fi

hsh "$@" --initroot-only
for f in /etc/host.conf /etc/hosts /etc/resolv.conf; do
    hsh-copy --rooter --workdir "$workdir" $f $f
done
cronbuild_requires=
[ -e ./.gear/cronbuild-options ] && . ./.gear/cronbuild-options ||:
# skip git-arch git-svn git-cvs mc
hsh-install "$workdir" 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/
elif [ -e ~/.config/git/config ]; then
    cp ~/.config/git/config "$workdir"/chroot/.in/.gitconfig
else
    cat <<EOF
The file ~/.gitconfig or ~/.config/git/config 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

# error: unable to set permission to '.git/objects/xx/xxx/...'
git gc --prune=all
gid2=`hsh-run "$workdir" -- grep -E '^builder:x:' /etc/group | awk -F: '{print $3}'`
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/
    share_network=1 hsh-run "$workdir" --rooter -- /.in/cronbuild-prepare-chroot-as-rooter
fi

cat <<EOF > "$workdir"/chroot/.in/cronbuild.sh
#!/bin/bash -e
. shell-quote
set -v
mkdir -p ~/.config/git
cp /.in/.gitconfig ~/.config/git/config
git config --global --add safe.directory /.in/cronbuild.git
cd /.in/cronbuild.git
gear-cronbuild-apply-hooks ${tarball_exclude_pattern:+--exclude '$tarball_exclude_pattern'}
[ -n "\$(git status --porcelain -s -uno)" ] || exit 0
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
if share_network=1 hsh-run "$workdir" -- /.in/cronbuild.sh; then
    # on success
    git pull "$workdir"/chroot/.in/cronbuild.git $gitbranch
else
    # on failure
    exitcode=$?
    # let's make a copy of the logfile
    rm -f ./cronbuild-apply-hooks.log
    [ ! -f "$workdir"/chroot/.in/cronbuild.git/cronbuild-apply-hooks.log ] || \
	cp -f "$workdir"/chroot/.in/cronbuild.git/cronbuild-apply-hooks.log .
    exit $exitcode
fi

: <<'__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<--exclude> I<pattern>]
[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<--exclude> I<pattern>

Pattern to pass to gear-uupdate/gear-update.

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