#!/bin/bash
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/scripts/Build-Job
# ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
# 
# 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. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

config=default
xdaemon=0
daemon=0

while [ "$1" ] ; do
    case "$1" in
	-daemon)
		daemon=1  ; shift ;;
	-xdaemon)
		xdaemon=1 ; shift ;;
	-cfg)
		config=$2 ; shift ; shift ;;
	-*)
		echo "Usage: $0 [ -cfg <config> ] { -daemon | <job(s)> }"
		exit 1 ;;
	*)
		break ;;
    esac
done

. ./scripts/parse-config
qdir="build/$ROCKCFG_ID/ROCK/queue"

build_logs="build/$ROCKCFG_ID/ROCK/logs"
mkdir -p "${build_logs}"

if [ $daemon = 1 ] ; then
    if [ -f $qdir/queue.txt ] ; then
	if [ -f $qdir/use_build_job_daemon ] ; then
		nohup $0 -cfg $config -xdaemon > /dev/null &
		sleep 1 ; exit 0
	else
		echo
		echo "The build has been configured to use a command for"
		echo "adding jobs (using another job scheduler)."
		echo
		exit 1
	fi
    else
	echo
	echo "$qdir:"
	echo "Queue not found! Please start 'Build-Target -cfg $config'"
	echo "first on the master node ..."
	echo
	exit 1
    fi
fi

if [ $xdaemon = 1 ] ; then
	echo "Writing output to $build_logs/build_job_${HOSTNAME}_$$.log." >&2
	exec > "$build_logs/build_job_${HOSTNAME}_$$.log" 2>&1 < /dev/null

	echo -e "Build-Job (daemon mode)\trunning on ${HOSTNAME} with PID $$" \
				> "$qdir/build_job_${HOSTNAME}_$$.stat"
	date "+%H:%M ${HOSTNAME}	new build-job daemon with PID $$" \
				>> $qdir/1_$qid.msg

	while [ -f $qdir/queue.txt -a -f $qdir/use_build_job_daemon -a \
			-f "$qdir/build_job_${HOSTNAME}_$$.stat" ] ; do
		didsomething=0
		while read next && [ "$next" ] ; do
			set $next ; qid="$1-$6"
			if [ -f $qdir/$qid.job -a ! -f $qdir/$qid.lock ] ; then
				$0 -cfg $config $qid
				didsomething=1 ; break
			fi
		done < $qdir/queue.txt
		[ $didsomething = 1 ] || sleep 3
	done

	rm -f "$qdir/build_job_${HOSTNAME}_$$.stat"
	if [ -f $qdir/queue.txt ] ; then
		date "+%H:%M ${HOSTNAME}%tbuild-job daemon ended ($$)" \
							>> $qdir/8_$qid.msg
	fi
	echo "Queue has been removed. exit now."
	exit 0
fi

for qid ; do
	if [ -f $qdir/$qid.job ] ; then
		if ! mv $qdir/$qid.todo $qdir/$qid.lock 2> /dev/null ; then
			echo "Job locked by other build proc:" \
			     "$qid ($qdir/$qid.lock)!"
			exit 1
		fi

		. $qdir/$qid.job
		date "+Job $qid%t$HOSTNAME ($PPID) since %H:%M %Y-%m-%d"  \
							> $qdir/$qid.lock
		date "+%H:%M ${HOSTNAME}:%tbuilding job '$qid' ($PPID) .." \
							>> $qdir/6_$qid.msg
		pkgloop_package $next
		if [ -f "${build_root}/var/adm/logs/${qid/[^0-9]*=/-}.log" ] ; then
			date "+%H:%M ${HOSTNAME}:%tfinished job '$qid' `
					`(ok)" >> $qdir/3_$qid.msg
		else
			date "+%H:%M ${HOSTNAME}:%tfinished job '$qid' `
					`(ERROR)" >> $qdir/3_$qid.msg
		fi
		rm -f $qdir/$qid.lock $qdir/$qid.job
	else
		echo "No such job: $qid ($qdir/$qid.job)!" ; exit 1
	fi
done