
# Stuff for replay.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc file_replay {} {
    global config_dir replay_dir
    
    set new_replay $config_dir/hexa
    set old_replay [file dirname $config_dir]/hexa
    if { ! [ file isdirectory $old_replay ] } {
# no old one
	if { ! [ file isdirectory $new_replay ] } {
# no new one: use new new one
	} else {
# new exist, check that
	    set replay_dir $new_replay
	}
    } elseif { ! [file isdirectory $new_replay ] } {
# old one and no new one
	set replay_dir $old_replay
    } else {
# old one and new one
	set old_files [ glob -nocomplain $old_replay/* ]
	set new_files [ glob -nocomplain $new_replay/* ]
	if { $old_files == "" } {
		set replay_dir $new_replay
	} elseif { $new_files == "" } {
		set replay_dir $old_replay
	} else {
# files in both
		set mess "There are replay files in both the old location \
		($old_replay)  and the new location ($new_replay). \
		Use the new location ?"
		set stat  [tk_dialog .which_replay \
			"Use new" $mess "" -1 Yes No] 
		if { $stat == 0 } {
			set replay_dir $new_replay
		} else {
			set replay_dir $old_replay
		}
	}
    }

    catch {cmd_mkdir $replay_dir }
    replay_form {
	{ button rijk "Update IJK" "get_indices 1; index_reset" {10 0} }
	{ checkbutton iger "Ignore errors" replay_ignore_errors {10 1 -cols 2} }
    }
}

proc file_replay_batch {file} {
    replay_batch $file
}

proc replay_filter {com} {
    set word [lindex $com 0]
    
    if { [ lsearch "set_background graphics_loc depthcue scan_plane \
	list_families ratio_histogram orient ijk_max df del \
	dem worst_hex index_from_edge \
	undo_query place_node rep split_edge define_point rh rr check_mesh \
	dynamic_edge_mesh mesh_dimension mesh_params \
	topo_vertex_number topo_edge_number topo_face_number  \
	max_superblock_number  \
	n_nodes_edge place_nodes display_text remove_edge_point \
	region_zoom print_topo_tree \
	get_surface_params sp edge_node_dim" $word ] >= 0 } {
		return 0
    }

# some query commands
    if { [ llength $com ] == 1 } {
	if { [ lsearch "default_bunching_law \
		projection_limit multi_grid_level floating_grid \
		transfinite_degree reference_blocking \
		unstruct_face_type set_ublock_mesh_type" $word ] >= 0 } {
		return 0
	}
    }
# query command
    if { [ llength $com ] == 2 && 
		$word == "default_bunching_law" && 
		[ lindex $com 1 ] == "new_format" } {
		return 0;
    }
# get node location can set variables
#	get_node_location 
    
    foreach i {
	select* display* unhighlight* erase* list*
    } { if [string match $i $word] { return 0 } }
    
    foreach i {
	"undo query"
	"redo query"
    } { if {$com == $i} { return 0 } }
    
    return 1
}

proc replay_execute_command {line update_display} {
    global replay_always_update split_ind last_line replay_ignore_errors
    
    set ll ""
    if {[string index $line 0] == "!"} {
	uplevel #0 [string range $line 1 end]
    } elseif [string match \#!* $line] {
	uplevel #0 [string range $line 2 end]
    } else {
	command $line
	set ll $last_line
    }

    switch [lindex $line 0] {
	initialize_mesh {
	    get_indices 1
	    index_reset
	    update_display
	}
	unknown_split_grid {
	    global index_min index_max
	    
	    set dir [lindex $line 2]
	    set ind []
	    if {$dir < 3} {
		if {$split_ind <= $index_max($dir)} {
		    incr index_max($dir)
		}
		if {$split_ind < $index_min($dir)} {
		    incr index_min($dir)
		}
	    }
	}
	define_family {
		set new_fam [ lindex $line 1 ]
# now do it through tcl channels to make sure it is in the menus.
		define_family $new_fam
	}
    }
    if { [ info exist replay_always_update ] && $replay_always_update } {
	replay_after_commands_done
    } elseif $update_display {
	update_display
    }
# check for error condition
    if { $replay_ignore_errors } {
	return 0
    } else {
    	return [ string match "error executing command:*" $ll ]
    }
}

proc replay_after_commands_done {} {
    get_indices 1
    index_reset
    update_display
}

