
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc tmpname {} {return .restart.tmp}

proc run_editor {file} {
    global editor xterm_prog xterm_params
    
    if {![file exists $editor]} {
	warningmsg "Can't find editor $editor, trying vi"
	foreach vp {/usr/bin/vi /usr/ucb/vi /usr/bsd/vi /bin/vi} {
	    if [file exists $vp] {set vipath $vp; break}
	}
	if {![info exists vipath]} {
	    errormsg "Can't find vi!  Giving up."
	    return
	}
	set editor $vipath
    }
    if {[string match "*/vi" $editor]} {
	if {![info exists xterm_prog]} {
	    errormsg "Can't find xterm!"
	    return
	}
	eval check_exec $xterm_prog $xterm_params -name $file -title $file \
	    -e /bin/csh -c \{$editor $file\}
    } else {
	check_exec $editor $file
    }
}

proc get_units {} {
    global units
    case "$units" in {
	English {return e}
	Metric {return m}
	default {warningmsg "Bad units: $units"; return ""}
    }
}

proc get_draft {} {
    global draft
    case "$draft" in {
	"ANSI-82" {return a82}
	DIN {return d}
	default {warningmsg "Bad drafting std: $draft"; return ""}
    }
}

proc get_partform {} {
    global partform
    
    if {[exec uname -s] == "OSF1"} {
	return a
    }
    switch $partform {
	ASCII   { return a }
	Binary  { return b }
	default { warningmsg "Bad part format: $partform"; return "" }
    }
}

proc check_dirtree {} {
    global icem_fluent icem_powermesh
    
    if {![file exists mulcad] || ![file exists parts]} {
	if {[query_question .query \
	     "No project or parts directory in [pwd]-- continue?"] \
		 == "*cancel*"} {return 0}
    }
    if ![file exists mulcad] {
	infomsg "Creating project directory (mulcad)"
	exec /bin/mkdir mulcad
    }
    if ![file exists parts] {
	infomsg "Creating parts directory"
	exec /bin/mkdir parts
    }
    if {!$icem_fluent && !$icem_powermesh} {
	if ![file exists objects] {
	    infomsg "Creating objects directory"
	    exec /bin/mkdir objects
	}
    }
    return 1
}

# To get the possible configurations, look in the mulcad directory
# in the current dir, and then extract the names of the directories
# found there.

proc possible_configurations {} {
    set here [pwd]
    if {![file exists mulcad]} then \
	{error "Can't find mulcad directory"}
    cd mulcad
    set files [glob -nocomplain *]
    set configs {}
    foreach file $files {
	if {[file isdirectory $file]} then \
	    {append configs " " $file}
    }
    cd $here
    return [lsort $configs]
}

proc possible_geometries {} {
    global confname
    set here [pwd]
    if {![file exists mulcad]} then \
	{error "Can't find mulcad directory"}
    cd mulcad
    if {![file exists $confname]} then \
	{error "Can't find config directory"}
    cd $confname
    set files [glob -nocomplain *]
    set geos {}
    foreach file $files {
	if {[file isdirectory $file] && ($file != "topology") && \
		($file != "hexa")} {
	    append geos " " $file
	}
    }
    cd $here
    return [lsort $geos]
}

proc check_path_all {path} {
    foreach comp [split $path /] {
	if ![check_path_component $comp Path puts] { return 0 }
    }
    return 1
}

proc check_path_component {string name {puts errormsg}} {
    if {[string length $string] > 14} {
	$puts "$name names may not be longer than 14 characters"
	return 0
    }
    if [string match {* *} $string] {
	$puts "$name names may not contain the space character"
	return 0
    }
    if [string match {*	*} $string] {
	$puts "$name names may not contain the tab character"
	return 0
    }
    if {[string index $string 0] == "-"} {
	$puts "$name names may not begin with a dash (-)"
	return 0
    }
    foreach c {/ = \" $ & \\* \\? ( ) \{ \} \\\[ \\\] \\\\ ' ` ~ | \; < >} {
        if [string match "*$c*" $string] {
	    $puts "$name names may not contain the $c character"
	    return 0
        }
    }
    if {[regexp \[A-Z\] $string] == 1} {
	$puts "$name names may not contain upper case characters"
	return 0
    }
    return 1
}

proc get_ddn_defaults {} {
    global ddnterm env icem_fluent
    
    set pm ""
    if $icem_fluent {
	if [file exists .ddn_modals] {
	    set pm "pm=.ddn_modals"
	} elseif [file exists $env(ICEM_ACN)/ddn/modals/preset_modals] {
	    set pm "pm=$env(ICEM_ACN)/ddn/modals/preset_modals"
	}
    }
    
    switch $ddnterm {
	console -
	native {
	    return "um=[get_units] ds=[get_draft] pf=[get_partform] tt $pm"
	}
	xterm -
	x11 {
	    return "um=[get_units] ds=[get_draft] pf=[get_partform] tt=xterm $pm"
	}
	default {
	    return "um=[get_units] ds=[get_draft] pf=[get_partform] tt=$ddnterm $pm"
	}
    }
}

