#!/bin/sh

genus=$1
species=$2

if [ -d /Library/Caches/Genomes ]; then
    repository=/Library/Caches/Genomes
fi
if [ -d /var/cache/genomes ]; then
    repository=/var/cache/genomes
fi
if [ ! -d $repository ]; then
    echo "Can't find genome repository."
    exit 1
fi
scriptdir=`pwd`

organism=${genus}_${species}

make
cd $repository/$organism

echo "Creating empty database '$organism'."
sudo mysql -e "drop database if exists $organism"
sudo mysql -e "create database $organism"
username=`id -un`
sudo mysql -e "grant all on $organism.* to $username@localhost"
sudo mysql $organism -e "source $scriptdir/empty.sql"

echo Loading GenBank file headers.
$scriptdir/loadhdr $organism *.gbk*

echo Saving GenBank file headers.
option="--compatible=mysql323"
(mysqldump $option $organism || mysqldump $organism) > $organism.hdr
echo >> $organism.hdr

echo Converting feature data.
$scriptdir/gbksql $organism *.gbk* > $organism.ftr

echo Loading feature data.
mysql $organism -e "source $organism.ftr"

echo Compiling bioentry index and gene index.

feature_ontology=`mysql $organism -B -N -e "select ontology_id from ontology where name='SeqFeature Keys'"`
annotation_ontology=`mysql $organism -B -N -e "select ontology_id from ontology where name='Annotation Tags'"`
gene_term=`mysql $organism -B -N -e "select term_id from term where name='gene' and ontology_id='$feature_ontology'"`
locus_tag_term=`mysql $organism -B -N -e "select term_id from term where name='locus_tag' and ontology_id='$annotation_ontology'"`

echo > $organism.ndx
echo "create table seqfeature_bounds select bioentry_id, min(seqfeature_id) as min_seqfeature_id, max(seqfeature_id) as max_seqfeature_id from seqfeature where type_term_id <> 2 group by bioentry_id;" >> $organism.ndx
echo "create table gene_locations (key (start_pos), key (end_pos), key (gene(20))) select l.location_id, l.seqfeature_id, l.start_pos, l.end_pos, l.strand, s.bioentry_id, q.value as gene from location as l, seqfeature as s, seqfeature_qualifier_value as q where s.seqfeature_id = l.seqfeature_id and s.seqfeature_id = q.seqfeature_id and type_term_id=$gene_term and q.term_id=$locus_tag_term;" >> $organism.ndx

mysql $organism -e "source $organism.ndx"
cat $organism.hdr $organism.ftr $organism.ndx > $organism.sql
rm $organism.hdr $organism.ftr $organism.ndx

echo Generating sequence files.
$scriptdir/gbkseq -a *.gbk*
ls -al *.asc
