#!/bin/sh
#
# Spk-ls - List 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

#
# Functions
#

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

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

$(gettext "List packages or installed files by packages")

$(boldify $(gettext "Options:"))
  --count      $(gettext "Display the number of installed packages")
  --mirror     $(gettext "List all the packages on mirror")
  --extra      $(gettext "List packages on extra mirrors ")
  --blocked    $(gettext "List all blocked packages")
  --short      $(gettext "Short packages list format")
  --modifiers  $(gettext "List package modifiers")
  --root=      $(gettext "Set the root file system path")
  --color=NB   $(gettext "Set package name color in list")
  --debug      $(gettext "Display some useful debug information")

$(boldify $(gettext "Examples:"))
  $name package1 package2 packageN
  $name --short --color=33

EOT
	exit 0
}

#
# Handle --options
#

for opt in $@
do
	case "$opt" in
		*usage|*help) usage ;;
		--count)
			count_installed
			count_mirrored
			exit 0 ;;
		--mirror)
			newline
			boldify $(gettext "Mirror") $(cat $mirrorurl)
			separator
			read_pkgsdesc $pkgsdesc
			separator
			boldify $(count_mirrored)
			newline && exit 0 ;;
		--extra)
				[ -d "$extradb" ] || exit 1
				for extra in $extradb/*
				do
					newline
					boldify $(gettext "Extra mirror")
					if [ ! -f "$extra/packages.desc" ]; then
						echo "URL: $(cat $extra/mirror)"
						gettext "Missing:"; colorize 31" packages.desc"
						continue
					fi
					separator
					read_pkgsdesc $extra/packages.desc
					separator
					boldify $(echo -n $(cat $extra/packages.$SUM | wc -l))
					gettext "packages in:"; echo " $(basename $extra)"
					newline
				done
				exit 0 ;;
		--blocked)
			if [ -s "$blocked" ]; then
				cat $blocked
			else
				gettext "No blocked packages"; newline
			fi
			exit 0 ;;
		--short)
			newline
			echo -n $(boldify $(gettext "Package"))
			indent 28 $(boldify $(gettext "Version"))
			separator
			for pkg in $(ls -1 $installed)
			do
				source_receipt $installed/$pkg/receipt
				echo $(colorize 32 $pkg) $(indent 28 "$VERSION")
			done
			separator
			boldify $(count_installed)
			newline
			exit 0 ;;
		--*) continue ;;
		*)
			# List installed files by the package.
			count=0
			for pkg in $@
			do
				[ -f "$installed/$pkg/files.list" ] || continue
				nb=$(cat $installed/$pkg/files.list | wc -l)
				count=$(($count + 1))
				[ "$count" == 1 ] && newline
				# List modifiers
				if [ "$modifiers" ]; then
					modifiers=$installed/$pkg/modifiers
					if [ -f "$modifiers" ]; then
						boldify $(gettext "Modifiers for") $pkg
						separator
						cat $modifiers
						separator && newline
					else
						echo $(boldify $pkg) $(gettext "package was not modified")
					fi
					continue
				fi
				boldify $(gettext "Installed files by") $pkg
				separator
				cat $installed/$pkg/files.list
				separator
				gettext "Installed files by"; echo -n " $pkg: "
				colorize 32 "$nb" && newline
			done
			exit 0 ;;
	esac
done

#
# Parse all installed pkgs receipt.
#

count=0

newline
boldify $(gettext "Installed packages")
separator
for pkg in $installed/*
do
	unset_receipt
	source_receipt $pkg/receipt
	count=$(($count + 1))
	[ "$count" != 1 ] && newline
	gettext "Package    :"; colorize 32 " $PACKAGE"
	receipt_info
done
separator
boldify $(count_installed) && newline
exit 0
