#!/bin/bash
# upkg-buildd
#
# daemon which builds several upkg packages in periods
#
# Copyright (C) 2004-2006 Raffaele Sandrini, Jürg Billeter
#
# This file is part of Upkg (http://www.upkg.org).
#
# Upkg is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# Upkg 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 Upkg; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Authors:
#   Raffaele Sandrini <rasa at paldo dot org>
#   Jürg Billeter <juerg at paldo dot org>

[ $UID -eq 0 ] || { echo "$(basename $0) wants to be run as root"; exit 1; }
[ -e /etc/upkg-buildd.conf ] || exit 0
. /etc/upkg-buildd.conf

function buildd_log ()
{
	date +"%F %X $1" >> /var/log/upkg-buildd/upkg-buildd.log
}

mkdir -p /var/cache/upkg
mkdir -p $UPKG_ARCHIVE

while true
do
	BUILDING=0
	for package in $PACKAGES
	do
		tag=$(echo $package | cut -d ':' -f 2)
		package=$(echo $package | cut -d ':' -f 1)
		# if there wasn't a colon in $package, we get $packge = $tag
		[ "$package" != "$tag" ] || tag=default
		for branch in $BRANCHES
		do
			buildd_log "Considering '$package-$branch'"
			upkg --verbose --bootstrap --repos=$REPOSITORY/$branch --tag=$tag --branch=$branch $package upkg-buildd-script > /var/log/upkg-buildd/$package-$branch.log 2>&1
			ret=$?
			sed -i -e '/Generated at:/d' /var/cache/upkg/upkg-buildd-script
			if [ $ret -ne 0 -o ! -e /var/cache/upkg/upkg-buildd-script ]
			then
				buildd_log "Couldn't generate script for '$package-$branch', not building"
			elif md5sum -c /var/cache/upkg/upkg-buildd-$package-$branch.digest
			then
				buildd_log "No changes detected in '$package-$branch', not building"
			else
				BUILDING=1
				# make sure we don't delete the real /dev directory in case of multiple mounts
				while umount -l /upkg/dev ; do
					true
				done
				umount -l /upkg/* /upkg/var/cache/upkg /upkg
				rm -rf $UPKG_ROOT /upkg
				mkdir -p $UPKG_ROOT /upkg
				if [ "$UPKG_ROOT" != "/upkg" ] ; then
					mount --rbind --make-rslave $UPKG_ROOT /upkg
				fi
				buildd_log "Building $package-$branch"
				md5sum --text /var/cache/upkg/upkg-buildd-script > /var/cache/upkg/upkg-buildd-$package-$branch.digest
				bash /var/cache/upkg/upkg-buildd-script --force >> /var/log/upkg-buildd/$package-$branch.log 2>&1
				ret=$?
				if [ $ret -eq 0 ]
				then
					buildd_log "Build of $package-$branch was SUCCESSFUL"
					mkdir -p $UPKG_ARCHIVE/$package-$branch
					mv -f /var/log/upkg-buildd/$package-$branch.log $UPKG_ARCHIVE/$package-$branch
					EXT="img iso tar.gz tar.bz2 tar.xz tar.zst"
					for ext in $EXT
					do
						link=/var/cache/upkg/$package-$branch.$ext
						file=$(basename $(readlink -f $link))
						[ -L $link -a -f $(readlink -f $link) ] || continue
						buildd_log "Moving file $file to $UPKG_ARCHIVE/$package-$branch"
						mv -f $(readlink -f $link) $UPKG_ARCHIVE/$package-$branch
						( cd $UPKG_ARCHIVE/$package-$branch ; md5sum $file > $file.md5 )
						OLD=$(readlink -f $UPKG_ARCHIVE/$package-$branch/$package-$branch.$ext)
						ln -sf $file $UPKG_ARCHIVE/$package-$branch/$package-$branch.$ext
						ln -sf $file.md5 $UPKG_ARCHIVE/$package-$branch/$package-$branch.$ext.md5
						rm -f $OLD $OLD.md5
					done
				else
					buildd_log "Build of $package-$branch FAILED"
					mv /var/log/upkg-buildd/$package-$branch{,-ERROR}.log
				fi
			fi
		done
	done
	if [ $BUILDING -eq 0 ]
	then
		buildd_log "No significant change in any package, sleeping for 5 minutes"
		sleep 5m
	fi
done
