#!/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-PkgList
# 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 ---

nobroken=0

if [ "$1" = "-nobroken" ] ; then
	nobroken=1 ; shift
fi

if [ "$1" -a ! -d architecture/"$1" ] ; then
	echo "Usage: $0 [ -nobroken ] [ <arch-name> ]" ; exit 1
fi

gawk '
BEGIN {
	arch="'$1'";
	nobroken='$nobroken';
}

function print_package() {
	disable=0
	if ( nobroken ) {
		cachefile="package/" pkgtree "/" package "/" package ".cache";
		no_cache_file_found=1;
		while ( (getline line < cachefile) > 0 ) {
			no_cache_file_found=0;
			if ( line ~ /^\[.-ERROR\]/ ) {
				disable=1;
			}
		}
		close(cachefile);
		if ( no_cache_file_found ) disable=1;
	}
	if ( ! disable ) {
		print defset, stages, pri, pkgtree,
		      package, ver, "/" categories flags, 0;
	}
}

END {
	if ( pkgtree ) print_package();
}

( FNR == 1 && pkgtree ) {
	if (!ignore) print_package();
}

FNR == 1 {
	split(FILENAME, a, /\//);
	pkgtree=a[2]; package=a[3];
	ignore = (a[3] ".desc" != a[4]);
	defset="X"; pri="999.999";
	stages="----------";
	ver="0000"; categories=""
	ver_dup=0; flags=""
}

/^\[(P|PRI|PRIORITY)\]/ {
	split($0, a, /[ \t]+/);
	defset=a[2]; stages=a[3]; pri=a[4];
}

/^\[(V|VER|VERSION)\]/ {
	split($0, a, /[ \t]+/);
	if ( ver_dup == 0 ) ver=a[2];
	ver_dup = 1;
}

/^\[(C|CATEGORY)\]/ {
	split($0, a, /[ \t]+/);
	for (c=2; a[c]; c++) categories = categories " " a[c];
}

/^\[(F|FLAGS)\]/ {
	split($0, a, /[ \t]+/);
	for (c=2; a[c]; c++) flags = flags " " a[c];
}

/^\[(R|ARCH|ARCHITECTURE)\]/ {
	split($0, a, /[ \t]+/);
	if ( arch != "" ) {
	    if ( a[2] == "+" ) {
		if ( index($0, arch) == 0 ) pkgtree="";
	    } else {
		if ( index($0, arch) != 0 ) pkgtree="";
	    }
	}
}


' package/*/*/*.desc | sort -k3