#!/bin/sh
# Queries the database index made by ra.
# needs dialog or gdialog
TMP_RESULT=`tempfile`
if [ -e $TMP_RESULT ]; then
	rm -rf $TMP_RESULT | exit 1
fi
# Not needed for arrow
#CONF_FILE=/etc/savantrc
DIALOG=/usr/bin/dialog
if [ -x /usr/bin/gdialog ]; then
	DIALOG=/usr/bin/gdialog
fi
if [ ! -x $DIALOG ]; then
	echo Dialog is not installed. You need to install it in order to run dpkg-query 
	exit 1
fi
#DPKG_INDEX_DIR=/tmp/index.db
DPKG_INDEX_DIR=/var/lib/dpkg/index.db


# Default values
maxq=10 # maximum number of results to present "Top Ten"

help () {

	echo -e " 
dpkg-query is a proof of concept code which is not even in beta stage.  
dpkg-query tells you which Debian package provides the software you are 
looking for using language processing tools) you just type in your needs 
and it will list you what can you install in order to fulfill it. 

To test it, go to \"query\" and then type in a query in natural language. 
For example \"multiplayer games that use ships\". The backend  
will process this query against the database build previously using 
word vectorization mechanisms in order to find the most appropiate 
package. This processing takes into account the information added 
into the description of the package (currently man pages, HTML files 
and other text included in packages is not considered) and correlates it 
with the given query. 

The program returns a list of the top ten (the number of results can 
be customized through the \"maxq\" option) packages that comply with the
query. That is, installing the packages show you have what you asked 
for. The number shown next to the package name is the \"closeness\" to 
the query you made, the closer it is to 1 the closer it is to what you 
asked for. 


Cyrrently the backend used is the Library Bag of Words, (although 
Remembrance Agent could be used too) however, vectorization has not been 
throughly tested for optimisation and it does not use all the options offered 
in the arrow program in order to minimize error. 

Send comments and ideas to jfs@computer.org " >>$TMP_RESULT
	$DIALOG --textbox $TMP_RESULT 15 75
}

ask_keyword () {
	$DIALOG --title "Enter keyword" \
                --inputbox "Enter one or more keywords for the query" \
		10 40 2>$TMP_RESULT
}

ask_number () {
	$DIALOG --title "Enter number" \
		--inputbox "Enter the number desired for the value of $value (currently $number)" \
		10 40 2>$TMP_RESULT
}

ask_string () {
	$DIALOG --title "Enter string" \
	       --inputbox "Enter string for query" \
	       10 40 2>$TMP_RESULT
}

execute_query () {
		ask_string
		query=`cat $TMP_RESULT`
		if [ ! -z "$query" ]; then
		# This is for remembrance-agent
 		#	echo -e "query $maxq\n$query\n." | ra-retrieve -c $CONF_FILE $DPKG_INDEX_DIR >$TMP_RESULT
		# For version 2.0.1
		#echo -e "query $maxq\n$query\n" | ra-retrieve -c $CONF_FILE $DPKG_INDEX_DIR >$TMP_RESULT

		#This is for arrow
		echo -e "$query\n" | arrow --data-dir $DPKG_INDEX_DIR -n $maxq -q >$TMP_RESULT
		$DIALOG --textbox $TMP_RESULT 15 75
		fi

}

execute_keyword () {

		ask_keyword
		keys=`cat $TMP_RESULT`
		if [ ! -z "$keys"  ]; then
 		#	echo -e "keyquery $maxq\n$keys\n." | ra-retrieve -c $CONF_FILE $DPKG_INDEX_DIR >$TMP_RESULT
		# For version 2.0.1
		# This is for remembrance-agent
		# echo -e "keyquery $maxq\n$keys\n" | ra-retrieve -c $CONF_FILE $DPKG_INDEX_DIR >$TMP_RESULT
		#This is for arrow
		echo -e "$keys\n" | arrow --data-dir $DPKG_INDEX_DIR -n $maxq -q >$TMP_RESULT
		$DIALOG --textbox $TMP_RESULT 15 75
		fi
}



$DIALOG --infobox "Dpkg package database query" 3 40
sleep 1

while [ -z "$quit" ] ; do
	$DIALOG  --menu "Select action" 15 60 10 \
		help "What is this?" \
		query "Do a query in the database by string" \
		keyword "Do a query by keywords" \
		maxq "Select number of results to show" \
		remake "Rebuild the database (only ROOT can)" \
		quit "Quit the program" 2>$TMP_RESULT
	result=`cat $TMP_RESULT`
	case $result in
		"query")  execute_query
			;;
		"keyword") execute_keyword
			;;	
		"maxq") value="maximum number of answers to show"
			number=$maxq
			ask_number
			number=`cat $TMP_RESULT`
			if [ ! -z "$number" ]; then maxq=$number; fi
			;;
		"remake") dpkg-make-index 2>&1 >$TMP_RESULT
			$DIALOG --textbox $TMP_RESULT 15 75
			;;
		"help")	help
			;;
		"quit" | "") quit=yes
			;;
	esac
done

#$DIALOG --title "End of the program" --msgbox "Exiting the program" 5 30

rm -rf $TMP_RESULT

exit  0


