#
# Library functions for the scripting library (scrib)
#
# File: scrib/lib/regression.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#---------------------------------------------------------------------

global env
if [info exists env(ICEM_SCRIB)] {
    set hostsfile [file join $env(ICEM_SCRIB) lib regression_hosts.tcl]
    if [file exists $hostsfile] {
	if [catch { uplevel \#0 source $hostsfile } res] {
	    mess "Error: getting host list: $res\n" red
	}
    }
}


## 

proc scrib_lib_regression_model_submit_to_test_queue { queue_id model_dir plat logfile comparison_type } {
    
    mess "Model Directory is \"$model_dir\" \n" 
    mess "Log File is \"$logfile\" \n" 
    set host [scrib_lib_regression_get_host $plat]
    set remote_icemacn [scrib_lib_regression_get_icemacn_for_platform $plat]
    
    set prefs_file   [scrib_lib_regression_preferences_file $model_dir]    
    set replay       [scrib_lib_regression_generate_script $prefs_file $logfile $comparison_type]
    mess "Replay File is \"$replay\" \n" 
    set max_time     [scrib_lib_regression_preferences_maxtime $prefs_file]

    set command "batch_queue_icbqs_server_medbatch_exec $model_dir $replay $logfile $remote_icemacn"
    #mess "command : $command\n" red
    
    set job_token [batch_queue::submit_job $queue_id "$command" -server $host -logfile $logfile \
		      -max_time $max_time]
    
    # fixme -- do more stuff ??
    
    return $job_token
}


## Returns "SUCCESS", "FAILURE" or "MISCONFIG"

proc scrib_lib_regression_model_test { model_dir platform logfile {report_bugs 0} {comparison_type ""} {xml_report ""} } {
   
    mess " Log file is \"$logfile \" \n" red  
    set old_dir [pwd]
    cd $model_dir
    
    # --> Get host for platform FIXME
    
    set host [scrib_lib_regression_get_host $platform]
    if {"" == $host} {
	
	set site [scrib_lib_regression_get_site]
	mess "Error: cannot find host for platform \"$platform\" at site \"$site\"\n" red
	return MISCONFIG
	
    } elseif {[info hostname] != $host} {
	
	set remote_icemacn [scrib_lib_regression_get_icemacn_for_platform $platform]
	mess "Running model on remote host \"$host\" for platform \"$platform\" with ICEM_ACN \"$remote_icemacn\"\n"
	
    } else {
	
	set host "localhost"
	global env
	if [info exists env(ICEM_ACN)] {
	    set remote_icemacn $env(ICEM_ACN)
	} else {
	    set remote_icemacn [scrib_lib_regression_get_icemacn_for_platform $platform]
	}
    }
    
    # --> Make a list of files existing before, so we can remove tmp files later
    
    set files_before_exec [glob -nocomplain -type f *]
    
    # --> Search for the preferences file; get script file
    
    set prefs_file   [scrib_lib_regression_preferences_file $model_dir]
    set script       [scrib_lib_regression_generate_script $prefs_file $logfile $comparison_type]
    set module       [scrib_lib_regression_model_module $model_dir]
    
    set job_token    [scrib_lib_exec_get_unique_job_token "${module}_[lindex [file tail $model_dir] end]_${platform}"]
    
    set med_args     "" ;# "-batch -script $script"
    set exec_args    "-logfile $logfile -remote_host $host -override_use_script $script -remote_icemacn $remote_icemacn"
    
    # --> Run MED with script and regression log, tracking time
    
    stdout "Testing \"[file tail $model_dir]\" with script \"[file tail $script]\"\n"
    if ![file exists $script] {
	mess "Error: cannot test model \"$model_dir\" because can't find its script:\n\t$script\n" red
    }
    
    set start_time [scrib_lib_regression_exectime start]
    
    set stat [scrib_lib_exec_medbatch $job_token "" $med_args $exec_args]
    
    set time_elapsed [scrib_lib_regression_exectime stop]
    
    # !! watch the return value of scrib_lib_exec_medbatch !! --fixme --jka !!
    
    if {1 != $stat} {
	
	# (but it's possible that the it waas not the test itself that failed)
	
	set stat [scrib_lib_exec_failure]
	
    } else {
	
	# check the tail of the log. last line should be either "SUCCESS" or "FAILURE"
	
	catch {exec tail -n 1 $logfile} stat
	if {("SUCCESS" != $stat) && ("MISCONFIG" != $stat)} {
	    set stat "FAILURE"
	}
    }
    
    # --> *NOW* we can put the execution time to the logfile
    
    set text "Execution time was: $time_elapsed seconds"
    mess "$text\n"
    scrib_lib_utils_append_text_to_file "\n\n$text" $logfile
    
    # --> If stat==0, perhapse mail report to maintainer
    #     fixme: check that the job was not simply killed
    
    if {"SUCCESS" == $stat} {	
	
	# --> Report something about time_profiling
	
	set stat [scrib_lib_regression_compare_exectime $prefs_file $logfile]
    }
    
    # --> If something failed, perhaps mail what is noted in "$logfile" to maintainer
    
    if {("FAILURE" == $stat) && $report_bugs} {
	set casemodel [file tail $model_dir]
	set maintainers [scrib_lib_regression_module_maintainers $module $prefs_file]
	scrib_lib_regression_report_bug $module $casemodel $platform $maintainers $logfile
    }
        
    # -->     
    catch {
	
	if {"EXISTING_DOM" == $xml_report} {
	    
	    TestBenchReport::addTestcase $job_token "START \"$start_time\" RESULT $stat TESTPATH $model_dir \
                                            ELAPSED $time_elapsed DUMP $logfile PLATFORM $platform DATE"
	    
	} elseif {"" != $xml_report} {
	    
	    TestBenchReport::createDocument "NAME $module:$job_token TITLE {scrib_lib_regression_test: $module} \
                                         MACHINE $host APPLICATION $module"
	    TestBenchReport::addTestcase $job_token "START \"$start_time\" RESULT $stat TESTPATH $model_dir \
                                            ELAPSED $time_elapsed DUMP $logfile PLATFORM $platform"
	    TestBenchReport::writeReport $xml_report xml
	    TestBenchReport::closeDocument	
	}
    }
    
    # --> Remove the list of files not existing before
    
    scrib_lib_utils_remove_files_not_in_list $files_before_exec
    
    cd $old_dir
    
    mess "$stat: testing model: $model_dir on $platform\n\n" [expr {("FAILURE" == $stat) ? "red" : "blue"}]
    
    return $stat
}

## 

proc scrib_lib_regression_model_module {model_dir} {
    #
    # --> Assume the model directory looks like .../$module/test_model ; fixme?
    #
    set model_path_split [file split $model_dir]
    set module [lindex $model_path_split end-1]
    return $module
}

## 

proc scrib_lib_regression_generate_script {prefs_file remote_host {comparison_type ""}} {
    
    # --> Get the replay file
    set date_stmp [lindex [exec date] 2]
    set hr_stmp [lindex [split [lindex [exec date] 3] :] 0]
    set min_stmp [lindex [split [lindex [exec date] 3] :] 1]
    set time_stamp $date_stmp-$hr_stmp-$min_stmp 
    set platform $remote_host 
    set module [lindex [file split [file dirname $prefs_file]] end-1]
    set model [lindex [file split [file dirname $prefs_file]] end] 
    
#    set replay [scrib_lib_regression_preferences_replay $prefs_file]    
    set replay [scrib_lib_regression_preferences_replay $prefs_file]    
    if ![file isfile $replay] {	
	mess "Error: cannot find replay file for model: $replay\n" red
	return ""
    }
    set prefs_file_rel [lindex [file split $prefs_file] end]   
    set replay_rel [lindex [file split $replay] end] 

    # --> Wrap the replay file in info for the test
    
#    set script [file join [file dirname $replay] "regression_wrapped_[file tail $replay].$platform.tcl"]
    set script [file join [file dirname $prefs_file] "regression_wrapped_[file tail $replay].$platform.tcl"] 
    if [catch {open $script w} fp] {
	mess "Error: could not open script file for writing: " red
	if ![file writable $script] {
	    mess " check permissions on directory\n" red
	}
	mess " [file dirname $script]\n" red
	return
    }
   
    # --> Print in some stuff, especially, necessarily: input_file output_file
    puts $fp "#Here we go\n" 
    puts $fp "#\n# --> This file created by scrib_lib_regression_generate_script from replay $replay"
    puts $fp "#\tin \"$::ScribApp::_app_name\" built on ICEM CFD ScribApp v.$::ScribApp::_version\n\#\n"
    puts $fp "global env"
    puts $fp "set stat \[catch {source \[file join \$env(ICEM_ACN) lib scrib lib regression.tcl\]} res\]"
    puts $fp "if \$stat \{\n\tmess \"MISCONFIG\"\n\treturn\n\}\n"
    
    puts $fp "set  prefs_file [lindex [file split $prefs_file] end]"
    puts $fp "set log_file $module.$model.$platform.log"
    puts $fp "set remote_host $platform"
    puts $fp "set old_log \{\[glob -nocomplain *.$platform.*.log\]\}"
    puts $fp "if \{\$old_log != \"\"\} \{ eval cmd_rm_r \$old_log \}" 
    puts $fp "set core_file \{\[glob -nocomplain *core\]\}"
    puts $fp "if \{\$core_file != \"\"\} \{ eval cmd_rm_r \$core_file \}" 
    puts $fp "set old_journal \{\[glob -nocomplain unnamed.jrf.*\]\}"
    puts $fp "if \{\$old_journal != \"\"\} \{ eval cmd_rm_r \$old_journal \}" 
#  
    if [file exists [scrib_lib_regression_preferences_input_geo_file  $prefs_file]] {
     puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file  $prefs_file]] end]"
    }
