#
# This script is run from the top level scripts.  You can rely on
# the following environment variables being set:
#
#    $BMTARBALL  - relative path to the release source tarball (bz2)
#    $BMTARBASE  - basename of the source tarball (bz2)
#    $BMBUILDDIR - directory to place resulting binary package tree
#

set -e

rm -rf /tmp/bmremote
mkdir -p /tmp/bmremote

#
# Build the DEB's
#

function do_ubuntu() {
	./save.sh /tmp/bmremote/$1 "$2" "$3" \
		./remote.sh root barrybuild.netdirect.ca 22 \
			"$BMTARBALL make-deb.sh chroot.sh" \
			/tmp/bmchroots/$1 \
			/tmp/bmremote/$1 \
		./chroot.sh cdfrey /home/chroot/$1 \
			"$BMTARBASE make-deb.sh" \
			/home/chroot/$1/home/cdfrey/barrychroot/bmbuild/results \
			/tmp/bmchroots/$1 \
			cdfrey \
		./make-deb.sh "$BMTARBASE" \
			"$4" \
			bmbuild bmbuild
}

# Build on the Debian squeeze host system too
function do_debian() {
	./save.sh /tmp/bmremote/squeeze64 "$BMBUILDDIR/dists/squeeze/main/binary-amd64" "" \
		./remote.sh cdfrey barrybuild.netdirect.ca 22 \
			"$BMTARBALL make-deb.sh" \
			/home/cdfrey/barryremote/bmbuild/results \
			/tmp/bmremote/squeeze64 \
		./make-deb.sh "$BMTARBASE" \
			"make -j16 debian-all" \
			bmbuild bmbuild
}


#
# Build the Fedora RPM's
#
# ./rpm.sh <path_to_chroot> <tag>
#

function do_fedora() {
	./save.sh "/tmp/bmremote/$1" "$2" "$4" \
		./remote.sh root barrybuild.netdirect.ca 22 \
			"$BMTARBALL make-bm-rpm.sh chroot.sh" \
			/tmp/bmchroots/$1 \
			/tmp/bmremote/$1 \
		./chroot.sh cdfrey /home/chroot/$1 \
			"$BMTARBASE make-bm-rpm.sh" \
			/home/chroot/$1/home/cdfrey/rpmbuild/bmbuild/results \
			/tmp/bmchroots/$1 \
			cdfrey \
		./make-bm-rpm.sh \
			"$BMTARBASE" \
			"$5" \
			/home/cdfrey \
			/home/cdfrey/rpmbuild/bmbuild
	mkdir -p "$3"
	mv "$2"/*src.rpm "$3"
}

do_ubuntu ubuntu1004 "$BMBUILDDIR/dists/ubuntu1004/main/binary-amd64" "" \
	"make -j16 debian-all deb-libtar"
# Ubuntu 11.04 (Natty) is also good for Mint 11
do_ubuntu ubuntu1104 "$BMBUILDDIR/dists/ubuntu1104/main/binary-amd64" "" \
	"make -j16 debian-all deb-libtar"
# Ubuntu 11.10 (Oneric) should also be good for Mint 12
do_ubuntu ubuntu1110 "$BMBUILDDIR/dists/ubuntu1110/main/binary-amd64" "" \
	"make -j16 debian-all deb-evo3-latest deb-libtar"
do_ubuntu ubuntu1204 "$BMBUILDDIR/dists/ubuntu1204/main/binary-amd64" "" \
	"make -j16 debian-all deb-evo3-latest deb-libtar"
do_debian

# Fedora and openSUSE use make 3.82, which is buggy and doesn't handle
# parallel builds safely all cases.  So we can't use any -j options here.
# http://lists.gnu.org/archive/html/bug-make/2012-01/msg00042.html
do_fedora fedora16 \
	"$BMBUILDDIR/dists/fedora16/x86_64/RPMS" \
	"$BMBUILDDIR/dists/fedora16/source-x86_64/SRPMS" "" \
	"make rpmclean && make LIB_SUFFIX=64 rpm-all rpm-evo3-latest"
do_fedora fedora17 \
	"$BMBUILDDIR/dists/fedora17/x86_64/RPMS" \
	"$BMBUILDDIR/dists/fedora17/source-x86_64/SRPMS" "" \
	"make rpmclean && make LIB_SUFFIX=64 rpm-all rpm-evo3-latest"
do_fedora opensuse12.1 \
	"$BMBUILDDIR/dists/opensuse12.1/x86_64/RPMS" \
	"$BMBUILDDIR/dists/opensuse12.1/source-x86_64/SRPMS" "" \
	"make rpmclean && make opensuse && make LIB_SUFFIX=64 BARRY_GUISU=kdesu rpm-libtar rpm-all"


#
# Cleanup
#
rm -rf /tmp/bmremote
ssh -x -a -2 root@barrybuild.netdirect.ca "rm -rf /tmp/bmchroots"

