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



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.6 $ \$Author: borland $"

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

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

proc ScanForUsers {args} {
    set pingOnly 0
    APSStrictParseArguments {pingOnly}

    global hostList fileName remoteMethod
    eval global $hostList

    if !$pingOnly {
        if ![string length $fileName] {
            return -code error "Supply a filename"
        }
        if ![file exists $fileName] {
            return -code error "Not found: $fileName"
        }
        set tmpFileList ""
        set tmpRoot /tmp/[APSTmpString]
        set widget [APSUniqueName .]
        set jobsFound 0
    }
    foreach host $hostList {
        if ![subst \$$host] continue
        if [catch {exec ping $host 2} result] {
            APSSetVarAndUpdate status "Problem pinging $host: $result"
            continue
        }
        if $pingOnly {
            APSSetVarAndUpdate status "$host is ok."
            continue
        }
        set tmpFile $tmpRoot.$host
        APSSetVarAndUpdate status "Scanning $host"
        if [string compare $result "$host is alive"]!=0 {
            APSSetVarAndUpdate status "$host is not responding"
            continue
        }
        set ItIsDone 0
        catch {exec $remoteMethod -n $host /usr/sbin/fuser $fileName 2> /dev/null} result
        set result [string trim $result]
        if [string length $result] {
            set pidList0 [split $result]
            set pidList ""
            foreach pid $pidList0 {
                lappend pidList [string trim $pid]
                incr jobsFound
            }
            catch {eval exec remoteMethod -n $host /usr/bin/ps -f -p [join $pidList ,]} result
            APSSetVarAndUpdate status "$result"
        }
    }
    if !$pingOnly {
        APSSetVarAndUpdate status "Done scanning: $jobsFound jobs found."
    }
}

proc DeleteFile {} {
    global fileName
    if [APSQueryToProceed -message "Warning: deletion of $fileName cannot be undone."] {
        if [catch {file delete -force $fileName} result] {
            return -code error "$result"
        }
    }
}
APSApplication . -name $title -version $CVSRevisionAuthor \
  -overview "Allows searching for and selective killing of processes running on MCR workstations."

set status Ready.
APSScrolledStatus .status -parent .userFrame -textVariable status \
        -width 80 -height 20

set remoteMethod ssh

APSRadioButtonFrame .method -parent .userFrame -label "Remote access method: " \
    -valueList "rsh ssh" -buttonList "rsh ssh" -orientation horizontal \
    -variable remoteMethod

APSLabeledEntry .progname -parent .userFrame -label "Program full path name: " \
    -textVariable fileName -width 60

APSCheckButtonFrame .hosts -parent .userFrame -label "Host choices" \
  -variableList $hostList -buttonList $hostList -orientation vertical \
  -limitPerRow 5 \
  -allNone 1 -contextHelp "Choose which hosts (i.e., workstations) you want to scan."
APSButton .scan -parent .userFrame -text Scan -command ScanForUsers
APSButton .del -parent .userFrame -text "Delete File" -command DeleteFile
APSButton .ping -parent .userFrame -text "Ping hosts" -command "ScanForUsers -pingOnly 1"
update

