#!/bin/bash
#
#   makeworld
#
#   Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.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/>.
#

myver='1.0'
BUG_REPORT_EMAIL='pacman-dev@archlinux.org'
toplevel=$(pwd)

usage() {
	printf "makeworld %s - a makepkg wrapper to build multiple packages\n" "$myver"
	echo
	printf "Usage: %s [options] <destdir> <category> [category] ...\n" "$0"
	echo
	echo "Where <category> is one or more directory names under the ABS root"
	echo "  eg: makeworld -c /packages base lib editors"
	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\
This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n"
}

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 "chisSbdfrBCemoSV-" 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 category in $*; do
	for port in $(find "$toplevel/$category" -maxdepth 1 -mindepth 1 -type d | sort); do
		cd $port
		if [ -f PKGBUILD ]; then
			. PKGBUILD
			buildstatus=0
			if [ ! -f "$dest/$pkgname-$pkgver-$pkgrel.pkg.tar.gz" ]; then
				PKGDEST="$dest" makepkg $MAKEPKG_OPTS -m 2>>$toplevel/makepkg.log
				if [ $? -gt 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:
