#!/bin/sh
#################################################################
#
# File: runtest.tcl
# Project: COST-250 Speaker Recognition Reference System
# Author:  H. Melin, KTH/CTT, 19/02/1998
#
# Description:
# Run a short calibration test on Polycost with the reference system.
#
# This is an example of a main script that will setup a database 
# and the reference recognition system and will run an experiment.
#
# The database and recognizer commands are contained in the package
# cost250, which must be installed before this script can be run.
# (it is installed when you run 'make install').
#
# ---------------------------------------------------------------
#
# History:
# 21/06/1999           - part of COST250 Speaker Recognition Reference 
#                        System, release 1.0
# 24/02/1999           - part of development release 0.2
# 24/02/1999 H. Melin  - added error handling
# 23/02/1999           - part of development release 0.1
#
#################################################################
#
# Run tcl shell from the user's PATH \
exec tclsh "$0" "$@"

package require cost250

# Default command line argument
set arg polycost/test

# See if we got a command line argument
if { $argc >= 1 } {
    set arg [lindex $argv 0]
}

#################################################################
#
# Define a directory structure.
#
# We use a 'tag' that is kind of the name of the experiment.
# This tag is used in many path names. The tag should be
# given as the first command line argument to this script
# (if not "polycost/test" is used as default, see above).
#
set tag $arg

# This is where models and results are stored:
set clidir $tag/cli
set refdir $tag/ref
set resultfile $tag/results.llk

# This is where experiment specification reside:
set expdir ../experiment/$tag

# This is where polycost files are stored. Note: we assume
# they are all in one directory and that all known bugs
# have been fixed there. Therefore you cannot run this
# with data on the first version of the CDROMs.
#
set datadir ../data/polycost

# This is where temporary files will be created
set tmpdir  /tmp

#################################################################
#
# Check our setup and create directories.
#

puts "\n*** Experiment batch: $tag\n"


##################################################################
#
# Create objects
#
# We need a database object to represent Polycost and a recognizer 
# object to represent the reference recognizer.
#
# To create an object we do:
#   1) create the object with default properties
#   2) change property values if needed
#   3) initialize the object.
#
puts "Creating resources..."

# 1) create objects with default properties:
set database [Database::new $datadir]
set recsys   [ReferenceSystem::new $database]

# 2) change property values:
$recsys set cliDir $clidir
$recsys set refDir $refdir
$recsys set tmpDir $tmpdir

# 3) initialize objects:
$database init
$recsys init

#################################################################
#
# Log object status
#
# It may be useful to store complete information about your system
# and database in a log file for later reference. This commands
# will print the internal states of our objects to stdout, so
# if we are sending all output to a log file...
#
puts "Object status:"

puts -nonewline "Database:   "
$database print

puts -nonewline "Recognizer: "
$recsys print

#################################################################
#
# Run experiments
#
# Our objects are now properly setup and we are ready to go.
# Our test experiment batch contain three parts:
#   1) train a non-client world model
#   2) enroll speakers
#   3) run verification tests
#

puts "Running experiment..."

set ok 1

# 1) train non-client world model:
if {$ok} {
    set ok [Experiment::run $expdir/os_wld.exp -database $database -system $recsys -outdir $refdir]
}


# 2) enroll speakers:
if {$ok} {
    set ok [Experiment::run $expdir/es.exp -database $database -system $recsys -outdir $clidir]
}

# 3) run verification tests:
if {$ok} {
    set ok [Experiment::run $expdir/ts.exp -database $database -system $recsys -resultfile $resultfile]
}


#################################################################
#
# Cleanup
#
# We should always destroy our objects so they get a chance to
# delete temporary files etc.
#
puts "Cleaning up..."

$recsys destroy
$database destroy

if {$ok} {
    puts "Done."
} else {
    puts "Aborted."
}