#!/bin/sh
#############################################################################
#
# This script will build a THOR database with a single command, given just
# the TDT file(s) to start with.
# 
# This is a fairly complicated example showing a variety of tricks that you
# might want to use for building THOR databases.  It is very general,
# handling databases with or without indirect data, and compressed or
# uncompressed TDT files.
#
# The files must be named with the following conventions.  Assume the
# database is going to be called "mydb".  The TDT files should be:
#
#	mydb.tdt
#	mydb_datatypes.tdt
#	mydb_indirect.tdt	(optional)
#
# Optionally, the data files can be compressed, e.g. "mydb.tdt.gz".)
#
# You invoke this script as "CreateDB mydb@server"; it will
# create the database "$DY_THORDB/mydb@server".
#
# To try this script, you can build the "example" database supplied with the
# Daylight release:
#
#	cd $DY_ROOT/data
#	CreateDB example
#
# Authors: Craig James, Jeremy Yang
# Rev:  14 Nov 2000
#############################################################################
set -e  ### Exit if an error is detected.

DY_SECURE_PASSWORDS=FALSE
export DY_SECURE_PASSWORDS

help ()
{
  echo "usage: $prog dbname@server:service:user"
  exit 1
}

prog=`basename $0`

if [ $# != 1 ]; then
  help
fi


### Parse the parameter into the server and database parts.

server=`echo $1 | sed -e 's/^.*@//'`
if [ "$1" = "$server" ]; then
  server=`hostname`
fi

### Read server pw once for convenience.

printf "Enter Daylight server password for $server>> "
stty -echo
read svrpw
stty echo
echo

db=`echo $1 | sed -e 's/@.*$//'`
if [ -z "$db" ]; then
  echo "ERROR: no database specified"
  help
fi

### Parse the filename from the database name.

dbpath=`echo $db | sed -e 's/%.*$//'`
dbname=`basename $dbpath`
destdir=`dirname $dbpath`
if [ "$destdir" = "." ]; then
  destdir=\$DY_THORDB
fi

### Check that the TDT files exist.  

if [ -r ${dbname}.tdt ]; then
  main_cat="cat"
elif [ -r ${dbname}.tdt.Z ]; then
  main_cat="zcat"
elif [ -r ${dbname}.tdt.gz ]; then
  main_cat="gzip -dc"
else
  echo "ERROR: missing TDT file: ${dbname}.tdt"
  help
fi

if [ ! -r ${dbname}_datatypes.tdt ]; then
  echo "NOTE: Creating ${dbname}_datatypes.tdt..."
  if [ "$main_cat" != "cat" ]; then
    echo "ERROR: datafile cannot be compressed for tdtinfo use."
    help
  fi
  tdtinfo ${dbname}.tdt > ${dbname}_datatypes.tdt
fi

if [ -r ${dbname}_indirect.tdt ]; then
  indir_cat="cat"
elif [ -r ${dbname}_indirect.tdt.Z ]; then
  indir_cat="zcat"
elif [ -r ${dbname}_indirect.tdt.gz ]; then
  indir_cat="gzip -dc"
else
  indir_cat=""
fi


echo "Counting TDTs and cross references..."

dtype_counts=`tdtcount ${dbname}_datatypes.tdt`
dtype_count=`echo "$dtype_counts" | awk '{print $5}'`
echo "Datatypes TDTs = $dtype_count"
dtype_count=`expr $dtype_count + 100`

tdt_counts=`tdtcount ${dbname}.tdt`
tdt_count=`echo "$tdt_counts" | awk '{print $5}'`
xref_count=`echo "$tdt_counts" | awk '{print $2}'`
xref_count=`expr $xref_count - $tdt_count`
echo "Primary TDT file (tdts, xrefs) = ${tdt_count}, ${xref_count}"

if [ "$indir_cat" != "" ]; then
  indir_counts=`tdtcount ${dbname}_indirect.tdt`
  indir_count=`echo "$indir_counts" | awk '{print $5}'`
  echo "Indirect TDTs = $indir_count"
fi

### set -x  ## Prints commands as they are executed.

echo "Creating the datatypes database first..."
thormake $destdir/${dbname}_datatypes%@$server%$svrpw $dtype_count 0

if [  "$indir_cat" != "" ]; then
  echo "Creating the indirect-data database next..."
  indir_dbname=$destdir/${dbname}_indirect@$server%$svrpw
  indir_dbname_opt="-indirect_database "${dbname}_indirect
  thormake $indir_dbname  $indir_count 0 \
	-datatypes_database ${dbname}_datatypes
else
  indir_dbname_opt=""
fi

echo "Finally, creating the regular database..."
thormake $destdir/${dbname}%@$server%$svrpw $tdt_count $xref_count \
	-datatypes_database ${dbname}_datatypes \
	$indir_dbname_opt

echo "Loading the datatypes database..."
### Standard datatypes take precedence.
cat ${dbname}_datatypes.tdt |
  thorload -merge false -overwrite true \
  ${dbname}_datatypes%@$server%$svrpw
cat $DY_ROOT/data/datatypes/std_datatypes.dcis.tdt |
  thorload -merge false -overwrite true \
  ${dbname}_datatypes%@$server%$svrpw

echo "Loading the primary database; adding fingerprints..."
$main_cat ${dbname}.tdt \
	| fingerprint \
	| thorload ${dbname}%@$server%$svrpw

if [ "$indir_dbname_opt" != "" ]; then
  echo "Loading the indirect-data database..."
  $indir_cat ${dbname}_indirect.tdt |
    thorload ${dbname}_indirect%@$server%$svrpw
fi

echo "Done creating $destdir/$dbname."
