#!/bin/bash

# Wait for database connection
dbhost="tmdb"
dbport="3306"
while :
do
	out=`echo "quit" | curl -v telnet://$dbhost:$dbport 2>&1 | grep -c "Connected to $dbhost"`
	if [ $out = 1 ]; then
		echo "\nDatabase connection on $dbhost:$dbport is successfully established"
		break
	fi
	echo -ne "Waiting for database connection on $dbhost:$dbport to be established ..."\\r
	sleep 1
done

cd /opt/amitm/ami/
CONTAINER_ALREADY_STARTED="container_initialised"
if [ ! -e $CONTAINER_ALREADY_STARTED ]; then
    #touch $CONTAINER_ALREADY_STARTED
    echo "-- First container startup , Creating and feeding albkdb/lexicon_kdb --"
	dbname="albkdb"
	lex_dbname="lexicon_kdb"
	./bin/albToolkit -b $dbname create_base -y -u root
	./bin/albToolkit -b $lex_dbname create_base -y -u root
	./bin/albLearner -b $dbname clear_base -y
    echo "-- Learning toolwords --"
	./bin/albLearner -b $dbname -ln 1 -x Databases/NamedEntity/toolwords.dic
    echo "-- Learning firstnames --"
	./bin/albLearner -b $dbname -ln 1 -ct person Databases/NamedEntity/firstnames.dic
    echo "-- Learning locations --"
	./bin/albLearner -b $dbname -ln 1 -l FR -ct loc entity_learn Databases/NamedEntity/pays_fr.dic
	./bin/albLearner -b $dbname -ln 1 -l EN -ct loc entity_learn Databases/NamedEntity/pays_en.dic
    echo "-- Learning firstnames (Arabic) --"
	./bin/albLearner -b $dbname -ln 1  -ct person Databases/NamedEntity/firstnames_ar.dic
    echo "-- Learning locations (Arabic) --"
	./bin/albLearner -b $dbname -ln 1 -l AR -ct loc entity_learn Databases/NamedEntity/pays_ar.dic
    echo "-- Learning firstnames (cyrillic) --"
	./bin/albLearner -b $dbname -ln 1  -ct person Databases/NamedEntity/firstnames_cy.dic
    echo "-- Learning locations (Cyrillic) --"
	./bin/albLearner -b $dbname -ln 1 -l RU -ct loc entity_learn Databases/NamedEntity/pays_ru.dic
else
    echo "-- Not first container startup --"
fi

