#!/bin/sh

export LANG=C
export LANGUAGE=C

PROG=`basename $0`

show_help()
{
#  -q, --quiet                       try to be more quiet;
#  -v, --verbose                     print a message for each action;
	cat <<EOF
$PROG - script to build, test and integrate (with -c) the set of packages. Current path must be valid autorepo directory.

Usage: $PROG [options]

Options:
  -c,--continuous  continuous integration; add packages to repo at each successful build
  -d,--drop        drop package at first unsuccessful build
  -h, --help       show this text and exit.

Report bugs to http://bugzilla.altlinux.org/
EOF
	exit
}

continuous_integration_mode=
rapid_drop_mode=

TEMP=`getopt -n $PROG -o cdh -l continuous,drop,help -- "$@"` ||
	show_help
eval set -- "$TEMP"

while :; do
	case "$1" in
		-c|--continuous) continuous_integration_mode=1
			;;
		-d|--drop) rapid_drop_mode=1
			;;
		-h|--help) show_help
			;;
		--) shift; break
			;;
		*) echo "Error parsing arguments. see --help" ; exit 1
			;;
	esac
	shift
done

. autorepo-config
. autorepo-sh-functions
autorepo_lock_workdir_or_exit

OUTDIR=$AUTOREPO_HOME/OUT
[ -d OUT ] && OUTDIR=OUT

for pkg in $OUTDIR/*.{src.rpm,tar,transaction}; do
    # or nullglob
    if [ -e "$pkg" ]; then
	autorepo-helper-do-atomic-build \
	    ${hashernumber:+--number $hashernumber} \
	    ${rapid_drop_mode:+--drop} "$pkg" || exit 1

	if [ -n "$continuous_integration_mode" ]; then
	    autorepo-merge-good
	    autorepo-rebuild-repository
	    autorepo-rm-out-dups
	fi
    fi
done

autorepo_unlock_workdir_safe