#
    if [file exists [scrib_lib_regression_preferences_input_msh_file  $prefs_file]] {   
      puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file  $prefs_file]] end]"
    }
#
    if [file exists [scrib_lib_regression_preferences_input_blk_file  $prefs_file]] { 
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file  $prefs_file]] end]"
    }
    if [file exists [scrib_lib_regression_preferences_input_fbc_file  $prefs_file]] {
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file  $prefs_file]] end]"
    }
    if [file exists [scrib_lib_regression_preferences_input_par_file  $prefs_file]] {
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file  $prefs_file]] end]"
    }
#
     puts $fp "if \[file exists rep-$model-on-$remote_host-is-done\] \{\n\teval cmd_rm_r  rep-$model-on-$remote_host-is-done\n\}\n"
# Removing old output files
#
    puts $fp "set output_file \$log_file\n"
#
    puts $fp "if \[file exists \$output_file\] \{\n\teval cmd_rm_r \$output_file\n\}\n"
    
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "geometry"} {	 
 
    puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file $prefs_file]] end]"
    puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file $prefs_file]] end]"
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file $prefs_file]] end]"
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file $prefs_file]] end]"
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file $prefs_file]] end]"
    puts $fp "set output_msh_file [lindex [file split [file root [scrib_lib_regression_preferences_output_geo_file $prefs_file]].$remote_host.uns] end]"
    puts $fp "set output_blk_file [lindex [file split [file root [scrib_lib_regression_preferences_output_geo_file $prefs_file]].$remote_host.blk] end]"
    puts $fp "set output_fbc_file [lindex [file split [file root [scrib_lib_regression_preferences_output_geo_file $prefs_file]].$remote_host.fbc] end]"
    puts $fp "set output_par_file [lindex [file split [file root [scrib_lib_regression_preferences_output_geo_file $prefs_file]].$remote_host.par] end]"
    puts $fp "set output_geo_file [lindex [file split [file root [scrib_lib_regression_preferences_output_geo_file $prefs_file]].$remote_host.tin] end]"
    puts $fp "set std_geo_file [lindex [file split [scrib_lib_regression_preferences_std_geo_file $prefs_file]] end]"
     puts $fp "if \[file exists \$output_geo_file\] \{\n\teval cmd_rm_r \$output_geo_file\n\}\n"
    }
#
#
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "mesh"} {	 
    puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file $prefs_file]] end]"
    puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file $prefs_file]] end]"
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file $prefs_file]] end]"
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file $prefs_file]] end]"
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file $prefs_file]] end]"
    puts $fp "set output_msh_file [lindex [file split [file root [scrib_lib_regression_preferences_output_msh_file $prefs_file]].$remote_host.uns] end]"
    puts $fp "set output_blk_file [lindex [file split [file root [scrib_lib_regression_preferences_output_msh_file $prefs_file]].$remote_host.blk] end]"
    puts $fp "set output_fbc_file [lindex [file split [file root [scrib_lib_regression_preferences_output_msh_file $prefs_file]].$remote_host.fbc] end]"
    puts $fp "set output_par_file [lindex [file split [file root [scrib_lib_regression_preferences_output_msh_file $prefs_file]].$remote_host.par] end]"
    puts $fp "set output_geo_file [lindex [file split [file root [scrib_lib_regression_preferences_output_msh_file $prefs_file]].$remote_host.tin] end]"
     puts $fp "set std_msh_file [lindex [file split [scrib_lib_regression_preferences_std_msh_file $prefs_file]] end]"
     puts $fp "if \[file exists \$output_msh_file\] \{\n\teval cmd_rm_r \$output_msh_file\n\}\n"
  }
#
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "blocking"} {	 
    puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file $prefs_file]] end]"
    puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file $prefs_file]] end]"
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file $prefs_file]] end]"
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file $prefs_file]] end]"
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file $prefs_file]] end]"
    puts $fp "set output_msh_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.uns] end]"
    puts $fp "set output_blk_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.blk] end]"
    puts $fp "set output_fbc_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.fbc] end]"
    puts $fp "set output_par_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.par] end]"
    puts $fp "set output_geo_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.tin] end]"
    puts $fp "set std_blk_file [lindex [file split [scrib_lib_regression_preferences_std_blk_file $prefs_file]] end]"
     puts $fp "if \[file exists \$output_blk_file\] \{\n\teval cmd_rm_r \$output_blk_file\n\}\n"
  }
#
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "attribute"} {	 
     puts $fp "set output_fbc_file \"[scrib_lib_regression_preferences_output_fbc_file $prefs_file].$remote_host.fbc\"\n"
     puts $fp "set std_fbc_file \"[scrib_lib_regression_preferences_std_fbc_file $prefs_file].$remote_host.fbc\"\n"
     puts $fp "if \[file exists \$output_fbc_file\] \{\n\teval cmd_rm_r \$output_fbc_file\n\}\n"
   }
#
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "parameter"} {	 
    puts $fp "set output_par_file \"[scrib_lib_regression_preferences_output_par_file $prefs_file].$remote_host.par\"\n"
    puts $fp "set std_par_file \"[scrib_lib_regression_preferences_std_par_file $prefs_file].$remote_host.par\"\n"
     puts $fp "if \[file exists \$output_par_file\] \{\n\teval cmd_rm_r \$output_par_file\n\}\n"
   }
# 
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "ansys_input"} {	 
    puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file $prefs_file]] end]"
    puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file $prefs_file]] end]"
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file $prefs_file]] end]"
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file $prefs_file]] end]"
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file $prefs_file]] end]"
    puts $fp "set output_msh_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.uns] end]"
    puts $fp "set output_blk_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.blk] end]"
    puts $fp "set output_fbc_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.fbc] end]"
    puts $fp "set output_par_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.par] end]"
    puts $fp "set output_geo_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.tin] end]"
    puts $fp "set ansys_input_file \"[file root [scrib_lib_regression_preferences_ansys_input_file $prefs_file]].$remote_host.in\"\n"
    puts $fp "set std_ansys_input_file [scrib_lib_regression_preferences_std_ansys_input_file $prefs_file]\n"
     puts $fp "if \[file exists \$ansys_input_file\] \{\n\teval cmd_rm_r \$ansys_input_file\n\}\n"
  }
