#!/bin/bash
DIR=$(dirname "${BASH_SOURCE[0]}")

MIRAFINDBIN="$DIR/../../../bin/mirafind"

function findPackage()
{
	res=$($MIRAFINDBIN --print-cache | sed -n -e "s/^$1 //p")
	echo "$res"
}

function reindex()
{
	$MIRAFINDBIN -p > /dev/null
}

if [[ $1 == "--reindex" ]];
then
	reindex
	exit 0
fi

if [[ -z "$1" ]];
then
	$MIRAFINDBIN --print-cache
else
	res=$(findPackage $1)
	if [[ -n "$res" ]];
	then
		# package found
		echo $res
	else
		# package not found, need reindex
		reindex
		res=$(findPackage $1)
		if [[ -n "$res" ]];
		then
			# package found
			echo $res
		else
			# package not found
			exit 255
		fi
	fi
fi





