#!/bin/sh

# Parameters: <pkglist from mkimage profile> <available pkglist>
fprofilelist=$1
fpkgnames=`mktemp /tmp/pkgchecker.names.XXXXX`
fpkgwildcards=`mktemp /tmp/pkgchecker.wildcards.XXXXX`
favaillist=$2
ftemp=`mktemp /tmp/pkgchecker.XXXXX` 

# cleaning pkg list from comments, emty lines, replacing * with '.*' for wildcards
# splitting several pkgnames on the one line
sed -e '/^#/d' -e '/^$/d' -e 's/ \+$//' -e 's/ \+/\n/g' < $fprofilelist | sort -u > $ftemp # got list of pkg names we need

# split pkgnames without wildcards and with wildcards
fgrep -v '*' $ftemp > $fpkgnames
fgrep 	 '*' $ftemp > $fpkgwildcards
sort $favaillist > $ftemp

# return not available packages
comm -2 -3 $fpkgnames $ftemp 

# return not available wildcards
for i in `cat $fpkgwildcards`; do
	# replacing * with regexp's \.+
	pattern="^`echo $i | sed -e 's/\*/.\\\\+/'`$"
	if ! grep "$pattern" $favaillist >/dev/null; then
		echo "$i"
	fi
done

# cleaning up
rm $ftemp $fpkgnames $fpkgwildcards
