# Library functions for the scripting library (scrib)
#
# File: scrib/lib/remote.tcl
#
# $Revision: 390974 $ on $Date: 2004-02-17 16:23:05 -0500 (Tue, 17 Feb 2004) $
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------
#
# --> if the name of the first proc already exists in interp, return
#
if {"" != [info commands scrib_lib_remote_get_client]} { return }


## Start a job on a server <em>remote_host</em> on port <em>server_port</em>.
## Returns the socket.

proc scrib_lib_remote_get_client {remote_host server_port} {    
    #
    # --> 
    #    
    if [catch {socket $remote_host $server_port} sock] {
	mess "Error connecting to remote host via port \"$server_port\"\n" red
	return ""
    }
    fconfigure $sock -buffering line
    
    set err [fconfigure $sock -error]
    if {"" != $err} {
	mess "Warning there may be an error in the current socket: $err\n" red
	return ""
    }
    
    set sockname [fconfigure $sock -sockname]
    set peername [fconfigure $sock -peername]
    #
    mess "On host $remote_host found port $server_port to use (socket: $sock)\n"
    mess "\nCommunicating on socket \"$sock\":\n"
    mess "\tsockname: $sockname\n"
    mess "\tpeername: $peername\n\n"
    
    return $sock
}

## Start a job on a remote_host ... fixme

proc scrib_lib_remote_client_exec {job_token remote_host server_port cshellscript logfile args} {
    
    mess "Executing script \"$cshellscript\" on \"$remote_host\"\n"
    
    set sock [scrib_lib_remote_get_client $remote_host $server_port]    
    if {"" == $sock} {
	mess "Error: could not open socket for job \"$job_token\"...\n" red
	return [scrib_lib_exec_failure]
    }
    
    set cmd "which tclsh" ;# fixme
    
    puts $sock $cmd
    mess "\n[gets $sock]\n"
    flush $sock
    # NOW WAIT--FIXME....
    
    set status [scrib_lib_exec_finished]
    scrib_lib_exec_update_job_status $job_token $status
    #scrib_lib_utils_file_to_mess $logfile
    
    close $sock
        
    return $status
}

## 

proc scrib_lib_remote_kill_job {job_token} {
    
    error "Cannot kill remote job yet! FIXME"
    
    # Make sure it is a remote job - still running and we can see 
    # the log file
    
    if {[scrib_lib_exec_remote] != [scrib_lib_exec_get_job_datum $job_token MODE]} {
	mess "Error: the given job \"$job_token\" is not remote!\n" red
	return [scrib_lib_exec_failure]
    }
    #
    # --> Kill a remote job...
    #
    set pid [scrib_lib_exec_get_job_datum $job_token PID]
    if {"" == $pid} {
	mess "Error: cannot find pid to kill remote job \"$job_token\"" red
	return [scrib_lib_exec_failure]
    }
    set host [scrib_lib_exec_get_job_datum $job_token HOST]
    
    #mess "scrib_lib_remote_kill_job $job_token HOST=$host; PID=$pid\n" green
    #
    # --> Write a script ...
    #
    set script [file join [asdf] killjob.csh]
    cmd_rm $script    
    # fixme write script with command    
    
    if [catch {exec rsh $machine -n source $script &} res] {
	mess "Sorry can not kill job. Failed to rsh the kill command script: $res\n" red
	set status [scrib_lib_exec_failure]
    } else {
	set status [scrib_lib_exec_success]
    }
    
    cmd_rm $script
    
    return $status
}

## Start a job on a remote machine via rsh; update for ssh using /etc/ssh/shosts.equiv; FIXME

proc scrib_lib_remote_rsh_exec {job_token remote_host cshellscript logfile args} {
    
    mess "Executing script \"$cshellscript\" on \"$remote_host\"\n"
    
    set status [catch {exec rsh $remote_host -n source $cshellscript >& $logfile} res]
    #exec rsh $remote_host -n source $cshellscript >& $logfile
    
    # NOW WAIT--FIXME....for log tail
    
    scrib_lib_exec_update_job_status $job_token [scrib_lib_exec_finished] ;#fixme
    scrib_lib_utils_file_to_mess $logfile
    
    return $status
}














proc unused_scrib_lib_remote_start_job {job_token remote_host script_file logfile server_port} {
    
    set sock [scrib_lib_remote_get_client $remote_host $server_port]    
    
    set cmd "which tclsh" ;# fixme
    
    puts $sock $cmd
    puts "\n[gets $sock]"
}
