# Library functions for the scripting library (scrib)
#
# File: scrib/lib/export.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------
#
# --> if the name of the first proc already exists in interp, return
#
if {"" != [info commands scrib_lib_export_filename_get]} { return }
#
# --> procs in this file depend on the following scrib/lib/ files
#
if 0 {
    # none required yet
    set required_files ""
    scrib_lib_setup_source_files $required_files
}

## needs doc

proc scrib_lib_export_filename_get {export_dir file extension {status normal}} {
    
    #set base $export_dir/[string range $file 0 [expr [string last . $file]-1]]    
    set base [file join $export_dir [file root [file tail $file]]]
    
    set path $base
    switch $status {
	normal { }
	empty  { append path "_no_surfs" }
    }
    append path ".$extension"
    return $path
}

## needs doc

proc scrib_lib_export_cad_byfile {file exec_args} {
    global proc_scrib_data env
    #
    # --> get the export file name for this file
    #
    set exportformat [string totitle $proc_scrib_data(export)]
    set exportfile [scrib_lib_export_filename_get $proc_scrib_data(export_dir) $file \
			$proc_scrib_data(export_extension)]

    set empty_export [scrib_lib_export_filename_get $proc_scrib_data(export_dir) $file \
	    $proc_scrib_data(export_extension) empty]
    #
    # --> if that file already exists, then continue. it must have been created.
    #
    if [file exists $exportfile]   { return 1 }
    if [file exists $empty_export] { return 1 }
    #
    scrib_lib_utils_record_append "------------------------------------\
	    ---------------------------\n \
	    Meshing file $file in ICEM CFD for export to $proc_scrib_data(export)\n"
    #
    # --> run icemcfd in batch with the scripts/mesh_quad.tcl script
    #
    # what the hell is this? --jka
    if ![info exists proc_scrib_data(foreach_script_name)] {
    	set script "mesh_quad.tcl"
	set script2 "mesh_tetra.tcl"
    } else {
	set script $proc_scrib_data(foreach_script_name)
    }
    if ![info exists proc_scrib_data(foreach_script_name2)] {
  	set script2 "mesh_tetra.tcl"
    } else {
	set script2 $proc_scrib_data(foreach_script_name2)
    }
    #
    set proc_scrib_data(batch_script) $script
    #
    # --> this list of values is given to the batch mode run of med
    #
    #     for a standard scrib/script/*.tcl file, use this proc
    #     to get the args for the call to scrib_lib_exec_medbatch
    #
    set proc_scrib_data(model) $file
    set job_token [scrib_lib_exec_get_unique_job_token [file tail [file root $file]]]
    set med_args ""
    set stat [scrib_lib_exec_medbatch $job_token "$script" $med_args $exec_args]
    
    if {0 > $stat} {
	# xxxxxxxx this may not be completely descriptive???... --jka
	return -1
    }
    
    if [file exists $exportfile] {
	#
	# --> everything is ok
	#
	scrib_lib_utils_report_message \
	    "Created $exportformat export file:\n\t[file tail $exportfile]\n"
	#
 	# --> it's possible that the mesher script did not find surfaces,
    	#     and so would have created an empty_export type file
    	#
    } elseif [file exists $empty_export] {
	#
	# --> we have an empty surfs file, so report that
	#
	scrib_lib_utils_report_message "There were no surfs in file $file\n"
	#
    } else {
	#
	# --> otherwise, try running second script file
	#
	scrib_lib_utils_report_message \
		"Could not create $exportformat file:\n\t[file tail $exportfile]\n"
	#
	# --> make a "failed" directory, and copy the file there
	#   
	if ![info exists proc_scrib_data(failed_dir)] {
	    set proc_scrib_data(failed_dir) [file join $proc_scrib_data(wdir) failed_dir]
	}
	if ![file exists $proc_scrib_data(failed_dir)] {
	    file mkdir $proc_scrib_data(failed_dir)
	}
	scrib_lib_utils_report_message \
		"Copying $proc_scrib_data(cadsys) file to $proc_scrib_data(failed_dir)\n"
	catch { file copy [file join $proc_scrib_data(cad_dir) $file] $proc_scrib_data(failed_dir) }
	#
	# --> try running second script if available
	#
	if [file exists $script2] {
	    scrib_lib_utils_report_message "Trying to mesh with [file tail [file rootname $script2]] \n"
	    set stat [scrib_lib_exec_medbatch "$script2" $args record $proc_scrib_data(interactive)]
	    mess "Finished trying [file tail [file rootname $script2]] on the model $file.\n"
	}
    }
    
    mess "Finished with model $file\n\n-------------------------------------------------------\n"
    return $stat
}

## needs doc

proc scrib_lib_export_cad_bydir {} {
    global proc_scrib_data
    
    set dir_or_file_list $proc_scrib_data(cad_dir)
    if {[info exists proc_scrib_data(cad_selmethod)] && ("" != $proc_scrib_data(cad_selmethod))} {
	switch -- $proc_scrib_data(cad_selmethod) {
	    dir {
		set dir_or_file_list $proc_scrib_data(cad_dir)
	    }
	    files {
		set dir_or_file_list $proc_scrib_data(cad_files)
	    }
	}
    }
    
    set command "scrib_lib_export_cad_byfile"
    set files [scrib_lib_exec_foreach_file $command $proc_scrib_data(cadsys) \
		   $dir_or_file_list $proc_scrib_data(wdir)]
    
    stdout "\nFinished trying to import, mesh and export $proc_scrib_data(cadsys) files:\n"
    foreach f $files { stdout "\t$f\n" }
    return $files
}

proc scrib_lib_export_cad_file {file exec_args} {
    global proc_scrib_data env
    #
    # --> get the export file name for this file
    #
    set exportformat [string totitle $proc_scrib_data(export)]
    set exportfile [scrib_lib_export_filename_get $proc_scrib_data(export_dir) $file \
			$proc_scrib_data(export_extension)]

    set empty_export [scrib_lib_export_filename_get $proc_scrib_data(export_dir) $file \
	    $proc_scrib_data(export_extension) empty]
    #
    # --> if that file already exists, then continue. it must have been created.
    #
    if [file exists $exportfile]   { return 1 }
    if [file exists $empty_export] { return 1 }
    #
    scrib_lib_utils_record_append "------------------------------------\
	    ---------------------------\n \
	    Meshing file $file in ICEM CFD for export to $proc_scrib_data(export)\n"
    #
    # --> run icemcfd in batch with the scripts/mesh_quad.tcl script
    #
    # what the hell is this? --jka
    if ![info exists proc_scrib_data(foreach_script_name)] {
    	set script "mesh_quad.tcl"
	set script2 "mesh_tetra.tcl"
    } else {
	set script $proc_scrib_data(foreach_script_name)
    }
    if ![info exists proc_scrib_data(foreach_script_name2)] {
  	set script2 "mesh_tetra.tcl"
    } else {
	set script2 $proc_scrib_data(foreach_script_name2)
    }
    #
    set proc_scrib_data(batch_script) $script
    #
    # --> this list of values is given to the batch mode run of med
    #
    #     for a standard scrib/script/*.tcl file, use this proc
    #     to get the args for the call to scrib_lib_exec_medbatch
    #
    set proc_scrib_data(model) $file
    set job_token [scrib_lib_exec_get_unique_job_token [file tail [file root $file]]]

    set custom_script [file join "$logdir" ${job_token}_med_script.tcl]

   
    scrib_lib_exec_build_script "$script"  $custom_script  
    

#    return $custom_script
    
    mess "Finished with model $file\n\n-------------------------------------------------------\n"

}
