# Write out parfile in reverse order
# However keep the sections in same order
function edit(p)  { 
   #print "#...edit(" p "): Category=" C
   if (C == "B") { for (i=1; i<=p; i++) { if (P[i] != "") print P[i]; }} 
   else          { for (i=p; i>=1; i--) { if (P[i] != "") print P[i]; }}
}

/^$/  {p++; next}

{  
    if(p<1) { print; next} 	# Header paragraph
    P[p] = P[p] "\n" $0 
    if ($1 != "%I") next;

    # The catalog identification: c = category
    na = split($2, a, "/"); 
    if (a[1] == "J") { 
        if (a[2] == "other") c = "J/other/" a[3]; 
	else                 c = "J/" a[2];
    }
    else c = a[1];
    #print "#I",$2,"c=" c, "C=" C

    # A new section ==> Print what's already known
    if (c != C) {	# New Section
	edit(p-1);
	P[1] = P[p];
	for (i=2; i<=p; i++) delete P[i];
        p=1; C = c;
    }
}

END { edit(p) }
