#!/usr/bin/env bash
#
# --- T2-COPYRIGHT-NOTE-BEGIN ---
# T2 SDE: scripts/Create-DescPatch
# Copyright (C) 2004 - 2023 The T2 SDE Project
# Copyright (C) 1998 - 2003 ROCK Linux Project
# 
# This Copyright note is generated by scripts/Create-CopyPatch,
# 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 version 2.
# --- T2-COPYRIGHT-NOTE-END ---

if [ "$1" = "-repository" ]; then
	shift; for y; do
		for x in package/$y/[a-z0-9]*; do $0 ${x##*/}; done
	done
	exit 0
fi

if [ "$1" != "${1#-}" -o $# -eq 0 ]; then
	echo "Usage: $0 <package-names>"
	echo "or     $0 -repository <repository-names>"
	exit 1
fi

for package; do
	lastpdir=""
	for pdir in package/*/$package; do
		[ "$lastpdir" ] && echo "$package: Found dup: $pdir $lastpdir"
		lastpdir="$pdir"
	done

	if [ -f $pdir/$package.desc ]; then
		tempfn=`mktemp`
		{ echo; nl=0
		while read line; do
			if [ "$line" = "--" -a "$nl" = 0 ]; then
				echo; nl=1
			fi
			if [ "${line#\[}" != "$line" ]; then
			    for x in $line; do
				x="`echo $x | tr -d '[]'`"
				grep "^\[$x\]" $pdir/$package.desc && nl=0
			    done
			fi
		done < misc/share/PKG-DESC-FORMAT
		if [ "$nl" = 0 ]; then echo; fi
		grep "^\[X" $pdir/$package.desc && echo
		} > $tempfn
		diff -u ./$pdir/$package.desc $tempfn
		rm -f $tempfn
	fi
done