#!/bin/sh
#
# This script provide a suitable output for the search results.
#

# Clean preview results.
rm -f /tmp/tazpkgbox/search-installed
touch /tmp/tazpkgbox/search-installed

list_files()
{
	sed 's/.\[[01]m//g' | awk 'BEGIN { show=0 } {
		if (/^===/) show=1-show;
		else if (/^Package/) pkg=$2;
		else if ($0 != "" && show != 0) printf("%s||%s\n",pkg,$0);
	}'
}

if [ "$1" = "--files" ]; then
	tazpkg search-file "$SEARCH" | list_files > \
		/tmp/tazpkgbox/search-installed
	tazpkg search-file "$SEARCH" --mirror | list_files > \
		/tmp/tazpkgbox/search-mirrored
	exit 0
fi

# Search installed.
for i in `ls /var/lib/tazpkg/installed`
do
	. /var/lib/tazpkg/installed/$i/receipt
	if echo "$PACKAGE $SHORT_DESC" | grep -q "$SEARCH"; then
		echo "$PACKAGE|$VERSION|$SHORT_DESC" >> /tmp/tazpkgbox/search-installed
	fi
done

# Search mirrored.
grep "$SEARCH" /var/lib/tazpkg/packages.desc > /tmp/tazpkgbox/search-mirrored

exit 0
