#!/bin/bash
#
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: scripts/Check-PkgFormat
# Copyright (C) 2006 - 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
#
# 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.
# --- SDE-COPYRIGHT-NOTE-END ---

if [ "$1" = "-all" ] ; then
	$0 -repository `ls package/. | egrep -v '(CVS|\.svn|\.git)'`
	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\\]" etc/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( |$)" etc/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	" etc/flags &&
				continue
			echo "$package: Unknown package flag: $x"
		done

		for x in `egrep '^\[(S|STATUS)\]' $pdir/$package.desc |
		          cut -f2- -d']'` ; do
                        egrep -q "^$x" etc/status &&
                                continue
                        echo "$package: Unknown package status: $x"
		done

		for x in `egrep '^\[(L|LICENSE)\]' $pdir/$package.desc |
		          cut -f2- -d']'` ; do
                        egrep -q "^$x" etc/licenses &&
                                continue
                        echo "$package: Unknown package license: $x"
		done

		grep '^\[.*(\*)$' etc/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