#!/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/Create-DepDB
# ROCK Linux is Copyright (C) 1998 - 2003 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 ---

cachedir="package"
descdir="package"
debug=''

while [ "$1" ] ; do
	case "$1" in
	    -cachedir)
		cachedir=$2 ; shift ; shift ;;
	    -descdir)
		descdir=$2  ; shift ; shift ;;
	    -debug)
		debug=$2    ; shift ; shift ;;
	    *)
		echo "Usage: $0 [ -cachedir cachedir ] [ -descdir descdir ] \\"
		echo "       ${0//?/ } [ -debug pkg ] > filename"
		exit 1 ;;
	esac
done

echo -n "Creating dependency database ..." >&2

sedscript1=`mktemp` ; sedscript2=`mktemp`
while read package command args
do
	package=${package%.desc:*}
	package=${package##*/} ; set -- $args
	case "$command" in
	    group)
		echo -e "/^$package\$/ a \\\\\n$*" >> $sedscript1
		echo -e "/^$*\$/ a \\\\\n$package" >> $sedscript2
	esac
done < <( egrep '^\[(E|DEP|DEPENDENCY)\]' $descdir/*/*/*.desc )

if [ -f create_depdb.cache ] ; then
	cat create_depdb.cache
else
	for x in $cachedir/base/*/*.cache ; do
		y=${x%/*.cache}   ; y=${y##*/}
		z=${x%/*/*.cache} ; z=${z##*/}

		priority=$( egrep '^\[(P|PRI|PRIORITY)\] ' \
						$descdir/$z/$y/$y.desc | \
		            tr '\t' ' ' | tr -s ' ' | cut -f3,4 -d' ' | \
		            sed 's,9 , ,; s,-,,g; s,.*\(.\) ,\1.,' )
		buildtime=$( grep '^\[BUILDTIME\] ' $x | cut -f2 -d' ' )
		[ "$buildtime" ] || continue

		deps=$( grep '^\[DEP\] ' $x | cut -f2- -d' ' | tr ' ' '\n' | \
			sed -f $sedscript1 | sed -f $sedscript2 | \
			sort -u | grep -vx $y )

		echo $y $buildtime $priority $deps $y

	done | if [ "$debug" ] ; then tee create_depdb.cache ; else cat ; fi
fi | gawk '

function getpri(package) {
	datafile="'$tmp1'";

	delete todo;
	delete done;
	todo[ package ] = 1;
	returncode=0;
	endloop=0;
	level=0;

	if (debug != "") {
		print "";
		print "Creating priority for " package " ...";
	}

	while ( ! endloop ) {
	    endloop=1; level++;
	    for (pkg in todo) {
		endloop=0;
		delete todo[pkg];
		done[pkg] = 1;
		firstdebug=1;

		for (nextpkg in database) {
		    if ( index(database[nextpkg], " " pkg " ") ) {
			if ( ! (nextpkg in done) &&
			     ! (nextpkg in todo) &&
			     ! (nextpkg in ignore) ) {
				if ("x" orderdb[nextpkg] < "x" orderdb[pkg]) {
				    if (debug != "") {
					if (firstdebug)
					    print "\n\t(" level ") Found " \
					          "dependencies for " pkg ":";
					print "\t\t" pkg ": " \
					      "required by " nextpkg \
					      " (ignore reverse dep)";
					firstdebug=0;
				    }
				} else {
				    if (debug != "") {
					if (firstdebug)
					    print "\n\t(" level ") Found " \
					          "dependencies for " pkg ":";
					print "\t\t" pkg ": " \
					      "required by " nextpkg;
					firstdebug=0;
				    }
				    todo[nextpkg] = 1;
				    returncode++;
				}
			}
		    }
		}
	    }
	}

	return returncode;
}

BEGIN {
	counter=0;
	debug="'$debug'";
}

{
	orderdb[$1]=$3; $3=0;
	database[$1]=$0;
	if (NF > 102) {
		printf "\rPackage %s has %d dependencies. I don'\''t " \
		       "believe this.\n", $1, NF - 3 > "/dev/stderr";
		ignore[$1] = 1;
	}
	counter++;
}

END {
	if (debug != "") {
		pri=getpri(debug);
		print "\nResulting Priority: " pri;
	} else {
		for (package in database) {
			printf "\rCreating dependency database (" \
			       counter ") ... \b" > "/dev/stderr";
			$0 = database[package];   $3 = getpri(package);
			$1 = $1 ":"; print;       counter--;
		}

		print "\rCreating dependency database ... " \
		      "done." > "/dev/stderr";
	}
}
' | if [ "$debug" ] ; then cat ; else sort ; fi

rm -f $sedscript1
rm -f $sedscript2

