#!/bin/sh
#
# List installed packages in a suitable format for GTK tree.
# List mirrored packages using the packages.desc file.
#

undigest_list()
{
	IFS="|"
	if [ -n "$1" -a "$1" != "all" ]; then
		cat undigest/$1/packages.desc
	else
		cat undigest/*/packages.desc
	fi 2> /dev/null | sort | while read PACKAGE VERSION SHORT_DESC; do
		ICON=tazpkg
		PACKAGE=${PACKAGE%% *}
		if [ -d installed/$PACKAGE ]; then
			[ "$2" == "installable" ] && continue
			ICON=tazpkg-installed
			if grep -qs "^$PACKAGE$" blocked-packages.list; then
				ICON=stop
			fi
		else
			[ "$2" == "installed" ] && continue
		fi
		[ "$2" == "blocked" -a "$ICON" != "stop" ] && continue
		[ "$2" == "upgradeable" ] && 
		! grep -q ^$PACKAGE$ upgradeable-packages.list && continue
		echo "$ICON|$PACKAGE|$VERSION|$SHORT_DESC"
	done
	unset IFS
}

installable_list()
{
	local cache
	cache=packages.installable_list.$CAT
	if [ -s $cache -a $cache -nt packages.desc -a $cache -nt installed ]; then
	     cat $cache
	     return
	fi
	IFS="|"
	cat packages.desc undigest/*/packages.desc 2> /dev/null | sort | \
	while read PACKAGE VERSION SHORT_DESC CATEGORY; do
		# Check first for category for more speed.
		CATEGORY=${CATEGORY%| *}
		ICON=tazpkg
		[ $CAT == all -o $CATEGORY == " $CAT " ] || continue
		[ -d installed/${PACKAGE%% *} ] && continue
		grep -qs "^$PACKAGE" undigest/*/packages.desc && ICON=add
		echo "$ICON|$PACKAGE|$VERSION|$SHORT_DESC"
	done | tee $cache
	unset IFS
}

installed_list()
{
	for pkg in ${1}installed/*
	do
		[ -n "$1" -a -s installed/$(basename $pkg)/receipt ] && continue
		. $pkg/receipt
		ICON=tazpkg-installed
		[ $CAT == all -o $CATEGORY == $CAT ] || continue
		if [ -n "$1" -o -L $pkg ]; then
			ICON=media-flash
		else
			grep -qs "^$PACKAGE" undigest/*/packages.desc && ICON=add
		fi
		grep -qs "^$PACKAGE$" blocked-packages.list && ICON=stop
		echo "$ICON|$PACKAGE|$VERSION|$SHORT_DESC"
	done
}

all_list()
{
	local cache
	cache=packages.all_list.$CAT
	if [ -s $cache -a $cache -nt packages.desc -a $cache -nt installed ]; then
	     cat $cache
	     return
	fi
	( installable_list ; installed_list ) | sort -t \| -k 2 -u | tee $cache
}

blocked_list()
{
	ICON=tazpkg-installed
	[ "$1" = "blocked" ] && ICON=stop
	for pkg in $(cat $1-packages.list 2> /dev/null); do
		[ -f installed/$pkg/receipt ] || continue
		. installed/$pkg/receipt
		[ $CAT == all -o $CATEGORY == $CAT ] || continue
		AVAILABLE=$(grep -s "^$pkg " packages.desc \
			undigest/*/packages.desc | awk '{ print $3 }')
		echo "$ICON|$PACKAGE|$VERSION (Available: $AVAILABLE)|$SHORT_DESC"
	done
}

cd /var/lib/tazpkg
CAT=`cat /tmp/tazpkgbox/category`
case $1 in
	all)
		STATUS=`cat /tmp/tazpkgbox/status`
		case $STATUS in
			blocked|upgradeable)
				blocked_list $STATUS;;
			linkable)
				[ -d fslink ] && installed_list \
					$(readlink fslink)/var/lib/tazpkg/;;
			installed)
				installed_list ;;
			installable)
				installable_list ;;
			*)
				all_list ;;
		esac ;;
	undigest)
		set -- `cat /tmp/tazpkgbox/undigest-category`
		if [ "$1" == "all" -o "$1" == "" ]; then
			undigest_list $2 $3
		else
			undigest_list $2 $3 | grep "$1"
		fi ;;
	blocked|upgradeable)
		blocked_list $1;;
	*)
		echo "Usage: $0 [all|undigest|blocked|upgradeable]" ;;
esac

exit 0