#
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "attribute"} {	 
     puts $fp "set output_fbc_file \"[scrib_lib_regression_preferences_output_fbc_file $prefs_file].$remote_host.fbc\"\n"
     puts $fp "set std_fbc_file \"[scrib_lib_regression_preferences_std_fbc_file $prefs_file].$remote_host.fbc\"\n"
     puts $fp "if \[file exists \$output_fbc_file\] \{\n\teval cmd_rm_r \$output_fbc_file\n\}\n"
   }
# 
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "dyna_input"} {	 
    puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file $prefs_file]] end]"
    puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file $prefs_file]] end]"
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file $prefs_file]] end]"
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file $prefs_file]] end]"
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file $prefs_file]] end]"
    puts $fp "set output_msh_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.uns] end]"
    puts $fp "set output_blk_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.blk] end]"
    puts $fp "set output_fbc_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.fbc] end]"
    puts $fp "set output_par_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.par] end]"
    puts $fp "set output_geo_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.tin] end]"
    puts $fp "set dyna_input_file [lindex [file split [file root [scrib_lib_regression_preferences_dyna_input_file $prefs_file]].$remote_host.k] end]"
    puts $fp "set std_dyna_input_file [lindex [file split [scrib_lib_regression_preferences_std_dyna_input_file $prefs_file]] end]"
     puts $fp "if \[file exists \$dyna_input_file\] \{\n\teval cmd_rm_r \$dyna_input_file\n\}\n"
  }
# 
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "nastran_input"} {	 
    puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file $prefs_file]] end]"
    puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file $prefs_file]] end]"
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file $prefs_file]] end]"
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file $prefs_file]] end]"
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file $prefs_file]] end]"
    puts $fp "set output_msh_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.uns] end]"
    puts $fp "set output_blk_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.blk] end]"
    puts $fp "set output_fbc_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.fbc] end]"
    puts $fp "set output_par_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.par] end]"
    puts $fp "set output_geo_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.tin] end]"
    puts $fp "set nastran_input_file [lindex [file split [file root [scrib_lib_regression_preferences_nastran_input_file $prefs_file]].$remote_host.dat] end]"
    puts $fp "set std_nastran_input_file [lindex [file split [scrib_lib_regression_preferences_std_nastran_input_file $prefs_file]] end]"
     puts $fp "if \[file exists \$nastran_input_file\] \{\n\teval cmd_rm_r \$nastran_input_file\n\}\n"
  }
# 
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "abaqus_input"} {	 
    puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file $prefs_file]] end]"
    puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file $prefs_file]] end]"
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file $prefs_file]] end]"
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file $prefs_file]] end]"
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file $prefs_file]] end]"
    puts $fp "set output_msh_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.uns] end]"
    puts $fp "set output_blk_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.blk] end]"
    puts $fp "set output_fbc_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.fbc] end]"
    puts $fp "set output_par_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.par] end]"
    puts $fp "set output_geo_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.tin] end]"
    puts $fp "set abaqus_input_file [lindex [file split [file root [scrib_lib_regression_preferences_abaqus_input_file $prefs_file]].$remote_host.inp] end]"
    puts $fp "set std_abaqus_input_file [lindex [file split [scrib_lib_regression_preferences_std_abaqus_input_file $prefs_file]] end]"
     puts $fp "if \[file exists \$abaqus_input_file\] \{\n\teval cmd_rm_r \$abaqus_input_file\n\}\n"
  }
# 
  if {[scrib_lib_regression_preferences_output_type $prefs_file] == "output_interface"} {	 
    puts $fp "set input_geo_file [lindex [file split [scrib_lib_regression_preferences_input_geo_file $prefs_file]] end]"
    puts $fp "set input_msh_file [lindex [file split [scrib_lib_regression_preferences_input_msh_file $prefs_file]] end]"
    puts $fp "set input_blk_file [lindex [file split [scrib_lib_regression_preferences_input_blk_file $prefs_file]] end]"
    puts $fp "set input_fbc_file [lindex [file split [scrib_lib_regression_preferences_input_fbc_file $prefs_file]] end]"
    puts $fp "set input_par_file [lindex [file split [scrib_lib_regression_preferences_input_par_file $prefs_file]] end]"
    puts $fp "set output_msh_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.uns] end]"
    puts $fp "set output_blk_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.blk] end]"
    puts $fp "set output_fbc_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.fbc] end]"
    puts $fp "set output_par_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.par] end]"
    puts $fp "set output_geo_file [lindex [file split [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.tin] end]"
    puts $fp "set solver_input_file [lindex [file split [file root [scrib_lib_regression_preferences_output_interface $prefs_file]].$remote_host.inp] end]"
    puts $fp "if \[file exists \$solver_input_file\] \{\n\teval cmd_rm_r \$solver_input_file\n\}\n"
  }
#
#
    puts $fp "set output_type \"[scrib_lib_regression_preferences_output_type $prefs_file]\"\n" 
    # --> Now copy in the source code of the $replay script
    
    puts $fp "#\n# --> BEGIN source from script $replay ---------\n#\n"   
    puts $fp "mess \"Sourcing the med replay file\"\n" 
    set ifp [open $replay r]
    puts $fp [read $ifp]
    close $ifp
    puts $fp "#\n# --> END source from script $replay -----------\n#\n"
    
    # --> Now maybe do some checking stuff
    
    if {[scrib_lib_regression_preferences_output_type $prefs_file] == "mesh"} {	 
     if {[file exists [file root [scrib_lib_regression_preferences_output_msh_file $prefs_file]].$remote_host.uns] && [file size [file root [scrib_lib_regression_preferences_output_msh_file $prefs_file]].$remote_host.uns] >0} { 
#     puts $fp "set model_dir \"[file dirname [scrib_lib_regression_preferences_output_msh_file $prefs_file]]\""
     puts $fp "set model_dir ."
     puts $fp "if \[file exists \$output_msh_file\] \{\n ic_compare_mesh_report \$model_dir \$output_msh_file testing $remote_host\n\} else \{\n mess \"Failed to create the output mesho\" red\n\}"
     puts $fp "if \[file exists \$std_msh_file\] \{\n ic_compare_mesh_report \$model_dir \$std_msh_file standard $remote_host\n\}"
     puts $fp "if \{\[file exists \$output_msh_file\] && \[file exists \$std_msh_file\]\} \{\n ic_compare_get_mesh_report current_report_$remote_host.rpt std_report.rpt  $remote_host\n\}"   
  }
}
#
#
# Geometry comparison
#
#
    if {[scrib_lib_regression_preferences_output_type $prefs_file] == "geometry"} {	 
     if {[file exists [file root [scrib_lib_regression_preferences_output_geo_file $prefs_file]].$remote_host.tin] && [file size [file root [scrib_lib_regression_preferences_output_geo_file $prefs_file]].$remote_host.tin] >0} { 
#     puts $fp "set model_dir [file dirname [scrib_lib_regression_preferences_output_geo_file $prefs_file]]"
     puts $fp "set model_dir ."
     puts $fp "if \[file exists \$output_geo_file\] \{\n ic_compare_geom_report \$model_dir \$output_geo_file testing $remote_host\n\} else \{\n mess \"Failed to create the output geometry\" red\n\}"
     puts $fp "if \[file exists \$std_geo_file\] \{\n ic_compare_geom_report \$model_dir \$std_geo_file standard $remote_host\n\}"
     puts $fp "if \{\[file exists \$output_geo_file\] && \[file exists \$std_geo_file\]\} \{\n ic_compare_get_geom_report current_report_$remote_host.rpt std_report.rpt  $remote_host\n\}"   
    }
   } 
