
proc APSLaunchPoissonJobHelp {} {
    return "Usage: APSLaunchPoissonJob \n-inputfile <string>\n\[-sf7inputfile <string>\]\n\[-outputDir <string>\]\n\[-workDirLinux <string>\]\n\[-workDirWindows <string>\]\n\[-windowsIP <IP address>\]\n\[-automesh <string>\]\n\[-poisson <string>\]\n\[-sf7 <string>\]\n\nThis procedure allows you to execute a Poisson job on a remote Windows computer using the tclDPServer/tclDPClient communication tool."
}

proc APSLaunchPoissonJob {args} {
    set inputfile ""
    set outputDir ""
    set sf7inputfile ""
    set workDirLinux /home/oxygen/FLOOD/poisson
    set workDirWindows y:/poisson
    set windowsIP 164.54.8.193
    set automesh c:/Tcl/automesh.exe
    set poisson c:/Tcl/poisson.exe
    set sf7 c:/Tcl/sf7.exe
    
    #Read in options
    APSStrictParseArguments {inputfile outputDir workDirLinux workDirWindows windowsIP automesh poisson sf7 sf7inputfile}
    
    #Ensure the input file exists.
    if {![file exists $inputfile]} {
        return -code error "LaunchPoissonJob: input file $inputfile does not exist"
    }
    
    #If sf7inputfile given then ensure that it exists
    if {[llength $sf7inputfile]} {
        if {![file exists $sf7inputfile]} {
            return -code error "LaunchPoissonJob: sf7 input file $sf7inputfile does not exist"
        }
    }
    
    #If no output directory was specified use the same directory as the input file.
    if {$outputDir == ""} {
        set outputDir [file dirname $inputfile]
    }
    
    #Create temp directory in /home/oxygen/FLOOD/poisson so that there are no conflicts if multiple instances are run at the same time.
    #Look for a directory name that does not exist and then create it break out of the loop.
    for {set i 1} {$i < 1000} {incr i} {
        if {![file isdirectory [file join $workDirLinux $i]]} {
            if {[catch {file mkdir [file join $workDirLinux $i]} result]} {
                return -code error "LaunchPoissonJob: $result"
            }
            #Set automesh and poisson input file names
            set workDirLinux [file join $workDirLinux $i]
            set workDirWindows [file join $workDirWindows $i]
            set subDir $i
            set automeshInput [file join $workDirLinux [file tail $inputfile]]
            set poissonInput [file rootname $automeshInput].T35
            set sf7Input [file join $workDirLinux [file tail $sf7inputfile]]
            break
        }
    }
        
    #Run chmod so that if anything goes wrong the directory can be deleted at a later date.
    if {[catch {exec chmod a+rwx $workDirLinux} result]} {
        catch {file delete -force $workDirLinux}
        return -code error "LaunchPoissonJob: $result"
    }

    #Copy the input file to /home/oxygen/FLOOD/poisson so the Windows computer can see it.
    if {[catch {file copy -force $inputfile $automeshInput} result]} {
        catch {file delete -force $workDirLinux}
        return -code error "LaunchPoissonJob: $result"
    }
    
    #Run chmod so that if anything goes wrong the file can be deleted at a later date.
    if {[catch {exec chmod a+rw $automeshInput} result]} {
        catch {file delete -force $workDirLinux}
        return -code error "LaunchPoissonJob: $result"
    }

    #if the sf7input file is given then copy the file to /home/oxygen/FLOOD/poisson so the Windows computer can see it.
    if {[llength $sf7inputfile]} {
        if {[catch {file copy -force $sf7inputfile $sf7Input} result]} {
            catch {file delete -force $workDirLinux}
            return -code error "LaunchPoissonJob: $result"
        }
        #Run chmod so that if anything goes wrong the file can be deleted at a later date.
        if {[catch {exec chmod a+rw $sf7Input} result]} {
            catch {file delete -force $workDirLinux}
            return -code error "LaunchPoissonJob: $result"
        }
    }

    
    #Randy's windows computer is at $windowsIP
    #Run automesh first
    #Add a rediculous amount of backslashes because a backslash is a special Tcl character. Two backslashes in a row get evaluated 
    #to mean only a single backslash. Because it will be evaluated 3 times, once here, once in tclDPClient, and once in tclDPServer
    #I need 8 backslashes. 8 becomes 4 which becomes 2 which becomes 1.
    set automesh [join [split $automesh /] {\\\\\\\\}]
    set poisson [join [split $poisson /] {\\\\\\\\}]
    set sf7 [join [split $sf7 /] {\\\\\\\\}]
    set workDirWindows [join [split $workDirWindows /] {\\\\\\\\}]
   
    if {[catch {exec tclDPClient -host $windowsIP -command "$automesh ${workDirWindows}\\\\\\\\[file tail $automeshInput]"} result]} {
        catch {file delete -force $workDirLinux}
        return -code error "LaunchPoissonJob: $result"
    }

    #Run poisson on output file from automesh
    if {[catch {exec tclDPClient -host $windowsIP -command "$poisson ${workDirWindows}\\\\\\\\[file tail $poissonInput]"} result]} {
        catch {file delete -force $workDirLinux}
        return -code error "LaunchPoissonJob: $result"
    }
    
    #Delete automesh inputfile
    catch {file delete -force $automeshInput}

    #Run sf7 if sf7input file is given
    if {[llength $sf7inputfile]} {
        if {[catch {exec tclDPClient -host $windowsIP -command "$sf7 ${workDirWindows}\\\\\\\\[file tail $sf7Input] ${workDirWindows}\\\\\\\\[file tail $poissonInput]"} result]} {
            catch {file delete -force $workDirLinux}
            return -code error "LaunchPoissonJob: $result"
        }
        #Delete s7f inputfile
        catch {file delete -force $sf7Input}
    }

    #Copy all output files to the specified output directory
    set inputRootname [file rootname [file tail $inputfile]]
    set files [glob -nocomplain ${workDirLinux}/*]
    foreach file $files {
        set tail [file tail $file]
        if {[catch {file copy -force $file [file join $outputDir ${inputRootname}_$tail]} result]} {
            catch {file delete -force $workDirLinux}
            return -code error "LaunchPoissonJob: $result"
        }
    }

    #Delete the temp directory
    catch {file delete -force $workDirLinux}
    
    return
}

proc APSQueryPoissonJobHelp {} {
    return "Usage: APSQueryPoissonJob \n\[-windowsIP <IP address>\]\n\nThis procedure is used to check if there is a Poisson job running."
}

proc APSQueryPoissonJob {args} {
    set windowsIP 164.54.8.193
    
    #Read in options
    APSStrictParseArguments {windowsIP}

    if {[catch {exec tclDPClient -host $windowsIP -query 1} result]} {
        return -code error "APSQueryPoissonJob: $result"
    }
    return -code ok $result

}

proc APSKillPoissonJobHelp {} {
    return "Usage: APSKillPoissonJob \n\[-windowsIP <IP address>\]\n\nThis procedure is used to kill an running Poisson job."
}

proc APSKillPoissonJob {args} {
    set windowsIP 164.54.8.193
    
    #Read in options
    APSStrictParseArguments {windowsIP}

    if {[catch {exec tclDPClient -host $windowsIP -kill 1} result]} {
        return -code error "APSKillPoissonJob: $result"
    }
    return
}
