#!/bin/bash
#
#   makeworld - a makepkg wrapper to build multiple packages
#
#   Copyright (C) 2002-2007 by Judd Vinet <jvinet@zeroflux.org>
#   Copyright (C) 2007-2010 Aaron Griffin <aaron@archlinux.org>
#
#   This program 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.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

ABS_VERSION="%%ABS_VERSION%%"
BUG_REPORT_EMAIL='pacman-dev@archlinux.org'
toplevel=$(pwd)

##
# Signal Traps
##
trap 'error "TERM signal caught. Exiting..."; exit 1' TERM HUP QUIT
trap 'error "Aborted by user! Exiting..."; exit 1' INT
trap 'error "An unknown error has occured. Exiting..."; exit 1' ERR

usage() {
	printf "makeworld %s - a makepkg wrapper to build multiple packages\n" "$myver"
	echo
	printf "Usage: %s [options] <destdir> <repo> [repo] ...\n" "$0"
	echo
	echo "Where <repo> is one or more directory names under the ABS root"
	echo "  eg: makeworld -c /packages core extra"
	echo
	echo "This should be run from the toplevel directory of ABS (usually /var/abs)"
	echo
	echo "See makepkg --help for supported options (passed directly to makepkg)"
}

version() {
	printf "makeworld %s\n" "$myver"
	printf "\
Copyright (C) 2002-2007 Judd Vinet <jvinet@zeroflux.org>\n\n\
Copyright (C) 2007-2010 Aaron Griffin <aaron@archlinux.org>\n\n\
This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n"
}

# Source makepkg.conf; fail if it is not found (need this for $CARCH, $PKGEXT)
if [ -r "/etc/makepkg.conf" ]; then
	source "/etc/makepkg.conf"
else
	printf "%s not found" "/etc/makepkg.conf"
	echo "Aborting..."
	exit 1
fi

# Source user-specific makepkg.conf overrides
if [ -r ~/.makepkg.conf ]; then
	source ~/.makepkg.conf
fi


MAKEPKG_OPTS=
for arg in $*; do
	case $arg in
		--help)
			usage; exit 0 ;;
		--version)
			version; exit 0 ;;
		--*)
			# pass anything except --help or --version to makepkg,
			#   and let it error out later if it was invalid
			MAKEPKG_OPTS="$MAKEPKG_OPTS $arg" ;;
		-*)
			while getopts "AcCdefFghiLmorRsV-" opt; do
				case $opt in
					h)
						usage; exit 0 ;;
					V)
						version; exit 0 ;;
					-)
						OPTIND=0; break ;;
					*)
						MAKEPKG_OPTS="$MAKEPKG_OPTS -$opt" ;;
				esac
			done
			;;
		*)
			dest=$arg; shift; break ;;
	esac
	shift
	if [ "$dest" != "" ]; then
		break
	fi
done

if [ "$dest" = "" ]; then
	usage
	exit 1
fi

if [ $# -lt 1 ]; then
	usage
	exit 1
fi

# convert a (possibly) relative path to absolute
if [ ! -d "$dest" ]; then
	echo "$dest does not exist, creating directory"
	mkdir "$dest"
fi
cd "$dest"
dest="$(pwd)"
cd - &>/dev/null

sd=$(date +"[%b %d %H:%M]")

for repo in $*; do
	for pkg in $(find "$toplevel/$repo" -maxdepth 1 -mindepth 1 -type d | sort); do
		cd $pkg
		if [ -f PKGBUILD ]; then
			. PKGBUILD
			buildstatus=0
			if [ ! -f "$dest/$pkgname-$pkgver-$pkgrel-$CARCH$PKGEXT" -a ! -f "$dest/$pkgname-$pkgver-$pkgrel-any$PKGEXT" ]; then
				PKGDEST="$dest" makepkg $MAKEPKG_OPTS -m 2>&1 1>&2 | tee -a $toplevel/makepkg.log

				if [ ${PIPESTATUS[0]} -ne 0 ]; then
					buildstatus=2
				else
					buildstatus=1
				fi

			fi

			d=$(date +"[%b %d %H:%M]")
			echo -n "$d  " >>$toplevel/build.log
			case $buildstatus in
				0) echo "$pkgname already built -- skipping" >>$toplevel/build.log ;;
				1) echo "$pkgname was built successfully" >>$toplevel/build.log ;;
				2) echo "$pkgname build failed" >>$toplevel/build.log ;;
			esac
		fi
	done
done
ed=$(date +"[%b %d %H:%M]")

echo "makeworld complete." >>$toplevel/build.log
echo "  started:  $sd" >>$toplevel/build.log
echo "  finished: $ed" >>$toplevel/build.log

exit 0

# vim: set ts=2 sw=2 noet:
