#!/bin/sh

##############################
## TODO: PIPESTATUS is bashism
##############################

logfile=cronbuild-apply-hooks.log
difffile=cronbuild-apply-hooks.diff
EXIT_MAGIC_CODE_NOTHING_TO_DO=77

error_hook()
{
    local hook_name="$1"; shift;
    echo "cronbuild: the hook $hook_name exited ubnormally. see $logfile for details."
    exit 2
}

do_hook()
{
    local hook="$1"; shift;
    echo running "$hook" "$@"
    "$hook" "$@" 2>&1 | tee -a "$logfile" 
    [ ${PIPESTATUS[0]} -gt 0 ] && error_hook "$hook"
    return 0
}

add_changelog_builtin()
{
    local gear_specfile="$1"; shift;
    adate=$(date +"%Y%m%d")
    add_changelog -e "- repocop cronbuild $adate. At your service." $gear_specfile
}

find_watchfile()
{
    local pkgname=$1
    watchfile=
    for i in .gear/$pkgname*.watch $pkgname*.watch .gear/autobuild.watch .gear/*.watch *.watch ; do
	if [ -f $i ]; then
	    watchfile=$i
	    return 0
	fi
    done
    watchfile=`find . -path '*/debian/watch' -type f | head -1`
    [ -f "$watchfile" ] && return 0
    return 1
}

if ! [ -d .gear ]; then
    echo ".gear directory not found. exiting."
    exit 1
fi

[ -e "$logfile" ] && rm -f "$logfile"
[ -e "$difffile" ] && rm -f "$difffile"

gear_specfile="$(gear --command sh -- -c 'printf %s "$gear_specfile"')"
if [ -z "$gear_specfile" ] || ! [ -e "$gear_specfile" ]; then
    echo "cronbuild: can't find spec file. exiting."
    exit 1
fi

saved_specfile="$gear_specfile".sav.~
saved_changelog="$gear_specfile".changelog.sav.~
gear_changelog="$gear_specfile".changelog.new.~
cat "$gear_specfile" > "$saved_specfile"
sed -e '1,/^%changelog/d' "$gear_specfile" > "$saved_changelog"

last_commit=`git show-ref --head HEAD | awk '{if ($2=="HEAD") print $1;}'`
last_commit=${last_commit:=HEAD}

gear_pkg_name="$(gear --command sh -- -c 'printf %s "$gear_pkg_name"')"
if [ -x ./.gear/cronbuild-update-source ]; then
    do_hook ./.gear/cronbuild-update-source $gear_specfile
elif find_watchfile $gear_pkg_name; then
    echo "INFO: using watchfile $watchfile"
    rpm-uscan --any-archive --watchfile="$watchfile" --force-action=gear-uupdate
else
    echo "cronbuild: the required hook ./.gear/cronbuild-update-source is missing or not executable."
    exit 3
fi

git diff $last_commit > "$difffile"
if ! [ -s "$difffile" ]; then
    echo "INFO: source files did not change after cronbuild-update. Nothing to do.";
    rm -f "$difffile"
    exit $EXIT_MAGIC_CODE_NOTHING_TO_DO;
fi
rm -f "$difffile"

if [ -x ./.gear/cronbuild-update-version ]; then
    do_hook ./.gear/cronbuild-update-version $gear_specfile
elif cmp "$saved_specfile" "$gear_specfile"; then
    echo "INFO: spec file did not change after cronbuild-update-source."
    echo "INFO: running built-in cronbuild-update-version"
    do_hook gear-cronbuild-update-spec-timestamp $gear_specfile
else
    echo "INFO: spec file changed after cronbuild-update-source."
    echo "INFO: skip built-in built-in cronbuild-update-version"
fi

sed -e '1,/^%changelog/d' "$gear_specfile" > "$gear_changelog"
if [ -x ./.gear/cronbuild-add-changelog ]; then
    do_hook ./.gear/cronbuild-add-changelog $gear_specfile
elif cmp "$saved_changelog" "$gear_changelog"; then
    echo "INFO: changelog did not change before cronbuild-add-changelog."
    echo "INFO: running built-in cronbuild-add-changelog"
    do_hook add_changelog_builtin $gear_specfile
else
    echo "INFO: changelog changed before cronbuild-add-changelog."
    echo "INFO: skip built-in built-in cronbuild-add-changelog"
fi

rm -f "$saved_specfile" "$saved_changelog" "$gear_changelog"

git add $gear_specfile

: <<'__EOF__'

=head1	NAME

gear-cronbuild-apply-hooks

=head1	SYNOPSIS

B<gear-cronbuild-apply-hooks>

=head1	DESCRIPTION

B<gear-cronbuild-apply-hooks> 

=head1	OPTIONS


= head1 FILES

=over

=item B<.gear/cronbuild-update-source>

Required.

=item B<.gear/cronbuild-update-version>

Optional.

=item B<.gear/cronbuild-add-changelog>

Optional.

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