#!/bin/sh
#
# Spk-find - Find/search SliTaz packages and files. Read the README before
# adding or modifying any code in spk!
#
# Copyright (C) SliTaz GNU/Linux - BSD License
# Author: See AUTHORS files
#
. /usr/lib/slitaz/libspk.sh

#
# Quick coded tool to have a simple find/search packages tool for people 
# using spk toolkit. TODO: find files, find in pkg desc for pattern, display
# status (installed or not).
#

find="$1"
cfile="/tmp/spk/count"
mkdir -p $(dirname $cfile)

#
# Functions
#

# Help and usage
usage() {
	name=$(basename $0)
	cat << EOT

$(boldify $(gettext "Usage:")) $name [package|--options]

$(boldify $(gettext "Options:"))
  --short      $(gettext "Short packages list format")

EOT
	exit 0
}

case "$1" in
	*help|usage) usage && exit 0 ;;
esac

newline
boldify $(gettext "Spk find: $find")
separator

# --files or packages
if [ "$files" ]; then
	type="Files"
	echo "TODO"
else
	type="Packages"
	IFS="|"
	grep "$find" $pkgsdesc | while read pkg vers desc null
	do
		echo "$pkg" >> ${cfile}
		echo "$(colorize 34 $pkg)${vers# }"
		[ "$short" ] || echo "  $desc"
	done
	unset IFS
fi

# Sep and stats
if [ -f "$cfile" ]; then
	count=$(cat $cfile | wc -l)
	color=32
else
	count=0
	color=31
fi
separator
boldify "$type found: $(colorize $color $count)" && newline

rm -rf ${cfile}
exit 0
