#!/usr/bin/env bash
# set -x # uncomment for debug
# Builds packages from ABS recursively. It tries to find dependencies that
# aren't built or need update and then makepkg them in order.

usage() {

	echo "cd to a dir containing a PKGBUILD and run:"
	echo "$0 [build_dir]"
	echo ""
	echo "This script will check dependencies, build them if possible "
	echo "and stage the packages on it's repo."
	echo ""
	echo "OPTIONS:"
	echo " -h           : this message."
	echo ""
	echo "Wrapper for \`fullpkg-find' and \`fullpkg-build'"
	echo ""
	exit 1

}

while getopts 'h' arg; do
	case "$arg" in
		h) usage ;;
	esac
done

shift $(( OPTIND - 1 ))

build_dir="${1:-$(mktemp -d /tmp/fullpkg.XXXXXX)}"
fullpkg-find "$build_dir" && fullpkg-build -N "$build_dir"

exit 0