#
# This code memics med_gui for output interfaces
# invoke script by
#
# tclsh gateway_output_batch.tcl <proj> <domain file> <solver> <famboco>
# <famtopo> <topomulcadout> <extra batch mode args>
#
# Meant to be used in conjunction with gateway_schema which creates
# a gateway schema file to create an fbc file programatically
# for CATIA (or any other user)
#
# 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
    gateway_solvers
} {
    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 1

proc dialog {args} {}

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

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

set domain_file [file tail [lindex $argv 1]]
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

# solver
set solver [lindex $argv 2]

# FBC file from gateway

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

# structured topo files have to be passed in too

set file_name(family_topo) [file tail [lindex $argv 4]]
set file_name(topo_mulcad_out) [file tail [lindex $argv 5]]
set batch_args [lrange $argv 6 end]

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

set ret [ICC_gateway_solutioninput_batch $solver $batch_args]
cd $old_dir
exit
