#!/bin/sh

SBOX_DIR=/scratchbox
# inside/outside scratchbox
if [ "#EMPTY#$_SBOX_DIR" = "#EMPTY#" ] ; then
    $(dirname "$0")/../../login $SBOX_DIR/devkits/$(basename `dirname $0` )/`basename $0` "$*"
    exit $?
fi


PKG_INFO_DIR="deb_list"

SB_DEVKIT_ROOT="$SBOX_DIR/devkits"
SB_COMPILER_ROOT="$SBOX_DIR/compilers"
SB_AUTOTOOLS_ROOT="$SBOX_DIR/tools/autotools"
SB_CONF="$SBOX_DIR/tools/bin/sb-conf"
SB_INFO_DIRS="$SBOX_DIR/tools/$PKG_INFO_DIR/"

function parse_sb_config
{
	if [ -d /host_usr/$PKG_INFO_DIR ]; then
		SB_INFO_DIRS="/host_usr/$PKG_INFO_DIR/ $SB_INFO_DIRS"
	fi


	local target="$1"

	if [ "#EMPTY#$target" == "#EMPTY#" ] ; then
		target=`$SB_CONF current | tr -d '\n'`
	fi

	local devkits=
	local compilers=
	if [ "$target" != "__all" ] ; then
		if [ -d "/targets/${target}_$PKG_INFO_DIR" ] ; then
			SB_INFO_DIRS="/targets/${target}_$PKG_INFO_DIR/ $SB_INFO_DIRS"
		fi

		devkits="`$SB_CONF show $target --devkits`"
		if [ "#EMPTY#$devkits" != "#EMPTY#" ]; then
			if [ "$devkits" == "none" ] ; then
				devkits=
			fi
		fi

		compilers="`$SB_CONF show $target --compiler`"
	else
		devkits="`$SB_CONF list --devkits 2> /dev/null`"
		compilers="`$SB_CONF list --compilers 2> /dev/null`"
	fi

	local devkit=
	for devkit in $devkits; do
		SB_INFO_DIRS="$SB_INFO_DIRS $SB_DEVKIT_ROOT/$devkit/$PKG_INFO_DIR/"
	done

	local compiler=
	for compiler in $compilers ; do
		SB_INFO_DIRS="$SB_INFO_DIRS $SB_COMPILER_ROOT/$compiler/$PKG_INFO_DIR/"
	done

	SB_INFO_DIRS="$SB_INFO_DIRS `find $SB_AUTOTOOLS_ROOT -mindepth 2 -maxdepth 2 -name $PKG_INFO_DIR -type d`"
}

function list_all
{
	(for file in `find $SB_INFO_DIRS -type f 2>/dev/null`; do
		local ver=`grep '^Version: ' $file | cut -d' ' -f2`
		for pkg in `grep '^Binary: ' $file | cut -d' ' -f2-`; do
			echo "$pkg=$ver"
		done
	 done
	) | sort -u

	exit
}

function list_complete
{
	(for file in `find $SB_INFO_DIRS -type f 2>/dev/null`; do
		local ver=`grep '^Version: ' $file | cut -d' ' -f2`
		local source=`grep '^Source: ' $file | cut -d' ' -f2`
		for pkg in `grep '^Binary: ' $file | cut -d' ' -f2-`; do
cat <<EOF
Package: $pkg
Status: install ok unpacked
Priority: optional
Section: virtual
Installed-Size: 1
Maintainer: Scratchbox Developers <scratchbox-devel@lists.scratchbox.org>
Architecture: all
Source: $source 
Version: $ver.sbox1
Description: $pkg from Scratchbox
 This is $pkg, a virtually generated package. Functionality of this package
 is provided by Scratchbox
 .
 origin: $file

EOF
		done
	 done
	) 
	exit 0
}

function list_sources
{
	(for file in `find $SB_INFO_DIRS -type f 2>/dev/null`; do
		local src=`grep '^Source: ' $file | cut -d' ' -f2`
		local ver=`grep '^Version: ' $file | cut -d' ' -f2`
		echo "$src=$ver"
	 done
	) | sort -u

	exit
}

function pkg_info
{
	local PACKAGE=$1
	local found=false

	for file in `find $SB_INFO_DIRS -type f 2>/dev/null` ; do
		if (grep '^Binary: ' $file | tr -d $'\n' ; echo -n ' ') | grep -q " $PACKAGE "; then
			cat $file
			found=true
			break
		fi
	done

	if ! $found ; then
		exit_error "Package not found: $PACKAGE"
	fi

	exit 0
}

function source_info
{
	local PACKAGE=$1
	local found=false

	for file in `find $SB_INFO_DIRS -type f 2>/dev/null` ; do
		if grep -q "^Source: $PACKAGE$" $file; then
			cat $file
			found=true
			break
		fi
	done

	if ! $found ; then
		exit_error "Package not found: $PACKAGE"
	fi
	
	exit 0
}

function exit_error
{
	echo >&2 "$0: $@"
	exit 1
}

function usage
{
	local error=$1

	cat <<EOF
Lists Debian packages provided by Scratchbox

Usage: $0 [-h] [-l] [-s] [-a] [-i pkg] [-f pkg] [-t target]

	-h         show this help
	-l         list all packages
	-c         list all packages in complete format
	-s         list all sources used
	-a         list available packages
	-t         list packages from a target (default: current target) 
	-i <pkg>   show info about package
	-f <pkg>   show info about source package

EOF
	exit $error
}

TARGET=
ACTION=
PKG=

while getopts alhcsi:f:t: opt ;do
	case $opt in
		h) usage 0;;
		l) ACTION='LIST-ALL' ;;
		s) ACTION='LIST-SOURCES' ;;
		c) ACTION='LIST-COMPLETE' ;;
		i)
			ACTION='INFO-PACKAGE'
			PKG="$OPTARG"
			;;
		f)
			ACTION='INFO-SOURCE'
			PKG="$OPTARG"
			;;
		t) TARGET="$OPTARG" ;;
		a) TARGET='__all' ;;
		?) usage 1;;
	esac
done

parse_sb_config "$TARGET"

case "$ACTION" in
	LIST-ALL) list_all ;;
	LIST-SOURCES) list_sources ;;
	LIST-COMPLETE) list_complete ;;
	INFO-SOURCE) source_info "$PKG" ;;
	INFO-PACKAGE) pkg_info "$PKG" ;;
	*) usage 1 ;;
esac

