#!/bin/sh
#
# $Id: release,v 1.62 2009-01-28 17:20:10 potyra Exp $
#
# Copyright (C) 2003-2009 FAUmachine Team <info@faumachine.org>.
# This program is free software. You can redistribute it and/or modify it
# under the terms of the GNU General Public License, either version 2 of
# the License, or (at your option) any later version. See COPYING.

set -e

build=$(cd ${0%release}; pwd)/build.sh
askbuild=yes


if [ ! -f /etc/debian_version ]; then
	echo "Please run this script on one of the debian machines"
	exit 1
fi

# only do something if we are in a FAUmachine working dir
if [ ! -d node-pc ]; then
	cat <<EOF
Please specify the version to release or start this script from a
FAUmachine working directory to release the current version.
EOF
	exit 1
fi

version=$1
if [ ! "$version" ]; then
	version=HEAD
fi
if [ $version = HEAD ] ; then
	version_date=`date +'%Y%m%d'`
	version_cvs=HEAD
else
	version_date=${version}
	version_cvs=v${version_date}
fi

if [ $version != HEAD ] ; then
	# check that the tag exists
	$build testtag --release=$version || {
		# now ask if the current dir should be tagged
		echo "There is no cvs tag \"v$version\"!"
		echo
		echo "Press return to automatically tag the sources in the current directory."
		echo "The new tag name will be \"v$version\"."
		read
		cvs tag -F v$version
		echo
	}
fi

echo "going to build release \"$version\"..."

mkdir $version
cd $version

# FIXME: CVS tags cannot contain '.', '_' is used instead
# make that work in release and build.sh scripts later
# (not needed for simple HEAD or 20030704 version numbers)

# build source packages
$build tar --release=$version
$build deb --release=$version --srctar=`pwd`/faumachine-${version_date}.tar.gz
$build rpm --release=$version --srctar=`pwd`/faumachine-${version_date}.tar.gz

declare -a dist
declare -a host
declare -a rpmbuild
declare -a tmpdir
declare -a lkm

# Debian hosts
dist[0]=debian40-x86
host[0]=faui31e
tmpdir[0]=/export/home.local/$USER

dist[1]=debian40-x86_64
host[1]=faui31f
tmpdir[1]=/export/home.local/$USER

# Ubuntu host
dist[2]=ubuntu710
host[2]=faui31l
tmpdir[2]=/export/home.local/$USER

# FedoraCore hosts
dist[3]=fc6
host[3]=131.188.33.98
rpmbuild[3]=rpmbuild
tmpdir[3]=/home/$USER

# SuSE hosts
dist[4]=suse103
host[4]=131.188.33.112
rpmbuild[4]=rpmbuild
tmpdir[4]=/home/$USER

# OpenBSD hosts
dist[5]=openbsd36
host[5]=131.188.33.29
tmpdir[5]=/export/home.local/${USER}
lkm[5]=yes