#
    if {[scrib_lib_regression_preferences_output_type $prefs_file] == "blocking"} {	 
     if {[file exists [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.blk] && [file size [file root [scrib_lib_regression_preferences_output_blk_file $prefs_file]].$remote_host.blk] >0} { 
#     puts $fp "set model_dir [file dirname [scrib_lib_regression_preferences_output_blk_file $prefs_file]]"
     puts $fp "set model_dir ."
     puts $fp "ic_compare_blk_report \$model_dir \$input_geo_file \$output_blk_file testing $remote_host" 
     puts $fp "ic_compare_blk_report \$model_dir \$input_geo_file \$std_blk_file standard $remote_host"
     puts $fp "ic_compare_get_blk_report current_report_$remote_host.rpt std_report.rpt $remote_host\n"  
    }
   } 
#
    if {[scrib_lib_regression_preferences_output_type $prefs_file] == "ansys_input"} {	 

     set ansys_input_file [file root [scrib_lib_regression_preferences_ansys_input_file $prefs_file]].$remote_host.in
     set std_ansys_input_file [scrib_lib_regression_preferences_std_ansys_input_file $prefs_file]
#     puts $fp "set model_dir [file dirname [scrib_lib_regression_preferences_ansys_input_file $prefs_file]]"
     puts $fp "set model_dir ."
     puts $fp "ic_compare_get_fea_solver_report $ansys_input_file $std_ansys_input_file  $remote_host"   
   } 
#
    if {[scrib_lib_regression_preferences_output_type $prefs_file] == "dyna_input"} {	 

     set dyna_input_file [file root [scrib_lib_regression_preferences_dyna_input_file $prefs_file]].$remote_host.k 
     set std_dyna_input_file [scrib_lib_regression_preferences_std_dyna_input_file $prefs_file]
#     puts $fp "set model_dir [file dirname [scrib_lib_regression_preferences_dyna_input_file $prefs_file]]"
     puts $fp "set model_dir ."
     puts $fp "ic_compare_get_fea_solver_report $dyna_input_file $std_dyna_input_file  $remote_host"   
   } 
#
#   Nastran Translator
#
    if {[scrib_lib_regression_preferences_output_type $prefs_file] == "nastran_input"} {	 

     set nastran_input_file [file root [scrib_lib_regression_preferences_nastran_input_file $prefs_file]].$remote_host.dat 
     set std_nastran_input_file [scrib_lib_regression_preferences_std_nastran_input_file $prefs_file]
#     puts $fp "set model_dir [file dirname [scrib_lib_regression_preferences_nastran_input_file $prefs_file]]"
     puts $fp "set model_dir ."
     puts $fp "ic_compare_get_fea_solver_report $nastran_input_file $std_nastran_input_file  $remote_host"   
   } 
#
    if {[scrib_lib_regression_preferences_output_type $prefs_file] == "abaqus_input"} {	 

     set abaqus_input_file [file root [scrib_lib_regression_preferences_abaqus_input_file $prefs_file]].$remote_host.inp  
     set std_abaqus_input_file [scrib_lib_regression_preferences_std_abaqus_input_file $prefs_file]
#     puts $fp "set model_dir [file dirname [scrib_lib_regression_preferences_abaqus_input_file $prefs_file]]"
     puts $fp "set model_dir ."
     puts $fp "ic_compare_get_fea_solver_report $abaqus_input_file $std_abaqus_input_file  $remote_host"   
   } 
#
    if {[scrib_lib_regression_preferences_output_type $prefs_file] == "output_interface"} {	 

     set solver_input_file [file root [scrib_lib_regression_preferences_solver_input_file $prefs_file]].$remote_host.inp  
     set std_solver_input_file [scrib_lib_regression_preferences_std_solver_input_file $prefs_file]
#     puts $fp "set model_dir [file dirname [scrib_lib_regression_preferences_solver_input_file $prefs_file]]"
     puts $fp "set model_dir ."
     puts $fp "ic_compare_get_solver_report $solver_input_file $std_solver_input_file  $remote_host"   
   } 
#
    # stat will be either SUCCESS or FAILURE. make sure it's the last line for quick "tail"s
#    puts $fp "puts -nonewline \$stat\n"
     puts $fp "set done_replay \[open rep-$model-on-$remote_host-is-done w\]" 
     puts $fp "close \$done_replay" 
    puts $fp "exit" 
    close $fp
    
    return $script
}


# ----------------------------------------------------------------------------------------
# -->  P   R   E   F   E   R   E   N   C   E   S
# ----------------------------------------------------------------------------------------

## Get the preferences file for a given model directory

proc scrib_lib_regression_preferences_file {model_dir} {
    
    # --> Search for the preferences file;
    #     it will hopefully be called "regression_prefs.tcl"
    #     it will have the string "regression_prefs_id_string" in it
    
    set prefs_file [file join $model_dir "regression_prefs.tcl"]
    
    if [file exists $prefs_file] {
	return $prefs_file
    } else {
	set key_string "regression_prefs_id_string"
	# [globa/regexp for the key_string] fixme FIXME
	mess "Error: could not find the preferences file for $model_dir\n" red
    }
}

##

proc scrib_lib_regression_model_preferences_get {model_dir} {
    
    set null_array(__NOTHING__) ""
    
    set prefs_file [scrib_lib_regression_preferences_file $model_dir]
    if ![file isfile $prefs_file] {
	return [array get null_array]
    }
    
    if [catch {source $prefs_file} res] {
	return [array get null_array]
    }
    
    if ![array exists regression_prefs] {
	return [array get null_array]
    }
    
    return [array get regression_prefs]
}

## Get script file

proc scrib_lib_regression_preferences_representative_cmds {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(representative_cmds)] {
	set rep_cmds $regression_prefs(representative_cmds)
    } else {
	# try default
	set rep_cmds ""
    }
    
    if {"" == $rep_cmds} {
	set rep_cmds_file [file join [file root $prefs_file] "representative_cmds"]
	if [file exists $rep_cmds_file] {
	    set rep_cmds [scrib_lib_utils_slurp_file_into_list $rep_cmds_file]
	}
    }
    
    return $rep_cmds
}

## Get script file

proc scrib_lib_regression_preferences_replay {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(replay_file)] {
	set script $regression_prefs(replay_file)
    } else {
	# try default
	set script "med.replay"
    }

    if {"relative" == [file pathtype $script]} {
	set script [file join [file dirname $prefs_file] $script]
    }

    return $script
}

## 

proc scrib_lib_regression_preferences_std_output_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_output_file)] {
	set comparandum $regression_prefs(std_output_file)
    } else {
	# try old school default
	set comparandum "std.output"
    }
    
    if {"relative" == [file pathtype $comparandum]} {
	set comparandum [file join [file dirname $prefs_file] $comparandum]
    }
    
    return $comparandum
}

## 
proc scrib_lib_regression_preferences_output_type {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(output_type)] {
	set output_type $regression_prefs(output_type)
    }


    return $output_type 
}
#

proc scrib_lib_regression_preferences_input_geo_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(input_geo_file)] {
	set input_geo_file $regression_prefs(input_geo_file)
    } else {
	# try old school default
	set input_geo_file "input.tin"
    }

    if {"relative" == [file pathtype $input_geo_file]} {
	set input_geo_file [file join [file dirname $prefs_file] $input_geo_file]
    }

    return $input_geo_file
}
#
proc scrib_lib_regression_preferences_input_msh_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(input_msh_file)] {
	set input_msh_file $regression_prefs(input_msh_file)
    } else {
	# try old school default
	set input_msh_file "input.uns"
    }

    if {"relative" == [file pathtype $input_msh_file]} {
	set input_msh_file [file join [file dirname $prefs_file] $input_msh_file]
    }

    return $input_msh_file
}
#
proc scrib_lib_regression_preferences_input_blk_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(input_blk_file)] {
	set input_blk_file $regression_prefs(input_blk_file)
    } else {
	# try old school default
	set input_blk_file "input.blk"
    }

    if {"relative" == [file pathtype $input_blk_file]} {
	set input_blk_file [file join [file dirname $prefs_file] $input_blk_file]
    }

    return $input_blk_file
}
#
proc scrib_lib_regression_preferences_input_fbc_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(input_fbc_file)] {
	set input_fbc_file $regression_prefs(input_fbc_file)
    } else {
	# try old school default
	set input_fbc_file "input.fbc"
    }

    if {"relative" == [file pathtype $input_fbc_file]} {
	set input_fbc_file [file join [file dirname $prefs_file] $input_fbc_file]
    }

    return $input_fbc_file
}
#
proc scrib_lib_regression_preferences_input_par_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(input_par_file)] {
	set input_par_file $regression_prefs(input_par_file)
    } else {
	# try old school default
	set input_fbc_file "input.par"
    }

    if {"relative" == [file pathtype $input_par_file]} {
	set input_par_file [file join [file dirname $prefs_file] $input_par_file]
    }

    return $input_par_file
}
#


