#!/bin/bash
# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: scripts/Create-PkgList
# Copyright (C) 2004 - 2005 The T2 SDE Project
# Copyright (C) 1998 - 2003 ROCK Linux Project
# 
# 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.
# --- T2-COPYRIGHT-NOTE-END ---

if [ $# -ne 1 -o ! -d architecture/"$1" ] ; then
	echo "Usage: $0 [ <arch-name> ]"
	[ $# -eq 0 ] ||
	echo "       '$1' is not a valid architecture"
	exit 1
fi 1>&2

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

function print_package() {
	print defset, stages, pri, pkgtree,
	      package, ver, "/" categories flags, 0;
}

END {
	if ( pkgtree ) print_package();
}

( FNR == 1 && pkgtree ) {
	print_package();
}

FNR == 1 {
	split(FILENAME, a, /\//);
	pkgtree=a[2]; package=a[3];
	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)\]/ {
	n=split($0, a, /[ \t]+/);
	#if (arch == "")
	#  next;

	# search for an architecture match
	if ( a[2] == "-" ) {
	  for ( i = 3; i<= n; ++i) {
		if ( arch == a[i] ) {
		  pkgtree=""
		  next;
		}
	  }
	  # fall thru: keep it
	}
	else { # intentionally start at index 2: implicit exlusive selection
	  for ( i = 2; i<= n; ++i) {
		if ( arch == a[i] ) {
		  next;
		}
	  }
	  # not found: remove it
	  pkgtree="";
	}
}

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