# Library functions for the scripting library (scrib)
#
# File: scrib/lib/glue.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------
#
# this file is for procs which require proc_scrib_data, and so depend on
# the ScribApp context, but should not be in script/*.tcl files...
#
#

## get a standardized naspid from the given filename

proc scrib_lib_glue_assembly_filename_naspid_get {filename} {
    global proc_scrib_data
    
    if [info exists proc_scrib_data(naspid)] {
	return $proc_scrib_data(naspid)
    }
    
    set tnaspid 1
    if {[info exists proc_scrib_data(naspid_keystrings)] && \
	    [info exists proc_scrib_data(naspid_nchar)] && \
	    [info exists proc_scrib_data(naspid_offset)]} {
	
	set tnaspid [scrib_lib_geo_filename_naspid_get $proc_scrib_data(name) \
		$proc_scrib_data(naspid_keystrings) \
		$proc_scrib_data(naspid_nchar) \
		$proc_scrib_data(naspid_offset)]
    } else {
	mess "not enough information to get a nastran pid from the file name\n" red
    }
    
    #mess "DEBUG: tnaspid $tnaspid\n"
    set proc_scrib_data(naspid) [scrib_lib_assembly_naspid_standardize $tnaspid]
    
    return $proc_scrib_data(naspid)
}

## get/fix the path of the orking directory

proc scrib_lib_glue_get_working_directory {} {
    global proc_scrib_data
    
    if ![info exists proc_scrib_data(wdir)] {
	set wdir [pwd]
	mess "not certain of the current working directory; using pwd $wdir\n" red
	set proc_scrib_data(wdir) $wdir
    }
    
    if {"" == $proc_scrib_data(wdir)} {
	set proc_scrib_data(wdir) "."
    }
    
    if [file isdirectory $proc_scrib_data(wdir)] {
	return $proc_scrib_data(wdir)
    }
    
    # wdir is not a directory from current place, so
    # try doing it relative to the caddir. Bugzilla # 5528
    set wdir $proc_scrib_data(wdir)
    if {[info exists proc_scrib_data(cad_dir)] && \
	    ("" != $proc_scrib_data(cad_dir)) && \
	    [file isdirectory $proc_scrib_data(cad_dir)] && \
	    [string equal "relative" [file pathtype $wdir]]} {
	
	set pot_wdir [file join $proc_scrib_data(cad_dir) $wdir]
	if {[file isdirectory $pot_wdir]} {
	    mess "resetting working directory relative to cad directory $pot_wdir\n" red
	    set wdir $pot_wdir
	}
    }
    
    set proc_scrib_data(wdir) $wdir
    
    return $proc_scrib_data(wdir)
}

##

proc scrib_lib_glue_get_execution_args_from_gui {} {
    global proc_scrib_data
    
    set args     ""    
    #
    # --> get remote host or batch-queueing
    #
    if [info exists proc_scrib_data(execution_mode)] {
	set execution_mode $proc_scrib_data(execution_mode)
	switch -- $execution_mode {
	    "LOCAL" {
		#
		# we only support interactive mode when running locally -- FIXME??
		#
		if {[info exists proc_scrib_data(interactive)] && \
			("" != $proc_scrib_data(interactive))} {
		    append args " -interactive $proc_scrib_data(interactive)"
		}
	    }
	    "REMOTE_OLD" {		
		if {[info exists proc_scrib_data(remote_host)] && \
			("" != $proc_scrib_data(remote_host))} {	    
		    append args " -remote_host $proc_scrib_data(remote_host)"
		    if {[info exists proc_scrib_data(remote_icemacn)] && \
			    ("" != $proc_scrib_data(remote_icemacn))} {
			append args " -remote_icemacn $proc_scrib_data(remote_icemacn)"
		    }
		    if {[info exists proc_scrib_data(remote_port)] && \
			    ("" != $proc_scrib_data(remote_port))} {
			append args " -remote_port $proc_scrib_data(remote_port)"
		    }
		} else {
		    mess "Error: cannot execute remotely without a remote_host var...please check input fields...\n" red
		}
	    }
	    "BATCH_QUEUE" {
		if {[info exists proc_scrib_data(batch_system)] && \
			("" != $proc_scrib_data(batch_system))} {
		    append args " -batch_queueing_system $proc_scrib_data(batch_system)"
		} else {
		    mess "Error: cannot execute batch-queueing without a batch_system var...please check input fields\n..." red
		}
	    }
	}
    }
    
    return $args
}