## 

proc unused_scrib_lib_regression_preferences_output_type {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(output_type)] {
	return $regression_prefs(output_type)
    }
    
    return "UNKNOWN"
}

## 

proc scrib_lib_regression_preferences_output_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(output_file)] {
	set ofile  $regression_prefs(output_file)
    } else {
	set ofile "test.output"
    }
    
    if {"relative" == [file pathtype $ofile]} {
	set ofile [file join [file dirname $prefs_file] $ofile]
    }
    
    return $ofile
}
#
proc scrib_lib_regression_preferences_output_geo_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(output_geo_file)] {
	set output_geo_file  $regression_prefs(output_geo_file)
    } else {
	set output_geo_file "output.tin"
    }
    
    if {"relative" == [file pathtype $output_geo_file]} {
	set output_geo_file [file join [file dirname $prefs_file] $output_geo_file]
    }
    
    return $output_geo_file
}
#
proc scrib_lib_regression_preferences_std_geo_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_geo_file)] {
	set std_geo_file  $regression_prefs(std_geo_file)
    } else {
	set std_geo_file "std_output.tin"
    }
    
    if {"relative" == [file pathtype $std_geo_file]} {
	set std_geo_file [file join [file dirname $prefs_file] $std_geo_file]
    }
    
    return $std_geo_file
}
#
proc scrib_lib_regression_preferences_output_msh_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(output_msh_file)] {
	set output_msh_file  $regression_prefs(output_msh_file)
    } else {
	set output_msh_file "output.uns"
    }
    
    if {"relative" == [file pathtype $output_msh_file]} {
	set output_msh_file [file join [file dirname $prefs_file] $output_msh_file]
    }
    
    return $output_msh_file
}
#
proc scrib_lib_regression_preferences_std_msh_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_msh_file)] {
	set std_msh_file  $regression_prefs(std_msh_file)
    } else {
	set std_msh_file "std_output.uns"
    }
    
    if {"relative" == [file pathtype $std_msh_file]} {
	set std_msh_file [file join [file dirname $prefs_file] $std_msh_file]
    }
    
    return $std_msh_file
}
#
proc scrib_lib_regression_preferences_ansys_input_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(ansys_input_file)] {
	set ansys_input_file  $regression_prefs(ansys_input_file)
    } else {
	set ansys_input_file "ansys.in"
    }
    
    if {"relative" == [file pathtype $ansys_input_file]} {
	set ansys_input_file [file join [file dirname $prefs_file] $ansys_input_file]
    }
    
    return $ansys_input_file 
}
#
proc scrib_lib_regression_preferences_std_ansys_input_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_ansys_input_file)] {
	set std_ansys_input_file  $regression_prefs(std_ansys_input_file)
    } else {
	set std_ansys_input_file "std_ansys.in"
    }
    
    if {"relative" == [file pathtype $std_ansys_input_file]} {
	set std_ansys_input_file [file join [file dirname $prefs_file] $std_ansys_input_file]
    }
    
    return $std_ansys_input_file 
}
#
proc scrib_lib_regression_preferences_dyna_input_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(dyna_input_file)] {
	set dyna_input_file  $regression_prefs(dyna_input_file)
    } else {
	set dyna_input_file "ls_dyna.k"
    }
    
    if {"relative" == [file pathtype $dyna_input_file]} {
	set dyna_input_file [file join [file dirname $prefs_file] $dyna_input_file]
    }
    
    return $dyna_input_file 
}
#
proc scrib_lib_regression_preferences_std_dyna_input_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_dyna_input_file)] {
	set std_dyna_input_file  $regression_prefs(std_dyna_input_file)
    } else {
	set std_dyna_input_file "std_ls_dyna.k"
    }
    
    if {"relative" == [file pathtype $std_dyna_input_file]} {
	set std_dyna_input_file [file join [file dirname $prefs_file] $std_dyna_input_file]
    }
    
    return $std_dyna_input_file 
}
#
proc scrib_lib_regression_preferences_nastran_input_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(nastran_input_file)] {
	set nastran_input_file  $regression_prefs(nastran_input_file)
    } else {
	set nastran_input_file "nastran.dat"
    }
    
    if {"relative" == [file pathtype $nastran_input_file]} {
	set nastran_input_file [file join [file dirname $prefs_file] $nastran_input_file]
    }
    
    return $nastran_input_file 
}
#
proc scrib_lib_regression_preferences_std_nastran_input_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_nastran_input_file)] {
	set std_nastran_input_file  $regression_prefs(std_nastran_input_file)
    } else {
	set std_nastran_input_file "std_nastran.k"
    }
    
    if {"relative" == [file pathtype $std_nastran_input_file]} {
	set std_nastran_input_file [file join [file dirname $prefs_file] $std_nastran_input_file]
    }
    
    return $std_nastran_input_file 
}
#
proc scrib_lib_regression_preferences_abaqus_input_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(abaqus_input_file)] {
	set abaqus_input_file  $regression_prefs(abaqus_input_file)
    } else {
	set abaqus_input_file "abaqus.inp"
    }
    
    if {"relative" == [file pathtype $abaqus_input_file]} {
	set abaqus_input_file [file join [file dirname $prefs_file] $abaqus_input_file]
    }
    
    return $abaqus_input_file 
}
#
proc scrib_lib_regression_preferences_std_abaqus_input_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_abaqus_input_file)] {
	set std_abaqus_input_file  $regression_prefs(std_abaqus_input_file)
    } else {
	set std_abaqus_input_file "std_abaqus.inp"
    }
    
    if {"relative" == [file pathtype $std_abaqus_input_file]} {
	set std_abaqus_input_file [file join [file dirname $prefs_file] $std_abaqus_input_file]
    }
    
    return $std_abaqus_input_file 
}
#
proc scrib_lib_regression_preferences_output_interface {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(solver_input_file)] {
	set solver_input_file  $regression_prefs(solver_input_file)
    } else {
	set solver_input_file "solver.inp"
    }
    
    if {"relative" == [file pathtype $solver_input_file]} {
	set solver_input_file [file join [file dirname $prefs_file] $solver_input_file]
    }
    
    return $solver_input_file 
}
#
proc scrib_lib_regression_preferences_std_output_interface {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_solver_input_file)] {
	set std_solver_input_file  $regression_prefs(std_solver_input_file)
    } else {
	set std_solver_input_file "std_solver.inp"
    }
    
    if {"relative" == [file pathtype $std_solver_input_file]} {
	set std_solver_input_file [file join [file dirname $prefs_file] $std_solver_input_file]
    }
    
    return $std_solver_input_file 
}
#
proc scrib_lib_regression_preferences_output_blk_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(output_blk_file)] {
	set output_blk_file  $regression_prefs(output_blk_file)
    } else {
	set output_blk_file "output.blk"
    }
    
    if {"relative" == [file pathtype $output_blk_file]} {
	set output_blk_file [file join [file dirname $prefs_file] $output_blk_file]
    }
    
    return $output_blk_file
}
#
proc scrib_lib_regression_preferences_std_blk_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_blk_file)] {
	set std_blk_file  $regression_prefs(std_blk_file)
    } else {
	set std_blk_file "std_output.blk"
    }
    
    if {"relative" == [file pathtype $std_blk_file]} {
	set std_blk_file [file join [file dirname $prefs_file] $std_blk_file]
    }
    
    return $std_blk_file
}
#
proc scrib_lib_regression_preferences_output_fbc_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(output_fbc_file)] {
	set output_fbc_file  $regression_prefs(output_fbc_file)
    } else {
	set output_fbc_file "output.fbc"
    }
    
    if {"relative" == [file pathtype $output_fbc_file]} {
	set output_fbc_file [file join [file dirname $prefs_file] $output_fbc_file]
    }
    
    return $output_fbc_file
}
#
proc scrib_lib_regression_preferences_std_fbc_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_fbc_file)] {
	set std_fbc_file  $regression_prefs(std_fbc_file)
    } else {
	set std_fbc_file "std_output.fbc"
    }
    
    if {"relative" == [file pathtype $std_fbc_file]} {
	set std_fbc_file [file join [file dirname $prefs_file] $std_fbc_file]
    }
    
    return $std_fbc_file
}
#
proc scrib_lib_regression_preferences_output_par_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(output_par_file)] {
	set output_par_file  $regression_prefs(output_par_file)
    } else {
	set output_fbc_file "output.par"
    }
    
    if {"relative" == [file pathtype $output_par_file]} {
	set output_par_file [file join [file dirname $prefs_file] $output_par_file]
    }
    
    return $output_par_file
}
proc scrib_lib_regression_preferences_std_par_file {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(std_par_file)] {
	set std_par_file  $regression_prefs(std_par_file)
    } else {
	set std_fbc_file "std_output.par"
    }
    
    if {"relative" == [file pathtype $std_par_file]} {
	set std_par_file [file join [file dirname $prefs_file] $std_par_file]
    }
    
    return $std_par_file
}


