#!/bin/sh  
# \
exec oagwish "$0" "$@"

#
# $Log: not supported by cvs2svn $
# Revision 1.2  1998/02/20 20:57:35  borland
# Improved version with separate areas for status and output.
#
# Revision 1.1  1998/02/20 20:39:14  borland
# First version.
#
#

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)]
APSDebugPath

set CVSRevisionAuthor "\$Revision: 1.3 $ \$Author: borland $"

set usage "usage: RunRemoteCommand -title <string> \[-hostList <string-list>\]"
set args $argv
set title RunRemoteCommand
set hostList [lsort {charis echo herema talos phoenix \
                       iris demeter ariel chaos artemus cadmus cyclops}]
if {[APSStrictParseArguments {title hostList}] || \
        ![string length $title]} {
    APSAlertBox [APSUniqueName .] -errorMessage "$usage"
    exit 1
}

set hostList [lsort $hostList]
foreach elem $hostList {
    set $elem 1
}       

proc APSRunRemoteCommand {args} {
    set command ""
    set host ""
    set argCopy [join $args ,]
    if {[APSStrictParseArguments {command host}] || \
          ![string length $command] || ![string length $host]} {
        return -code error "APSRunRemoteCommand: Invalid arguments: [split $argCopy ,]"
    }
    if [catch {eval exec rsh -n $host $command} result] {
        return -code error "APSRunRemoteCommand: host $host: $result"
    }
    return "$result"
}

proc RunProcesses {} {
    global command pause hostList
    foreach host $hostList {
        global $host
        if ![set $host] continue
        APSSetVarAndUpdate status "Working on $host..."
        if [catch {APSRunRemoteCommand -host $host \
                     -command $command} result] {
            APSSetVarAndUpdate status "$result"
        } else {
            APSSetVarAndUpdate output "$host:"
            APSSetVarAndUpdate output "$result"
            APSSetVarAndUpdate status "No errors for $host."
        }
        after [expr int($pause*1000)]
    }
}

APSApplication . -name $title -version $CVSRevisionAuthor \
  -overview "Allows running a command on many workstations in sequence."

set status Ready.
set output ""
APSScrolledStatus .status -parent .userFrame -textVariable status \
        -width 130 -height 6 -label Status
APSScrolledStatus .output -parent .userFrame -textVariable output \
        -width 130 -height 12 -label Output
APSFrameGrid .fg -parent .userFrame -yList {top bot}
set top .userFrame.fg.top
set bot .userFrame.fg.bot

APSCheckButtonFrame .hosts -parent $top -label "Host choices" \
  -variableList $hostList -buttonList $hostList -orientation vertical -limitPerRow 10 \
  -allNone 1 -contextHelp "Choose which hosts (i.e., workstations) you want to scan."

set command ""
set pause 1
APSLabeledEntry .program -parent $bot -label "Command: " \
    -textVariable command -width 60
APSLabeledEntry .pause -parent $bot -label "Pause (s): " \
  -textVariable pause 
APSButton .run -parent $bot -text "Run" -command RunProcesses 

update

