proc fluent6_write_input {args} {
    global file_name fluent6_output_path icem_fluent 
    global confname file_name
    
    if [aie_wb2_is_running] {
        set outfile $file_name(transfer_dir)/[aie_get_global wb_initProjName].msh 
    } else {
        set outfile $file_name(transfer_dir)/fluent
    }
    
    set result 0
    set dim2d 0
    set binary_flag 0
    set bocofile ""
    set scale 0
    set x 1.0
    set y 1.0
    set z 1.0
    set ignore_couplings 0
	set per_info [ic_geo_get_periodic_data]

	set periodic_flag 0
    if {[lsearch $per_info none] ==-1 } {
        set periodic_flag 1
    }
	
    # Now set the values from the arguments if any
    foreach {n v} $args { set $n $v }
    set is_batch [llength $args]
    
    if !$is_batch {
	set domain [domain_select $file_name(domains_dir) \
			"Select an unstructured domain." unstruct 0]
	if {$domain == {}} return
	
	get_boco_topo
        
	set family_boco $file_name(mesh_dir)/family_boco
	
	set params {}
	lappend params {dim2d -opt {{3D 0} {2D 1}} \
			    -title "Grid dimension"}
	lappend params {scale -opt {{"Yes" 1} {"No" 0}} \
			    -title "Scaling"} 
	lappend params {x -type string -active {$scale} \
			    -title "x scaling factor"}
	lappend params {y -type string -active {$scale} \
			    -title "y scaling factor"}
	lappend params {z -type string -active {$scale} \
			    -title "z scaling factor"}
	lappend params {binary_flag -opt {{Yes 1} {No 0}} \
			    -title "Write binary file"}
	lappend params {ignore_couplings -opt {{Yes 1} {No 0}} \
			    -title "Ignore couplings"}
	lappend params {bocofile -type path -title "Boco file"}
	lappend params {outfile -type path -title "Output file"}
#	lappend params {result -opt {{"Yes" 1} {"No" 0}} \
#			    -title "Create .dat file" \
#			    -help  "Create a result data file (.dat)"}
	
	if {![dialog .fluent6 "Fluent V6" \
		  "Please edit the following Fluent_V6 options." $params]} return
	
	# strip extension in case user specified it
	if {[file extension $outfile] == ".msh"} {
	    set outfile [file rootname $outfile]
	}
	
	if {[file exists $outfile.msh] && \
		[query_question .query "Overwrite $outfile.msh?"] != "*okay*"} return
	
	if {$result && [file exists $outfile.dat] && \
		[query_question .query "Overwrite $outfile.dat?"] != "*okay*"} return
	
    }
    
    if ![info exists domain] {
	error "No domain was given"
	return
    }

    if {![file exists $bocofile]} {
	errormsg "File $bocofile doesn't exist"
#	return
    }

    set command "\"$fluent6_output_path\" -dom \"$domain\"" 
       if {[file exists $bocofile]} {set command "$command -b \"$bocofile\""}
       if $dim2d {set command "$command -dim2d"}
       if $binary_flag {set command "$command -bin"}
       if $scale {set command "$command -scale $x,$y,$z"}
       if $ignore_couplings {set command "$command -nocoupling"}
	   if {$periodic_flag} {set command "$command -per \"$per_info\""}
       set command "$command \"$outfile\""

    
    #runcom $command ""
    
    update
    mess "Running: $command\n"
    busy 1
    if [catch {
	   eval exec $command
    } res] {
	   user_error $res
	   mess $res\n red
    } else {
        mess "$res\n" blue
    }
    busy 0
    mess Done\n
    
    if [ic_uns_is_loaded] {
	  if {[ic_uns_num_couplings]==0} {
        ic_undo_group_begin 
        ic_uns_create_diagnostic_edgelist 1
        set ret [ic_uns_diagnostic subset all diag_type uncovered fix_fam FIX_UNCOVERED diag_verb {Uncovered faces} fams {} busy_off 1 quiet 1]
        if [lindex $ret 0] {
            if {[ic_uns_dimension] == 2} {
                mess "WARNING: Mesh has uncovered edges. Fluent needs a complete boundary (lines in 2D) or it will give a variety\
                    of errors and not read in the mesh! If this was 2D Hexa, perhaps your edges are not associated with perimeter curves\n" red
            } else {
                mess "WARNING: Mesh has uncovered faces. Fluent needs a complete boundary (shells in 3D) or it will give a variety\
                    of errors and not read in the mesh!\n" red
            }
        }
        ic_uns_create_diagnostic_edgelist 0
        ic_undo_group_end
      }
    }
}
