#! /bin/sh

cvsup_server=cvsup.freebsd.org
supfile=/usr/share/examples/cvsup/standard-supfile
logfile=/var/log/build.log

usage() {
	echo "Upgrade a FreeBSD system using cvsup and buildworld"
	echo "You must be in the /usr/src or similar or you'll get odd results"
	echo "Usage: $0 [-mn] [-l <logfile>] [-s <supfile>] [-H <host>]"
	echo "  -m: don't send email when finished"
	echo "  -n: don't cvsup"
	echo "  -M <args>: make buildworld arguments"
	echo "  -c <args>: cvsup arguments"
	echo "  -l: log to <logfile> instead of $logfile"
	echo "  -s: use supfile instead of $supfile"
	echo "  -H: cvsup using <host> instead of $cvsup_server"
	echo "  -h: this help"
}

# void command: do nothing.
void () {
}

args=`getopt hmnM:l:s:H: $*`

if [ $? != 0 ] ; then
        usage
        exit 1
fi

set -- $args

for i; do
        case "$i" in
		-H)
			cvsup_server=$2; shift; shift;;
		-s)
			supfile=$2; shift; shift;;
                -l)
                        logfile=$2; shift; shift;;
                -c)
                        cvsupargs=$2; shift; shift;;
                -M)
                        makeargs=$2; shift; shift;;
                -n)
                        nocvsup="void"; shift;;
		-m)
			mflag="-m"; shift;;
                --)
                        shift; break;;
		-h)
			usage; exit; break;;
        esac
done

$nocvsup buildit -m -l $logfile -- "cvsup $cvsupargs -h $cvsup_server $supfile" && \
buildit $mflag -l $logfile -- make $makeargs buildworld | whereintheworld
