#!/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/Check-PkgFormat
# 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 ---

if [ "$1" = "-all" ] ; then
	$0 -repository `ls package/. | egrep -v '(CVS|\.svn)'`
	exit 0
fi

if [ "$1" = "-repository" ] ; then
	shift ; for y ; do
		for x in package/$y/[a-z0-9]* ; do
			[ -f "$x/${x##*/}.desc" ] && $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>"
	echo "or     $0 -all"
	exit 1
fi

for package ; do
	lastpdir=""
	for pdir in package/*/$package ; do
		[ -f $pdir/$package.desc ] || continue
		[ "$lastpdir" ] && echo "$package: Found dup: $pdir $lastpdir"
		lastpdir="$pdir"
	done
	if [ -z "$lastpdir" ] ; then
		pdir="..."
	else
		pdir="$lastpdir"
	fi

	if [ -f $pdir/$package.conf ] ; then
		if egrep -q '^[^#]*\bflistdel' $pdir/$package.conf ; then
			echo "$package: Use of \$flistdel is evil!"
		fi
	fi

	if [ -f $pdir/$package.desc ] ; then
		grep '[^ ]' $pdir/$package.desc | \
		egrep -v '^(\[[A-Z0-9-]+\]( |$)|#)' | \
		sed "s,^,$package: Syntax error in $package.desc: ,"

		egrep '^\[[A-Z0-9-]+\]( |$)' $pdir/$package.desc | \
		tr '[]' '||' | cut -f2 -d'|' | \
		while read tag ; do
			grep -q "\\[$tag\\]" Documentation/Developers/$(
					)PKG-DESC-FORMAT && continue
			[ "${tag#X-}" != "$tag" ] && continue
			echo "$package: Unknown tag in $package.desc: [$tag]"
		done

		for x in `egrep '^\[(C|CATEGORY)\]' $pdir/$package.desc | \
		          cut -f2- -d']'` ; do
			egrep -q "^$x( |$)" Documentation/Developers/$(
					)PKG-CATEGORIES && continue
			echo "$package: Unknown package category: $x"
		done

		for x in `egrep '^\[(F|FLAG)\]' $pdir/$package.desc | \
		          cut -f2- -d']'` ; do
			egrep -q "^$x	" Documentation/Developers/$(
					)PKG-FLAGS && continue
			echo "$package: Unknown package flag: $x"
		done

		for x in `egrep '^\[(S|STATUS)\]' $pdir/$package.desc | \
		          cut -f2- -d']'` ; do
			case $x in
				Alpha|Beta|Gamma|Stable) ;;
				*) echo "$package: Unknown package" \
				        "status: $x" ;;
			esac
		done

		for x in `egrep '^\[(L|LICENSE)\]' $pdir/$package.desc | \
		          cut -f2- -d']'` ; do
			case $x in
				Unknown|GPL|LGPL|MPL|FDL|MIT|BSD) ;;
				OpenSource|Free-to-use|Commercial) ;;
				IBM-Public-License) ;;
				*) echo "$package: Unknown package" \
				        "license: $x" ;;
			esac
		done

		grep '^\[.*(\*)$' Documentation/Developers/PKG-DESC-FORMAT |
		sed 's/. ./|/g; s/|\*)//; s/^\[//;' | \
		while read line ; do
			egrep -q "^\[($line)\]" $pdir/$package.desc ||
			echo "$package: No [$line] tag found."
		done

	else
		echo "$package: File not found: $pdir/$package.desc"
	fi

	if [ -f $pdir/$package.conf ] ; then
		bash -n $pdir/$package.conf 2>&1 | sed "s,^,$package: ,"
	fi
done