#!/bin/sh
# Create a database from directories given by user 
# the databases are made at a give directory (should be customizable)
# TODO:
# Use scope to modify the /etc/emacsxx/site-start.d/50remembrance...
# file so they are automatically loaded on startup depending on
# scope, maybe do a sed -e to change 'scope' into the scope given.
PATH=/bin:/usr/bin
package=remembrance-agent
database=/var/lib/emacs/remembrance
program=/usr/bin/ra-index
config_file=/etc/savantrc
emacs_el_file=/usr/share/emacs/19.34/site-lisp/remem-display-mode.el
tmp_file=/tmp/${package}.el

# First, per policy call emacs-install
/usr/lib/emacsen-common/emacs-package-install ${package}


user_input () {
# User input
echo "-------------- Remembrance Agent database building --------------"
echo -e "\n"
echo "In order to work, the Remembrance Agent needs to build a database"
echo "of documents of interest. Please introduce the directories (and/or"
echo "files) to be indexed separated by spaces, afterwards the database will be"
echo "made. If you do not select any directories (default) the database"
echo "will not be made. For more info look at /usr/doc/${package}."
echo -e "\n"
echo "You can do this more than once, the program will stop asking when you"
echo "do not give more directories to index. Also, remembrance-agent only"
echo "works with tree scopes, so DO NOT do this more than three times."
echo -e "\n"
echo "NOTE: If you are installing on a multiuser environment where each user"
echo "may want to have their personal index, DO NOT index user directories"
echo "as superuser since there are no mechanisms to prevent other users from"
echo "invading their privacy. Instead, all users may run ${program} in order"
echo "to build their own databases."
echo -e "\n"
echo -n "Name this scope [none]: "
read scope
echo "NOTE: You can have as many as three scopes, but this installation does "
echo "not yet enable you to do so."
echo -n "Enter directories for Remembrance Agent to index in ${database} [none]: "
read dirs
# Should check here that the user has given only three..
echo -n "Enter directories for Remembrance Agent to exclude [none]: "
read edirs
ONCE=TRUE
}

# First time around call user input
user_input

# Now build the database
if [ ${dirs} ]; then
	echo "Building database in ${database} using ${program}."
	echo "If the file structure is large this will take quite some time"
	echo "and require an important amount of memory and harddisk."
	if [ ! -d ${database}/${scope} ]; then \
		mkdir -p ${database}/${scope}; \
	fi;
	${program} -c ${config_file} ${database}/${scope} ${dirs} -e ${edirs}
	echo -e "\nDatabase FINISHED."
	echo -e "\nModifying ${emacs_el_file}"
	sed -e s/\$DATABASE/${database}/ ${emacs_el_file} >${tmp_file}
	cp ${tmp_file} ${emacs_el_file}
	sed -e s/\$SCOPES/${scope}/ ${emacs_el_file} >${tmp_file}
	cp ${tmp_file} ${emacs_el_file}
	rm ${tmp_file}
else
	echo -e "\nNOT building the database since no directories were specified."
fi