proc scrib_lib_regression_preferences_comparison_type {prefs_file} {
    
    catch {source $prefs_file}
    
    if [info exists regression_prefs(comparison_type)] {
	return $regression_prefs(comparison_type)
    }
    
    return "all"
}

proc scrib_lib_regression_run_comparison_internal {output_type outout_file std_output_file comparison_type} {
    
    # --> Run comparison
    
    set stat "UNKNOWN"
    
    switch -- $comparison_type {
	"" - none { return "SUCCESS" }
	exact - fuzzy {
	    
	    if [catch {ic_regression_test run $std_output_file -$comparison_type -info "" double 0 "" domain} stat] {
		mess "Got stat from ic_regression_test: $stat\n" red
	    }
	    if {1 == $stat} { set stat "SUCCESS" } else { set stat "FAILURE" } ;# translate to "regression-speak"
	}
	all - strict - quality {
	    mess "Warning: cannot yet handle comparison type of $comparison_type\n" red	
	}
	default {
	    mess "Warning: could not handle comparison type of $comparison_type\n" red
	}
    }
    
    switch -- $stat {
	SUCCESS {
	    set text "SUCCESS: The $output_type output passed the \"$comparison_type\" comparison testing\n"
	}
	FAILURE {
	    set text "FAILURE: The $output_type output did not pass the \"$comparison_type\" comparison testing\n"
	}
	UNKNOWN - default {
	    set text "UNKNOWN: There is no comparison information to report: $stat\n"
	    set stat "SUCCESS"
	}
    }
    
    # --> Put results in the regression log
    
    mess "$text" [expr {("FAILURE" == $stat) ? "red" : "blue"}]
    #scrib_lib_utils_append_text_to_file $text $regression_log
    
    return $stat
}

## This version should be called with a mesh loaded, within med_batch, and with a med_interp

proc scrib_lib_regression_run_comparison {prefs_file {comparison_type ""}} {
    
    set     output_type [scrib_lib_regression_preferences_output_type $prefs_file]
    set     output_file [scrib_lib_regression_preferences_output_file $prefs_file]
    set std_output_file [scrib_lib_regression_preferences_std_output_file $prefs_file]    
    
    if {"" == $comparison_type} {
	set comparison_type [scrib_lib_regression_preferences_comparison_type $prefs_file]
    }
    
    return [scrib_lib_regression_run_comparison_internal $output_type $output_file $std_output_file $comparison_type]
}

##

proc scrib_lib_regression_preferences_exectime {prefs_file} {
    
    set std_exectime -1
    
    if [catch {source $prefs_file} res] {
	mess "Warning: could not get the exectime preferences: $res\n" red
	return $std_exectime
    }
    
    if [info exists regression_prefs(std_exectime)] {
	set std_exectime $regression_prefs(std_exectime)
    }
    
    return $std_exectime
}

##

proc scrib_lib_regression_preferences_maxtime {prefs_file} {
    
    set max_time 0
    
    if [catch {source $prefs_file} res] {
	mess "Warning: could not get the max_time preferences: $res\n" red
	return $max_time
    }
    
    if [info exists regression_prefs(max_time)] {
	set max_time $regression_prefs(max_time)
    }
    
    return $max_time
}

## Returns number of seconds in every case of <em>what</em> FIXME with "how"

proc scrib_lib_regression_exectime {what} {
    global scrib_global_start_time scrib_global_stop_time
    
    set rel_exectime -1
    
    switch $what {
	begin - start {
	    set scrib_global_start_time [clock seconds]
	    if [info exists scrib_global_stop_time] {
		unset scrib_global_stop_time
	    }
	    # if starting, return the start-time in 
	    return [clock format $scrib_global_start_time]
	}
	get {
	    if ![info exists scrib_global_start_time] {
		return -1
	    }
	    if [info exists scrib_global_stop_time] {
		# if the clock has been stopped
		set rel_exectime [expr $scrib_global_stop_time - $scrib_global_start_time]
	    }
	    # the clock is still running
	    set rel_exectime [expr [clock seconds] - $scrib_global_start_time]	
	}
	stop {
	    # stop the clock and return the time
	    set scrib_global_stop_time [clock seconds]
	    set rel_exectime [expr $scrib_global_stop_time - $scrib_global_start_time]
	}
	end - finish {
	    if {![info exists scrib_global_start_time] || ("" == $scrib_global_start_time)} {
		# the clock has not been started
		return -1
	    }
	    if {[info exists scrib_global_stop_time] && ("" != $scrib_global_stop_time)} {
		# there is a current stop time; use that
		return  [expr $scrib_global_stop_time - $scrib_global_start_time]
	    } else {
		# there is no stop time
		set scrib_global_stop_time [clock seconds]
	    }
	    set rel_exectime [$scrib_global_stop_time - $scrib_global_start_time]
	    catch {unset scrib_global_start_time}
	    catch {unset scrib_global_stop_time}
	}
	default {
	    mess "Warning: not sure what you wanted to do with exectime based on $what\n" red
	    return -1
	}
    }
    
    return $rel_exectime
}

## 

proc scrib_lib_regression_compare_exectime {prefs_file regression_log} { 

    # --> Get the current and standard exectime
    
    set cur_exec_time [scrib_lib_regression_exectime "get"]
    set std_exec_time [scrib_lib_regression_preferences_exectime $prefs_file]
    
    if {(0 <= $std_exec_time) && (0 <= $cur_exec_time)} {
	
	if {$cur_exec_time <= $std_exec_time} {
	    set text "SUCCESS: The test completed in less time ($cur_exec_time) than required ($std_exec_time)\n"
	    set stat "SUCCESS"
	} else {
	    set text "FAILURE: The test did NOT complete in less time (${cur_exec_time}s) than required (${std_exec_time}s)\n"
	    set stat "FAILURE"
	}
	
    } else {
	set text "UNKNOWN: There is no test execution time to report ($cur_exec_time; $std_exec_time)\n"
	set stat "SUCCESS"
    }
    
    # --> Put results in the regression log    
    
    mess "$text\n" [expr {("FAILURE" == $stat) ? "red" : ""}]
    scrib_lib_utils_append_text_to_file $text $regression_log
    
    return $stat
}