proc build_tetra_param_file {file input output} {
    set fd [open $file w]
    
    puts $fd "SurfaceFile               \"$input\""
    puts $fd "RootTetraFile             \"[file tail $input]/tetra_file\""
    puts $fd "TraceFile                 \"-\""
    puts $fd "DomainOutputFile          \"$output\""
    puts $fd "EatBadEdges               1"
    puts $fd "DebugMalloc               0"
    puts $fd "CheckBalance              0"
    puts $fd "CheckConformality         0"
    puts $fd "CheckConformalityAtEnd    0"
    puts $fd "LookAtInputData           0"
    puts $fd "LookAtSubdivisionOutput   0"
    puts $fd "LookAtConformalOutput     0"
    puts $fd "LookAtSplitOutput         0"
    puts $fd "LookAtThirdPassOutput     0"
    puts $fd "LookAtDisplacementOutput  0"
    puts $fd "LookAtSurfaceOutput       1"
    puts $fd "Delta                     0.200"
    puts $fd "DisplaceByAverage         0"
    puts $fd "DisplaceByProjection      1"
    puts $fd "WindowSize              0.5"
    
    close $fd
}

proc run_ddn_interpreted {name what script libdir extras} {
    global partfile place partname sheetname
    global ddn_path icem_fluent icem_powermesh
    
    if {($partname == "") && ![pick_part]} return
    
    if { !$icem_fluent && !$icem_powermesh } {
	if {$what == "d"} {
	    if {($partfile != "") && [part_is_mulcad] && \
		[query_question .query \
		 "The current part is in the Meshing Parts directory: really run $name?"] \
		    == "*cancel*"} return
	}
	if {$what == "m"} {
	    if {($partfile != "") && ![part_is_mulcad] && \
		[query_question .query \
		 "The current part is in the Geometry Parts directory: really run $name?"] \
		    == "*cancel*"} return
	}
    } else {
        if {$what == "d"} { 
            if {($partfile != "")} return 
        }
        if {$what == "m"} { 
            if {($partfile != "")} return
        }
    }

    
    set restart_name [tmpname]
    exec /bin/rm -f $restart_name
    set restart [open $restart_name w]
    puts $restart "F.5.13.5.3.$libdir"
    puts $restart "F.5.13.3.$script"
    if {$extras != ""} {puts $restart $extras}
    close $restart
    if {$partfile == ""} {
	set db parts
    } else {
	set db [file dirname $partfile]
    }
    set command "$ddn_path db=$db pn=\"$partname\" sn=$sheetname\
		i=$restart_name [get_ddn_defaults]"
    runxcom $name $command "" 1 0
    update_partlist d 0
}

proc run_ddn_linked {name what script extras} {
    global partfile place partname sheetname
    global ddn_path
    
    if {($partname == "") && ![pick_part]} { return 0 }
    
    if {$what == "d"} {
	set q "The current part is in the Meshing Parts directory: really run $name?"
	if {($partfile != "") && [part_is_mulcad] && \
		[query_question .query $q] == "*cancel*"} {
	    return 0
	}
    }
    if {$what == "m"} {
	set q "The current part is in the Geometry Parts directory: really run $name?"
	if {($partfile != "") && ![part_is_mulcad] && \
		[query_question .query $q] == "*cancel*"} {
	    return 0
	}
    }
    
    set restart_name [tmpname]
    exec /bin/rm -f $restart_name
    set restart [open $restart_name w]
    # menus on and off are no longer supported
    #    puts $restart "m"
    puts $restart "F.5.13.7.$script"
    if {$extras != ""} { puts $restart $extras }
    close $restart
    if {$partfile == ""} {
	set db parts
    } else {
	set db [file dirname $partfile]
    }
    set command "$ddn_path db=$db pn=\"$partname\" sn=$sheetname\
		i=$restart_name [get_ddn_defaults]"
    runxcom $name $command "" 1 0
    update_partlist d 0
    return 1
}

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

foreach ks {
    { { } space }
    { {!} 1 Shift }
    { {"} ' Shift }
    { {#} 3 Shift }
    { {$} 4 Shift }
    { {%} 5 Shift }
    { {&} 7 Shift }
    { {'} apostrophe }
    { {(} 9 Shift }
    { {)} 0 Shift }
    { {*} 8 Shift }
    { {+} equal Shift }
    { {,} comma }
    { {-} minus }
    { {.} period }
    { {/} slash }
    { {:} semicolon Shift}
    { {;} semicolon }
    { {<} comma Shift }
    { {=} equal }
    { {>} period Shift }
    { {?} slash Shift }
    { {@} 2 Shift }
    { {[} bracketleft }
    { {/} backslash }
    { {]} bracketright }
    { {^} 6 Shift }
    { {_} minus Shift }
    { {`} grave }
    { \{  bracketleft Shift }
    { {|} backslash Shift }
    { \}  bracketright Shift }
    { {~} grave Shift }
} {
    set ks_names([lindex $ks 0]) [lrange $ks 1 end]
}

proc send_keyboard_events {name wait string} {
    global ks_names env
    
    set fp [open keyboard.csh w]
    puts $fp "while ( 1 )"
    puts $fp "    set win = `xlswins | grep \"$name\" | sed 's/(.*\$//'`"
    puts $fp "    if ( \$win != \"\" ) break"
    puts $fp "    sleep 1"
    puts $fp "end"
    puts $fp "sleep $wait"
    set i 0
    foreach c [split $string ""] {
	if {!($i % 5)} {
	    puts $fp ""
	    puts $fp "if ( ! -e keyboard.csh ) exit 0"
	    puts $fp ""
	    puts $fp "$env(ICEM_ACN)/bin/xse -win \$win" nonewline
	}
	incr i
	set s ""
	if [info exists ks_names($c)] {
	    set s [lindex $ks_names($c) 1]
	    set c [lindex $ks_names($c) 0]
	}
	puts $fp " '$s<KeyPress>$c' '$s<KeyRelease>$c'" nonewline
    }
    puts $fp ""
    close $fp
    exec /bin/csh -f keyboard.csh &
    return keyboard.csh
}