for i in `seq 0 5` ; do
(
	echo ""
	echo "**** Building binaries for ${dist[$i]} ****"
	echo ""

	mkdir -p ${dist[$i]}
	tmp=${tmpdir[$i]}/build-faumachine-${version_date}-$$

	case ${dist[$i]} in
	debian*|ubuntu*)
		# build debian/ubuntu binary
		scp faumachine_0.${version_date}.1.dsc \
			faumachine_0.${version_date}.1.tar.gz \
			${host[$i]}:${tmpdir[$i]}

		ssh ${host[$i]} " \
			cd ${tmpdir[$i]}; \
			dpkg-source -x faumachine_0.${version_date}.1.dsc; \
			cd faumachine-0.${version_date}.1; \
			debuild binary; \
			cd .."

		scp ${host[$i]}:${tmpdir[$i]}/faumachine'*'_0.${version_date}.1_'*'.deb \
			${dist[$i]}

		ssh ${host[$i]} " \
			cd ${tmpdir[$i]}; \
			rm faumachine*_0.${version_date}.1_*.deb; \
			rm faumachine*_0.${version_date}.1*; \
			rm -rf faumachine-0.${version_date}.1"
		;;

	fc*|suse*)
		ssh ${host[$i]} " \
			rm -rf ${tmpdir[$i]}/build-faumachine-*; \
			set -x ;\
			mkdir -p ${tmp}; cd ${tmp}; \
			mkdir -p SRPMS BUILD RPMS; \
			mkdir -p RPMS/athlon RPMS/i{3,4,5,6}86 RPMS/noarch ;\
			mv ~/.rpmmacros ~/.rpmmacros.tmp-before-script 2>/dev/null || true; \
			echo %_topdir ${tmp} > ~/.rpmmacros; \
			exit \$?"

		scp faumachine-${version_date}-1.src.rpm ${host[$i]}:${tmp}/SRPMS

		ssh ${host[$i]} " \
			set -x; \
			cd ${tmp}; \
			rpm -hiv SRPMS/faumachine-${version_date}-1.src.rpm; \
			. /etc/profile; \
			export PATH=${PATH}:/usr/X11R6/bin:/usr/local/bin/qt3/bin; \
			export PKG_CONFIG_PATH=/opt/gnome/lib/pkgconfig; \
			${rpmbuild[$i]} --clean -bb SPECS/faumachine.spec; \
			exit \$?"

		scp ${host[$i]}:${tmp}/RPMS/*/faumachine-${version_date}-1.*.rpm ${dist[$i]}/

		ssh ${host[$i]} " \
			set -x; \
			rm ~/.rpmmacros; \
			mv ~/.rpmmacros.tmp-before-script ~/.rpmmacros 2>/dev/null || true; \
			rm -rf ${tmp}; \
			exit 0"
		;;

	openbsd*ports*)
		# scp faumachine-core-$version.tar.gz ${host[$i]}:/usr/ports/distfiles
		# 
		# ssh ${host[$i]} " \
		# 	set -x; \
		# 	cd /usr/ports/emulators/FAUmachine; \
		# 	make clean; \
		# 	make; \
		# 	exit \$?"
		# scp ${host[$i]}:/usr/ports/packages/i386/all/faumachine-HEAD.tgz ${dist[$i]}
		;;

	openbsd*)
		ssh ${host[$i]} " \
			set -x; \
			rm -rf ${tmpdir[$i]}/faumachine-${version_date}; \
			mkdir -p ${tmp}/tar; \
			exit \$?"

		scp faumachine-${version_date}.tar.gz ${host[$i]}:${tmp}/tar

		ssh ${host[$i]} " \
			set -x; \
			cd ${tmp}; \
			tar xzf tar/faumachine-${version_date}.tar.gz; \
			cd faumachine-${version_date}; \
			export AUTOMAKE_VERSION=1.9; \
			export AUTOCONF_VERSION=2.59; \
			./autogen.sh; \
			./configure --prefix=/usr/local --disable-build-hefkg; \
			gmake; \
			gmake install DESTDIR=\`pwd\`/faumachine-${version_date}-i386; \
			cd faumachine-${version_date}-i386/; \
			tar zcvf ../../tar/faumachine-${version_date}.i386.tar.gz .; \
			exit \$?"

		scp ${host[$i]}:${tmp}/tar/faumachine-${version_date}.i386.tar.gz ${dist[$i]}

		if [ ${lkm[$i]} = yes ] ; then \
			ssh ${host[$i]} " \
				set -ex; \
				cd ${tmp}/faumachine-${version_date}/opt-openbsd/opt-openbsd-3.6/src/sys/compat/faumachine; \
				make obj; \
				make depend; \
				make; \
				cd ${tmp}; \
				mkdir -p usr/lkm; \
				cp faumachine-${version_date}/opt-openbsd/opt-openbsd-3.6/src/sys/compat/faumachine/obj/faumachine.o usr/lkm; \
				tar zcvf tar/openbsd-faumlkm-${version_date}.i386.tar.gz usr"

			scp ${host[$i]}:${tmp}/tar/openbsd-faumlkm-${version_date}.i386.tar.gz ${dist[$i]}
		fi
	
		ssh ${host[$i]} " \
			set -x; \
			rm -rf ${tmp}; \
			exit 0"
		;;

	*)
		echo "Unknown distribution ${dist[$i]}" 1>&2
		exit 1
		;;
	esac

	echo ""
	echo "**** Building binaries for ${dist[$i]} done ****"
	echo ""
) > build.${dist[$i]}.log 2>&1 &
done
wait

echo
echo "All release files are located in the new directory \"$version\"."
echo "Please add this directory to the www tree and run 'make'"
echo "to publish this release."