##

proc scrib_lib_regression_preferences_exectime {prefs_file} {
    
    set std_exectime -1
    
    if [catch {source $prefs_file} res] {
	mess "Warning: could not get the exectime preferences: $res\n" red
	return $std_exectime
    }
    
    if [info exists regression_prefs(std_exectime)] {
	set std_exectime $regression_prefs(std_exectime)
    }
    
    return $std_exectime
}

## Get the maintainer of a module or prefs_file

proc scrib_lib_regression_module_maintainers {module prefs_file} {

    set prefs_file [file join $module $prefs_file]    
    set maintainers [scrib_lib_regression_preferences_module_maintainers $prefs_file]
    if {"" == $maintainers} {
	set maintainers [scrib_lib_regression_cvs_module_maintainers $module]
    }
    if {"" == $maintainers} {
	set maintainers "narugopal.ghata" ;# ghata
    }
    return $maintainers
}

##  Try getting the maintainers file from the ~cvs/$module/maintainer

proc scrib_lib_regression_cvs_module_maintainers {module} {    
    # FIXME
    # we should actually be getting this maintainers file from the ~cvs/$module/maintainer    
    return ""
}

proc scrib_lib_regression_preferences_module_maintainers {prefs_file} {
    
    catch {source $prefs_file}
    
    set maintainers ""
    if [info exists regression_prefs(maintainers)] {
	set maintainers $regression_prefs(maintainers)
    }
    
    return $maintainers
}

## Report the bug to the maintainers with all of the information

proc scrib_lib_regression_report_bug {module casemodel plat maintainers bugfile} {
    
    set stat 0
    
    set rpt [cmd_tempfile "bugrpt"]
    set fp [open $rpt a]
    puts $fp "The following problems occurred in $module on $plat\n"
    puts $fp "The location of this directory is $casemodel"
    close $fp
    
    if [file exists $bugfile] {
	set fp [open $rpt a]
	
	set bgfp [open $bugfile r]
	#puts $fp "\n[read $bgfp]\n"
	while {[gets $bgfp line] > 0} {
	    puts $fp $line
	}
	close $bgfp
	
	close $fp
    }
    
    set subject "Problem in $module/$casemodel on $plat"
    set stat [scrib_lib_mail_send_filebody $maintainers $subject $rpt]
    
    return $stat
}

## Scrape a replay file for ic_* commands

proc scrib_lib_regression_get_ic_procs_from_replay {replay_file} {
    
    if ![file exists $replay_file] {
	stdout "Error: could not find replay file $replay_file from which to get proc names\n" red
	return ""
    }
    
    set list_of_commands ""
    
    set chunks [scrib_lib_utils_slurp_file_into_list $replay_file]
    
    foreach piece $chunks {
	
	if [string equal -length 3 $piece "ic_"] {
	    lappend list_of_commands $piece
	}
    }
    
    return [lsort -unique $list_of_commands]
}

## Get all platforms for testing

proc scrib_lib_regression_get_platforms {{site ""}} {
    global pune_host_list berkeley_host_list livonia_host_list scrib_regression_site
    
    set default_plats [list sgi64 sol7 sol64 hp11 hpit linux linux64 ibm]
    
    if {"" == $site} {
	if [info exists scrib_regression_site] {
	    set site $scrib_regression_site
	} else { return $default_plats }
    }
    
    if ![info exists ${site}_host_list] {
	stdout "Error: cannot find host list for site \"$site\"\n" red
	return $default_plats
    }
    
    return [array names ${site}_host_list]
}

## these are kind of work-a-rounds

proc scrib_lib_regression_set_site {site} {
    global scrib_regression_site
    set scrib_regression_site $site
}
proc scrib_lib_regression_get_site {} {
    global scrib_regression_site
    if [info exists scrib_regression_site] { return $scrib_regression_site }
    return ""
}

## Reconstruct the current ICEM_ACN based on the given platform

proc scrib_lib_regression_get_icemacn_for_platform { platform } {
    global env
    
    if ![info exists env(ICEM_ACN)] {
	return ""
    }
    
    # FIXME--this should be smarter--this may break if conventions change
    # replace the platform in a string like:
    #   /home/charles/icemcfd/TESTING/AIEnv2.0/4.3-hp11
    #
    
    set base_acn [split $env(ICEM_ACN) "-"]
    
    set new_acn "[lrange $base_acn 0 end-1]-${platform}"
    
    return $new_acn
}


## 

proc scrib_lib_regression_get_host {plat {site ""}} {
    global scrib_regression_site pune_host_list berkeley_host_list
    
    if {("" == $site) && [info exists scrib_regression_site]} {
	set site $scrib_regression_site
    }
    
    if ![info exists ${site}_host_list] {
	stdout "Error: cannot find host list for site \"$site\"\n" red
	return ""
    }
    array set current_hosts [array get ${site}_host_list]
    
    if ![info exists current_hosts($plat)] {
	stdout "Error: cannot find platform \"$plat\" in host list for site \"$site\"\n" red
	return ""
    }
    
    return $current_hosts($plat)
}


## 

proc scrib_lib_regression_get_plat { host {site ""} } {
    global scrib_regression_site pune_host_list berkeley_host_list
    
    if {("" == $site) && [info exists scrib_regression_site]} {
	set site $scrib_regression_site
    }
    
    if ![info exists ${site}_host_list] {
	stdout "Error: cannot find host list for site \"$site\"\n" red
	return ""
    }
    
    array set current_hosts [array get ${site}_host_list]
    
    foreach plat [array names current_hosts] {
	
	if {$host == $current_hosts($plat)} {
	    return $plat
	}
    }
    
    return ""
}


# -----------------------------------------------------------------------------------------

## FIXME: this is from Naru's stuff, but not incorporated yet !!! Not sure if we'll sure it or not --jka

proc scrib_lib_regression_kill_proc {pid max_exec_time} {
    
    set user [exec whoami]
    exec ps -u $user >proc_info
    
    set chk_proc [open proc_info r]
    
    foreach line $chk_proc {
	set  split_proc [split $line]
	set procs [lindex $split_proc 0]
	if {[scan $procs "%f"]} && {[expr ($procs - $pid)=0]} {
	    after $max_exec_time "exec kill $pid"
	}
    }
}


# ----------------------------------------------------------------------------------------------

##

proc notyet_scrib_lib_regression_bsm_test {} {
    
    TestBenchReport::createDocument "NAME $test TITLE Hello"
    TestBenchReport::addTestcase first "START today RESULT good"
    TestBenchReport::addTestcase second "START late RESULT bad"
    TestBenchReport::writeReport ${test}a.html html
    TestBenchReport::writeReport ${test}a.xml xml
    TestBenchReport::closeDocument
}


# ----------------------------------------------------------------------------------------------


##

proc scrib_lib_regression_bsm_test_commands {commands platforms report_bugs run_comparison} {
    
    foreach cmd $commands {
	
	foreach plat $platforms {
	    
	    scrib_lib_regression_bsm_test_command $cmd $plat $report_bugs $run_comparison
	}
    }
}


## 

