#!/bin/bash
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# Filename: scripts/Create-PkgQueue
# Copyright (C) 2006 - 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
# 
# More information can be found in the files COPYING and README.
# 
# 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; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

config=default
logdir=
single=0
debug=0
nobrokendeps=0

while [ "$1" ] ; do
	case "$1" in
	    -cfg)
		config=$2 ; shift ; shift ;;
	    -single)
		single=1  ; shift ;;
	    -logdir)
		logdir=$2 ; shift ; shift ;;
	    -debug)
		debug=1   ; shift ;;
	    -nobrokendeps)
		nobrokendeps=1 ; shift ;;
	    *)
		echo "Usage: $0 [ -cfg config ] [ -single ] \\"
		echo "       ${0//?/ } [ -logdir logdir ] [ -debug ]"
		exit 1 ;;
	esac
done

. ./lib/parse-config

gawk '
function check_package() {
	split(pkgline, a); dep=a[2];
	if ( a[1] != "X" || ! index(a[2], stagelevel) ) return;

	repository = a[4];
	package = a[5];

	logfile = logfile_tpl;
	gsub("<stagelevel>", stagelevel, logfile);
	gsub("<repository>", repository, logfile);
	gsub("<package>",    package,    logfile);

	errfile = logfile; outfile = logfile;
	gsub("<log>", "log", logfile);
	gsub("<log>", "err", errfile);
	gsub("<log>", "out", outfile);

	
	if ( (getline dummy < logfile == -1) &&
	     (getline dummy < errfile == -1) ) {
		build_this_package = 1;
		found_dependencies = 0;
		buildtime = 60;
		priority = 0;

		if ( getline dummy < outfile != -1 )
					build_this_package = 0;

		if ( index(not_present, " " package " ") ) {
			if (debug && build_this_package)
				print "DEBUG: Not building " stagelevel "-" \
				      package ": earlier stages not done " \
				      "for this package."
			build_this_package = 0;
		}

		if ( build_this_package && (package in database) ) {
			split(database[package], a);
			buildtime = a[2];
			priority = a[3];

			for (c in a) {
				# if ( a[c] == package  ) continue;
				# if ( strtonum(c) <= 3 ) continue;
				if (debug && 0)
					print "DEBUG: Checking " stagelevel \
					      "-" package ": needs " a[c];
				if ( index(not_present, " " a[c] " ") &&
				     stagelevel != stage9_no_deps ) {
					if (debug && build_this_package)
						print "DEBUG: Not building " \
						      stagelevel "-" package \
						      ": " a[c] " is missing";
					build_this_package=0;
				}
				found_dependencies = 1;
			}
		}

		# Do not build packages parallel in stage 0
		#
		if ( stagelevel == 0 && not_present != "")
			build_this_package = 0;

		# Do not build packages from stages > 0 if packages
		# from stage 0 are still missing
		#
		if ( stagelevel == 0 )
			stage0_not_present=1;
		if ( stagelevel > 0 && stage0_not_present )
			build_this_package = 0;

		# Do not build packages from stages >= 2 if packages
		# from stages <= 1 are not build already.
		#
		if ( stagelevel <= 1 )
			stage01_not_present=1;
		if ( stagelevel >= 2 && stage01_not_present )
			build_this_package = 0;

		# Only ignore deps in stage 9 if everything < stage 9
		# is already there
		if ( stagelevel < 9 )
			stage9_no_deps = "x";

		# A packages without dependencies automatically depend
		# on all packages build before it
		if (found_dependencies == 0 && not_present != "" &&
		    stagelevel != stage9_no_deps)
			build_this_package = 0;

		# sde-debug must be build _after_ all other packages
		if (package == "sde-debug" && not_present != "")
			build_this_package = 0;

		if (build_this_package) {
			sub("^X ", priority " ", pkgline);
			sub(" 0$", " " buildtime, pkgline);
			print stagelevel, pkgline;
			if (single) exit 0;
		}

		not_present = not_present " " package " ";
	}
	else
	if ( nobrokendeps )
	{
		close(errfile);
		if ( (getline dummy < errfile != -1) ) {
			not_present = not_present " " package " ";
		}
	}

	close(logfile); # ignore errors here if we
	close(errfile); # did not open this files
	close(outfile);
	close(depfile);
}

BEGIN {
	not_present=;
	stage0_not_present=0;
	stage01_not_present=0;
	stage9_no_deps=9;

	nobrokendeps='$nobrokendeps';
	config="'$config'"; single='$single'; debug='$debug';
	logdir="'"${logdir:-build/$SDECFG_ID/var/adm/logs}"'";

	logfile_tpl   = logdir "/<stagelevel>-<package>.<log>";
	#depdb_file    = "scripts/dep_db.txt";
	packages_file = "config/" config "/packages";

	#while ( (getline depline < depdb_file) > 0 )
	#	{ $0 = depline; sub(/:/, "", $1); database[$1]=$0; }
	#close(depdb_file);

	for (stagelevel=0; stagelevel<=9; stagelevel++) {
		while ( (getline pkgline < packages_file) > 0 ) {
			check_package();
		}
		close(packages_file);
	}
}
'