#!/bin/bash -e

# find GBROOT based on assumption this script is being run with a
# qualified path
dir=`dirname $0`
dir=`dirname $dir`
GBROOT=`dirname $dir`
. ${GBROOT}/tests/subset/init.sh

# FIXME: need to parameterize or figure out version; check for unreadable dirs.

doOrphanTest=NO
#confFile='-conf refseq,xeno.conf'

shopt -s nullglob

rm -f core.*
rm -rf out
mkdir -p out
reset-db 
chmod ug+rwx data/processed/*/* data/aligned/*/${TESTDB}/*


db-load-test -initialLoad $confFile all

dailies=`(cd data/processed/genbank.${RELVERSION} && echo daily.*)`

reset-db 
chmod a-rwx data/processed/*/daily.* data/aligned/*/${TESTDB}/daily.*

db-load-test $confFile full

if [ $doOrphanTest = "YES" ] ; then
    echo "NOTE: doing orphan test"
    hgsql -e 'DELETE FROM gbStatus limit 5' ${TESTDB}
fi

if [ -e reload.acc ] ; then
    echo "NOTE: doing reload test"
    reloadList="-reloadList reload.acc"
fi

for daily in $dailies ; do
    chmod ug+rwx data/processed/genbank.${RELVERSION}/$daily
    chmod ug+rwx data/aligned/genbank.${RELVERSION}/${TESTDB}/$daily
    chmod ug+rwx data/processed/refseq.${RELVERSION}/*
    chmod ug+rwx data/aligned/refseq.${RELVERSION}/${TESTDB}/*
    db-load-test $confFile $reloadList $daily
    reloadList=""
done

chmod ug+rwx data/processed/*/* aligned/*/${TESTDB}/*

# list of tables and selected tab contents between the all load and the
# incremental load. The extFile table is expected to differ, since we don't
# clean it up.  Diff it for inspection, but don't report errors.
diff -r out/all/tbls out/$daily/tbls >out/all-last.diffs

diff out/all/gbExtFile.tbl out/$daily/gbExtFile.tbl >out/gbExtFile.diffs || true
