#!/bin/bash

# sbrun wrapper, runs multiple builds.

# usage: sbqrun arg [arg ...]

if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
	cat <<EOF
Usage: $( basename $0 ) [-n] [-t] [-p] [-d] arg [arg ...]

Options:
  -t  Don't track filesystem writes (passed to sbrun).
  -n  Allow network access (passed to sbrun).
  -p  If a binary package already exists for a given build, install
      it instead of building a new one.
  -d  Download sources only; do not build or install packages.

Run this script from the top level of a SBo repo clone!

If arg is a file, its contents are added to the list of builds. Use -
to read a list of builds from stdin (possibly piped from sbodeps -q).

If arg is a directory, it's added to the list.

If arg is neither, it's assumed to be a bare build name (with no category/
directory) and searched for.

Full sbopkg queue syntax is NOT supported, only a simple list of builds
(no |VAR=value, no @file, no -app). If you need sbopkg, use sbopkg!

Once the list is built, each build is downloaded with sbodl, built with
sbrun -c, and installed with upkg, in the order given.

The -p option requires SlackBuild scripts that support the
PRINT_PACKAGE_NAME option (Slackware 15.0 and up). It searches
for built packages in the same place SlackBuild scripts place them
(OUTPUT, TMP, or /tmp).
EOF
	exit 0
fi

olddir="$(pwd)"
source ~/sbostuff/sbostuff.sh
cdsb

for arg; do
	if [ "$arg" = "-p" ]; then
		use_prebuilt=yes
	elif [ "$arg" = "-d" ]; then
		download_only=yes
	elif [ "$arg" = "-t" -o "$arg" = "-n" ]; then
		SBRUN_OPTS="$SBRUN_OPTS $arg"
	elif [ "$arg" = "-" ]; then
		while read build; do
			BUILDS="$BUILDS $build"
		done
	elif [ -f "$olddir/$arg" ]; then
		BUILDS="$BUILDS `cat $olddir/$arg`"
	elif [ -d "$arg" ]; then
		BUILDS="$BUILDS $arg"
	else
		arg=$( echo */$arg )
		if [ -d "$arg" ]; then
			BUILDS="$BUILDS $arg"
		else
			echo "!!! no idea what '$arg' is, skipping"
		fi
	fi
done

FAILED=""
COUNT=0
FAILCOUNT=0

for build in $BUILDS; do
	: $(( COUNT ++ ))
	echo "==== sbqrun: COUNT=$COUNT, FAILCOUNT=$FAILCOUNT, starting $build"
	if ! cdsb "$build"; then
		echo "==== sbqrun: ERROR: no such build: $build"
		FAILED+="$build "
		: $(( FAILCOUNT ++ ))
		continue
	fi

	if [ "$use_prebuilt" = "yes" ]; then
		script="$( pwd | sed 's,.*/,,' )".SlackBuild
		if ! grep -q '^[^#]*PRINT_PACKAGE_NAME' "$script"; then
			echo "==== sbqrun: PRINT_PACKAGE_NAME not supported by $script, ignoring -p"
		else
			pkgnam="$( PRINT_PACKAGE_NAME=1 sh $script )"
			OUTPUT="${OUTPUT:-${TMP:-/tmp}}"
			if [ -e "$OUTPUT/$pkgnam" ]; then
				upkg "$OUTPUT/$pkgnam"
				continue
			fi
		fi
	fi

	sbodl || echo "==== sbqrun: WARN: download failed, trying build anyway"

	if [ "$download_only" = "yes" ]; then
		echo "==== sbqrun: downloaded source for $build"
		continue
	fi

	if ! sbrun -c $SBRUN_OPTS; then
		echo "==== sbqrun: BUILD FAILED: $build, see $(pwd)/build.log"
		FAILED+="$build "
		: $(( FAILCOUNT ++ ))
	else
		if ! upkg; then
			echo "==== sbqrun: $build built, but FAILED to install"
			FAILED+="$build "
			: $(( FAILCOUNT ++ ))
		fi
	fi
done

echo "==== Processed $COUNT builds"
if [ "$FAILCOUNT" = "0" ]; then
	echo "==== No failures"
	exit 0
else
	echo "==== $FAILCOUNT build failures: $FAILED"
	exit 1
fi
