# This is the UNIX version of these functions.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc check_exec {args} {
    global exec_problem no_exec_errors batch 
    
    set string "exec $args"

    if $batch { append string " >@stdout" }
    regsub -all {\#} $string {\#} string
    
#   if {[catch $string retstr] && ![info exists no_exec_errors]} {
#	warningmsg "$retstr"
#	set exec_problem 1
#	return ""
#   }

    set string_new $string
    regsub "exec " $string_new "" string_new
#   puts "now do it : $string_new"
#   puts "-----------------"
    set retstr [eval exec $string_new]
#   puts "result : $retstr"

    set exec_problem 0
    catch {unset no_exec_errors}
    return $retstr
}

proc get_preserved_vars {} {
    global env
    
    set str ""
# Fix for HP - 6.6.99 - not including SHLIB_PATH
    foreach v {TK_LIBRARY TCL_LIBRARY DISPLAY ICEM_ACN LM_LICENSE_CHECK
		LM_LICENSE_FILE LIBPATH} {
	    if [info exists env($v)] {
		set val $env($v)
		set str "$str setenv $v $val;"
	    } else {
		set str "$str unsetenv $v;"
	    }
	}
    foreach v {LD_LIBRARY_PATH} {
	    if [info exists env($v)] {
		set val $env($v)
		set str "$str setenv $v $val;"
	    }
	}
    return $str
}

proc runxcom {name com catfile hitreturn back} {
    global runcoms xterm_params xterm_prog env rsh_command
    global override_hitreturn batch
    
    if [info exists override_hitreturn] { set hitreturn $override_hitreturn }
    
    infomsg "Running: $com"
    set prog [lfirst $com]
    if 0 {
	if {![file exists $prog]} {
	    errormsg "File $prog doesn't exist!"
	    return
	}
    }
    
    if [info exists rsh_command] {
	set cur [pwd]
	# set home [glob ~]
	cd
	set home [pwd]
	cd $cur

	if {![regexp "$home/(.*)" $cur dummy place]} {set place $cur}
	
	set setup "cd $place; [get_preserved_vars] "
	set com "$rsh_command \"$setup $com\""
    } else {
	set setup "[get_preserved_vars] "
	set com "$setup $com"
    }
    
    if {$catfile != ""} then {
	set scom "csh -c '$com; \
echo Listing of file $catfile ---------------------------------- ; \
cat $catfile; \
echo End of file $catfile -------------------------------------- ; \
echo; echo Hit return when done...; \
set i=\$<'"
    } else {
	if $hitreturn then {
	    set scom "csh -c '$com ; \
echo; echo Hit return when done...; \
set i=\$<'"
	} else {
	    set scom "csh -c '$com'"
	}
    }
    
    #puts stdout "running $scom"
    
    if $batch {
	puts "Run: $scom"
	set xcom $scom
	
    } elseif [info exists xterm_prog] {
	set xcom "$xterm_prog $xterm_params \
-name $name -title $name -e $scom"
    } else {
	infomsg "Hey, no xterm...  Trying to do without..."
	set xcom $scom
    }
    
    #puts stdout "xcom is $xcom"
    if {$runcoms} {
	if $back {
	    check_exec sh -c $xcom &
	} else {
	    check_exec sh -c $xcom
	    infomsg "Done."
	}
    } else {
	infomsg "NOT!!"
    }
}

proc runxcom_remote {name com catfile hitreturn back} {
    global remotehost env rsh_command standalone
    
    # if $standalone
    if 0 {
	# This is not cool for interactive padamm.
	runxcom $name $com $catfile $hitreturn $back
	return
    }

    if {($remotehost == "") || ($remotehost == "localhost") ||
	($remotehost == "LOCALHOST")} {
	    runxcom $name $com $catfile $hitreturn $back
	    return
	}
    
    infomsg "Running program on machine $remotehost (this can be changed using the \
modals menu -- blank means to run the program on the local host)"
    
    foreach rp {/usr/bin/rsh /usr/ucb/rsh /usr/bsd/rsh /bin/rsh} {
	if [file exists $rp] {set rsh $rp; break}
    }
    
    if [info exists rsh] {
	set rsh_command "$rsh $remotehost"
    } else {
	warningmsg "Can't find rsh, running locally"
    }
    warningmsg "Prompts and messages from the program may not be visible until the \
program terminates"
    
    # This is wierd.
    set old_display $env(DISPLAY)
    if {[string index $env(DISPLAY) 0] == ":"} {
	set env(DISPLAY) [exec uname -n]$env(DISPLAY)
    }
    
    runxcom $name $com $catfile $hitreturn $back
    
    set env(DISPLAY) $old_display

    catch {unset rsh_command}
}

proc runcom {com {out ""} {noerrors 0}} {
    global runcoms no_exec_errors exec_problem
    
    infomsg "Running: $com"
    set prog [lfirst $com]
    if {![file exists $prog]} {
	errormsg "File $prog doesn't exist!"
	return
    }
    if {!$runcoms} {infomsg "NOT!!"; return}
    
    if $noerrors { set no_exec_errors 1 }
    
    if {$out == ""} {
	set out .tmp.outfile
	check_exec sh -c "$com >$out"
    } else {
	check_exec sh -c $com
    }
    
    if {[file exists $out] && [file size $out]} {
	infomsg "Output of command:"
	set file [open $out r]
	set stuff [read $file nonewline]
	close $file
	infomsg $stuff
	infomsg "Done."
    } else {
	infomsg "Done."
    }
    if $exec_problem { errormsg "The command did not finish correctly." }
    
#    exec /bin/rm -f $out
}

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
    }
}
