#
# This code memics med_gui for output interfaces
# invoke script by
#
# wish gateway_output.tcl <fam desc list> <proj> <domain file> <famboco>
# <boco flag> <famtopo> <topomulcadout> <solver> <batch mode args>
#
# boco flag -- 0 -- clean family boco file
# boco flag -- 1 -- edit family boco file
# boco flag -- 2 -- use existing family boco file as is
#
# All files must be in the proj_dir due to the limitation of the possibility
# of spaces being in the path on windows.
#
# Copyright (c) 2002 ICEMCFD Engineering, a subsidary of ANSYS
# Author: Forest Rouse
#

# Grab main gateway interface proc file

set gateway_dir ""
if [info exists env(GATEWAY_DIR)] {
    lappend gateway_dir $env(GATEWAY_DIR)
}
lappend gateway_dir .

if [info exists env(ICEM_ACN)] {
    lappend gateway_dir $env(ICEM_ACN)/lib/gateway_gui
}

foreach f {
    gateway
} {
    set found 0
    foreach gd $gateway_dir {
	if [file exists $gd/$f.tcl] {
	    set ICC_gateway(tempfile) $gd/$f.tcl
	    set stat [catch {source $ICC_gateway(tempfile)} msg]
	    if !$stat {
		set found 1 
		break
	    }
	}
    }
    if !$found {
	error "File $f.tcl not found in gateway_dir list \"$gateway_dir\""
    }
}

# set guibase_dir_list

set guibase_dir_list ""
if [info exists env(GUIBASE_DIR)] {
    lappend guibase_dir_list $env(GUIBASE_DIR)
}

lappend guibase_dir_list guibase ../guibase

if [info exists env(ICEM_ACN)] {
    lappend guibase_dir_list $env(ICEM_ACN)/lib/gateway_gui/guibase
    lappend guibase_dir_list $env(ICEM_ACN)/lib/guibase
}

# bring in all tcl files and withdraw top level window

ICC_gateway_initialize
top_withdraw .

# override defs in family_boco

proc ic_boco_is_loaded {} {
    global file_name
    
    if [info exists file_name(family_boco_loaded)] {
	return $file_name(family_boco_loaded)
    }
    return 0
}

proc ic_boco_fam_dim {fam} {
    set famdesc [ICC_findfam $fam]
    set dim [lindex $famdesc 2]
    set llen [llength $dim]
    if {$llen == 0 || $llen > 1} { return mixed }
    return $dim
}

proc famboco_load_params {args} {}

# Setup files (project, domain, mesh and transfer directories)

set famdesc [lindex $argv 0]
set proj_dir [lindex $argv 1]
if {$proj_dir == ""} {
    set proj_dir [pwd]
}
set old_dir [pwd]
cd $proj_dir

# if the above exists as a file, read file.  Otherwise it is the
# family descriptor list

if [file readable $famdesc] {
    set fp [open $famdesc r]
    set famdesc [read $fp]
    close $fp
}

set ICC_gateway(famdesc) $famdesc
set domain_file [file tail [lindex $argv 2]]
if {$domain_file == ""} {
    set domain_file ICCGateway.uns
}
if ![file exists $domain_file] {
    cd $old_dir
    error "Domain file \"$domain_file\" does not exist."
}

set file_name(project_dir)  $proj_dir
set file_name(domains_dir)  $proj_dir
set file_name(transfer_dir) $proj_dir
set file_name(mesh_dir)     $proj_dir
set file_name(domain)       $domain_file

# Generally the boco file will be constructed using the boco's
# from the CATIA BCs, but for now allow construction using
# our methodology from forms.

set file_name(family_boco) [file tail [lindex $argv 3]]
set ICC_gateway(boco_flag) [lindex $argv 4]

# structured topo files have to be passed in too

set file_name(family_topo) [file tail [lindex $argv 5]]
set file_name(topo_mulcad_out) [file tail [lindex $argv 6]]

# Solver specified?

set sol [lindex $argv 7]

# extract batch mode arguments (ie, setting values external)

set batch_args [lrange $argv 8 end]

# for now
set batch_mode 0

set ret [ICC_gateway_output $sol $batch_args]
cd $old_dir
exit