proc scrib_lib_regression_bsm_test_command {command platform report_bugs run_comparison} {
    
    if {$command != [info commands $command]} {
	mess "Error: could not find command \"$command\"\n" red
	return [scrib_lib_exec_misconfig]
    }
    
    set host [scrib_lib_regression_get_host $platform]
    if {"" == $host} {
	
	set site [scrib_lib_regression_get_site]
	mess "Error: cannot find host for platform \"$platform\" at site \"$site\"\n" red
	return MISCONFIG
	
    } elseif {[info hostname] != $host} {
	
	set remote_icemacn [scrib_lib_regression_get_icemacn_for_platform $platform]
	mess "Running model on remote host \"$host\" for platform \"$platform\" with ICEM_ACN \"$remote_icemacn\"\n"
	
    } else {
	
	set host "localhost"
	global env
	if [info exists env(ICEM_ACN)] {
	    set remote_icemacn $env(ICEM_ACN)
	} else {
	    set remote_icemacn [scrib_lib_regression_get_icemacn_for_platform $platform]
	}
    }
    
    # --> Make a list of files existing before, so we can remove tmp files later
    
    set files_before_exec [glob -nocomplain -type f *]
    
    # --> Search for the preferences file; get script file
    
    set prefs_file   [scrib_lib_regression_preferences_file [pwd]]
    set script       [scrib_lib_regression_generate_script $prefs_file "" $run_comparison]
    set job_token    [scrib_lib_exec_get_unique_job_token $command]
    
    # --> Create log file name
    
    set regression_log [scrib_lib_utils_logfile "." "log.bsm.regression"]
    
    # --> Run MED with script and regression log, tracking time
    
    set start_time [scrib_lib_regression_exectime start]
    # FIXME
mess "command: $command\n" red
return FAILURE
    set stat [scrib_lib_regression_test_command $command $job_token "" $med_args $exec_args]
    # FIXME
    set time_elapsed [scrib_lib_regression_exectime stop]
    
    # !! watch the return value of scrib_lib_exec_medbatch !! --fixme --jka !!
    
    if {1 != $stat} {
	
	# (but it's possible that the it waas not the test itself that failed)
	
	set stat [scrib_lib_exec_failure]
	
    } else {
	
	# check the tail of the log. last line should be either "SUCCESS" or "FAILURE"
	
	catch {exec tail -n 1 $logfile} stat
	if {("SUCCESS" != $stat) && ("MISCONFIG" != $stat)} {
	    set stat "FAILURE"
	}
    }
    
    # --> *NOW* we can put the execution time to the logfile
    
    set text "Execution time was: [scrib_lib_regression_exectime stop] seconds"
    mess "$text\n"
    scrib_lib_utils_append_text_to_file $text $regression_log
    
    # --> If stat==0, perhapse mail report to maintainer
    #     fixme: check that the job was not simply killed
    
    if [scrib_lib_exec_succeeded $stat] {
	
	# --> Maybe report something about time_profiling
	
	set stat [scrib_lib_regression_compare_exectime $prefs_file $regression_log]    
    }
    
    # --> If something failed, perhaps mail what is noted in "$regression_log" to maintainer
    
    if {[scrib_lib_exec_failed $stat] && $report_bugs} {
	set maintainers "jason" ;# ben simon
	scrib_lib_regression_report_bug $command [pwd] $platform $maintainers $regression_log
    }
    
    # --> Remove the list of files not existing before, fixme!!
    
    scrib_lib_utils_remove_files_not_in_list $files_before_exec
    
    mess "[string toupper $stat]: testing command \"$command\" on $platform\n\n" [expr {("FAILURE" == $stat) ? "red" : "blue"}]
    
    return $stat
}


#-------------------------------------------------------------------------------------




















# -----------------------------------------------------------------------------------------

## FIXME: this is from Naru's stuff, but not incorporated yet !!! Pretty sure we won't use this --jka

proc scrib_lib_regression_reg_test {replay_file} {
    
    rsh $host "cd $CASE_DIR/$plat/; setenv ICEM_ACN $ICEM_LOCATION/4.3-$plat; \
               $ICEM_ACN/bin/icemcfd -batch -script ../standard/${REPLAY_FILE} |& tee -i output.rpt.$plat; \
               echo \"Done replay\"; \
               $MODULE_DIR/err_catch.tcl $plat $case $MODULE_DIR $CASE_DIR; \
               $QA_BIN/rm_empty_log.tcl $plat $case; \
               rm -rf core; \
               rsh maxi $QA_BIN/report_bugs $case $plat $CASE_DIR/$plat/error.$case.$plat $module $MODULE_DIR" &
}

## FIXME: this is from Naru's stuff, but not incorporated yet !!! Pretty sure we won't use this --jka

proc scrib_lib_regression_err_catch {plat module} {
    
    set mess_file [open output.rpt.$plat r]
    set err_file [open error.$module.$plat w]
    
    if {([file exists output.tin]) && ([ file size output.tin] > 0)} {
	set ifile [open output.tin r]
	set ifilewv [open output.wv.tin w]
	
	set lineno 0
	while {[gets $ifile iline] >=0} {
	    incr lineno
	    set solv_info [split $iline]
	    set err_solv [lindex $solv_info 0]
            if {![regexp // $err_solv]} {
		puts $ifilewv $iline
	    }
	}
	seek $ifile 0 start
	close $ifile; close $ifilewv
    }
    
    if {([file exists output.tin]) && ([ file size output.tin] > 0)} {
	set ifilewvr [open output.wv.tin r]
	set std_file [open output_std.wv.tin r]
	set ofile [open diff_tetin w]
	
	set line_nu 0
	while {[gets $std_file std_file_line] >=0} {
	    incr line_nu
            if {[ gets $ifilewvr ifile_wv_line] >=0} {
		if {[string compare $std_file_line $ifile_wv_line] !=0} {
		    puts $ofile "Change in the Tetin file: Please\
              check $module on $plat \n"
		}
	    }
	}
	
	seek $ifilewvr 0 start
	seek $std_file 0 start
	close $ifilewvr
	close $ofile
	close $std_file
    }
    
    if {([file exists output.tin]) && ([ file size output.tin ] > 0)} {
        set diff_file [open diff_tetin r]
	set lineno 0
	set line_nu 0
	while {[gets $diff_file diff_line] >=0} {
	    incr lineno
	    if {[regexp Change $diff_line]} {
		puts $err_file $diff_line
            }
	}
	while {[gets $mess_file mess_line] >=0} {
	    incr line_nu
	    if {[regexp error $mess_line]} {
		puts $err_file $mess_line
	    } elseif {[regexp FAIL $mess_line]} {
		puts $err_file $mess_line
	    }
	}
	seek $mess_file 0 start
	seek $diff_file 0 start
	close $diff_file
    }
    
    if {(![file exists output.tin]) || ([file size output.tin] == 0)} {
	set line 0
	while {[gets $mess_file line_no] >=0} {
	    incr line
	    puts $err_file $line_no
	}
    }
    close $err_file
    close $mess_file
}

## FIXME: this is from Naru's stuff, but not incorporated yet !!! Pretty sure we won't use this --jka

proc scrib_lib_regression_rm_empty_log {plat module} {
    
    if {([ file exists error.$module.$plat]) && ([ file size error.$module.$plat ] == 0)} {
	exec rm -r error.$module.$plat
    }
}

## Test the given models

proc unused__scrib_lib_regression_test_models {models platforms {report_bugs 0} {comparison_type ""} {xml_report ""}} {
    
    mess "\nTesting models on platforms \"$platforms\"\n\n"
    
    if {"" != $xml_report} {
	TestBenchReport::createDocument "NAME scrib_lib_regression TITLE hi TYPE thorough \
                                         APPLICATION icem_cfd VERSION testing MACHINE many"
	set xml_ref "EXISTING_DOM"
    }
    
    set old_dir [pwd]
    
    set nfailed    0
    set nsucceeded 0
    set nunkown    0
    foreach model $models {	
	foreach plat $platforms {
	    set stat [scrib_lib_regression_model_test $model $plat $report_bugs $comparison_type $xml_ref]
	    switch -- $stat {
		SUCCESS {
		    incr nsucceeded
		}
		FAILURE {
		    incr nfailed
		}
		MISCONFIG - default {
		    incr nunkown
		}
	    }
	}
    }
    
    cd $old_dir
    
    if {"" != $xml_report} {
	TestBenchReport::writeReport $xml_report "xml"
	TestBenchReport::closeDocument
    }
    
    return FIXME
}
