#!/bin/sh  
# \
exec oagtclsh "$0" "$@"
	
# $Log: not supported by cvs2svn $
# Revision 1.5  1999/07/02 16:29:40  soliday
# It now copies the output files from the oagData area to the desired location
#
# Revision 1.4  1999/06/09 21:38:34  soliday
# All file names are converted to full path names.
#
# Revision 1.3  1999/06/01 19:08:52  soliday
# Fixed pipeing so that it works with binary files
#
# Revision 1.2  1999/04/28 03:20:09  borland
# Fixed incorrect use of variable tempFile for temporary file when -pipe out
# is given.  Changed to tempFile2.
#
# Revision 1.1  1999/04/07 16:32:56  soliday
# This is the initial version of tclcaget.  This program is ment to be used
# in conjuction with tclcaGPServer so that snapshot files can be created.
#

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
set CVSRevisionAuthor "\$Revision: 1.6 $ \$Author: soliday $"

proc putResult {snapshot log output} {
    global pipe snapshotFile logFile
    if {($pipe == "out") || ($pipe == "through")} {
	fconfigure stdout -translation binary
	if [catch {open $snapshot r} fileID] {
	    puts stderr "Unable to read file: $fileID"
	    exit 1
	}
	fconfigure $fileID -translation binary
	puts stdout [read -nonewline $fileID]
	catch {close $fileID}
	file delete -- $snapshot
	file rename -force -- $log $logFile
    } else {
	file rename -force -- $snapshot $snapshotFile
	file rename -force -- $log $logFile
	if {[llength $output]} {
	    puts stdout $output
	}
    }
    exit
}

proc putError {output} {
    puts stderr $output
    exit 1
}

set parser [interp create -safe]

$parser alias putResult putResult
$parser alias putError putError
$parser eval {
    proc finished {snapshot log output} {
	putResult $snapshot $log $output
    }
    proc error_result {output} {
	putError $output
    }
}

proc client_handle {sid} {
    global parser buffer
    if {[gets $sid request] < 0} {
	catch {close $sid}
	puts stderr "Disconnected from server"
	exit 1
    } else {
	append buffer $request "\n"
	if {[info complete $buffer]} {
	    set request $buffer
	    set buffer ""
	    if {[catch {$parser eval $request} result]} {
		puts stderr $result
		exit 1
	    }
	}
    }
}


set args $argv
set requestFile ""
set snapshotFile ""
set logFile ""
set cawait 30
set host localhost
set pipe ""
set apsTempFileList ""

if [APSStrictParseArguments {requestFile snapshotFile logFile cawait host pipe}] {
    puts stderr "usage: tclcaget -requestFile <string> -snapshotFile <string> -logFile <string> \[-cawait <timeInSecs>\] \[-host <computer address>\] -pipe \[in | out | through\]\n[join [split $CVSRevisionAuthor $] ""]"
    exit 1
}
if {($pipe == "in") || ($pipe == "through")} {
#    fconfigure stdin -blocking 0
    fconfigure stdin -translation binary
    set tempFile /tmp/[APSTmpString]
    APSAddToTempFileList $tempFile
    if [catch {open $tempFile w} fileID] {
	puts stderr "Unable to create file: $fileID"
	exit 1
    }
    fconfigure $fileID -translation binary
    puts $fileID [read -nonewline stdin]
    catch {close $fileID}
    exec cp $tempFile junk2
    set requestFile $tempFile
}
if {($pipe == "out") || ($pipe == "through")} {
    set tempFile2 /tmp/[APSTmpString]
    APSAddToTempFileList $tempFile2
    set snapshotFile $tempFile2
}

if {([llength $requestFile] != 1) || ([llength $snapshotFile] != 1) || ([llength $logFile] != 1)} {
    puts stderr "usage: tclcaget -requestFile <string> -snapshotFile <string> -logFile <string> \[-cawait <timeInSecs>\] \[-host <computer address>\] -pipe \[in | out | through\]\n[join [split $CVSRevisionAuthor $] ""]"
    exit 1
}

if [catch {socket $host 4577} sid] {
    puts stderr $sid
    exit 1
} else {
    set requestFile [file join [pwd] $requestFile]
    set snapshotFile [file join [pwd] $snapshotFile]
    set logFile [file join [pwd] $logFile]
    fileevent $sid readable "client_handle $sid"
    fconfigure $sid -buffering line
    puts $sid "getvalues $requestFile $cawait"
    vwait enter-mainloop
}

