#!/bin/sh
#                        R U N _ T E S T
# BRL-CAD
#
# Copyright (c) 2004-2013 United States Government as represented by
# the U.S. Army Research Laboratory.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this file; see the file named COPYING for more
# information.
#

###########
#
#                        R U N _ T E S T
#
# This file is a hybrid sh/tcl file intended for stand-alone running
# of individual *.mged test files.


# Script level

# It shouldn't matter much which tclsh is used to run the test, as
# long as it works - tclsh is just being used to invoke mged and
# the location of mged is handled by the

#\
LD_LIBRARY_PATH=../src/other/tcl/unix:../src/other/tk/unix:$1/src/other/tcl/unix:$1/src/other/tk/unix:$LD_LIBRARY_PATH
#\
DYLD_LIBRARY_PATH=../src/other/tcl/unix:../src/other/tk/unix:$1/src/other/tcl/unix:$1/src/other/tk/unix:$DYLD_LIBRARY_PATH
#\
export LD_LIBRARY_PATH DYLD_LIBRARY_PATH
#\
if test -f ../../src/bwish/btclsh ; then
#\
   exec ../../src/bwish/btclsh $0 $@
#\
else
#\
   exec tclsh $0 $@
#\
fi


#	SETUP

set test_binary_name mged
set CMD_NAME ""
set top_srcdir [lindex $argv 2]
set top_bindir [lindex $argv 1]

if {[string match $top_srcdir ""]} {
   global top_srcdir
   set top_srcdir ../../
}

if {![string match $top_bindir ""]} {
   # We've been given a $top_bindir, try to validate it.
   global CMD_NAME test_binary_name top_bindir

   # Try legal directory and file options
   if {[file isdirectory $top_bindir]} {
      global CMD_NAME test_binary_name top_bindir
      set candidate_name $top_bindir/src/$test_binary_name/$test_binary_name
      if {[file executable $candidate_name] && ![file isdirectory $candidate_name]} {
         global CMD_NAME candidate_name
	 set CMD_NAME $candidate_name
      }
   } else {
      global CMD_NAME test_binary_name top_bindir
      if {[string match $test_binary_name [file tail $top_bindir]] &&
          [file executable $top_bindir]} {
          global CMD_NAME top_bindir
          set CMD_NAME $top_bindir
      }
   }

   # If we don't have a CMD_NAME, assume mged is somewhere in the path and
   # go with it
   if {[string match $CMD_NAME ""]} {
      global CMD_NAME
      puts "Warning: no local mged binary found - assuming mged is in path."
      set CMD_NAME mged
   }
} else {
   # OK, top_bindir is empty and we're on our own for a binary directory.
   global CMD_NAME top_srcdir test_binary_name
   set candidate_name ../../src/$test_binary_name/$test_binary_name
   if {[file executable $candidate_name] && ![file isdirectory $candidate_name]} {
      global CMD_NAME candidate_name
      set CMD_NAME $candidate_name
   } else {
      # If we don't have a CMD_NAME, assume mged is in path and go with it.
      global CMD_NAME
      puts "Warning: no local mged binary found - assuming mged is in path."
      set CMD_NAME mged
  }
}

file delete [lindex $argv 0].g [lindex $argv 0].log [lindex $argv 0]_test.mged

proc add_test {cmdname} {
     global test_binary_name top_srcdir
     set testfilename [format ./%s_test.%s $cmdname $test_binary_name]
     set testfile [open $testfilename a]
     puts $testfile "source [format %s/regress/%s/regression_resources.tcl $top_srcdir $test_binary_name]"
     set inputtestfile [open [format %s/regress/%s/%s.%s $top_srcdir $test_binary_name $cmdname $test_binary_name] r]
     while {[gets $inputtestfile line] >= 0} {
        puts $testfile $line
     }
     close $testfile
     close $inputtestfile
}

proc run_test {cmdname} {
     global CMD_NAME
     global test_binary_name
     add_test $cmdname
     exec $CMD_NAME -c [format %s.g $cmdname] < [format %s_test.%s $cmdname $test_binary_name] >>& [format %s.log $cmdname]
     file delete [format %s_test.%s $cmdname $test_binary_name]
}

# Assume the first argv is the command name and try to run it.

run_test [lindex $argv 0]
