#!/bin/sh -e
#
# missing-in-tree - List items present in libraries but not in the tree
#
# Copyright 2012 by Werner Almesberger
#
# 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.
#


usage()
{
	echo "usage: $0 [-F] [-L libdir ...] [-l lib ...] hierarchy" 1>&2
	exit 1
}


scan_comp()
{
	for n in "$@"; do
		sed '/^DEF ~\?/{s///;s/ .*//;p;};d' <$n >>_tmp2
	done
}


scan_fped()
{
	for n in "$@"; do
		fped -k $n - | sed '/^\$MODULE /s///p;d' >>_tmp2
	done
}


trap "rm -f _tmp1 _tmp2" 0

gencat -D "$@" >_tmp1 || exit

ext=.lib

>_tmp2
while [ "$1" ]; do
	case "$1" in
	-F)	ext=.fpd;;
	-L)	shift
		[ "`echo \"$1\"/*.$ext`" = "$1/*.$ext" ] ||
		    scan_lib "$1"/*.$ext;;
	-l)	shift
		if [ $ext = .lib ]; then
			scan_comp "$1"
		else
			scan_fped "$1"
		fi;;
	-*)	usage;;
	*)	break;;
	esac
	shift
done

[ "$1" ] || usage
[ -z "$2" ] || usage

cat _tmp1 _tmp1 _tmp2 | sort | uniq -u

exit 0