#!/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)]

set arg0 [lindex $argv 0]
set arg1 [lindex $argv 1]

set gui 0

if {[llength $arg0] == 0} {
    puts stderr "Usage: findInCVS \[<filename>\] \[-gui\] "
    exit
}
if {[string compare $arg0 "-gui"] == 0} {
    set gui 1
    set filename ""
} else {
    set gui 0
    set filename $arg0
}
if {[llength $arg1] != 0} {
    if {$gui} {
        set filename $arg1
    } else {
        if {[string compare $arg1 "-gui"] == 0} {
            set gui 1
        }
    }
}

if {$gui == 0} {
    wm withdraw .
} else {
    APSApplication . -name "Find In CVS"
    set status Ready...
    APSScrolledStatus .status -parent .userFrame -textVariable status \
      -packOption "-fill both -expand true" -width 80 -height 8
    APSLabeledEntry .entry -parent .userFrame -label "Filename:" -textVariable filename -packOption "-expand true -fill x"
    APSButton .button -parent .userFrame -text "Search" -command {FindInCVS $filename}
}

proc FindInCVS {name} {
    global gui
    if {$gui} {
        APSSetVarAndUpdate status "Searching..."
    }
    if {[catch {exec find /net/phoebus/epicsmgr/cvsroot/epics/extensions/src -name ${name},v} results]} {
        if {$gui == 0} {
            puts "error: $results"
            exit 1
        } else {
            APSSetVarAndUpdate status "error: $results"
        }
    }
    foreach name $results {
        set name [string range $name 30 end-2]
        if {$gui == 0} {
            puts $name
        } else {
            APSSetVarAndUpdate status $name
        }
    }
    
    if {[catch {exec find /net/phoebus/oagmgr/cvsroot/oag/apps/src -name ${name},v} results]} {
        if {$gui == 0} {
            puts "error: $results"
            exit 1
        } else {
            APSSetVarAndUpdate status "error: $results"
        }
    }
    foreach name $results {
        set name [string range $name 28 end-2]
        if {$gui == 0} {
            puts $name
        } else {
            APSSetVarAndUpdate status $name
        }
    }
    if {$gui} {
        APSSetVarAndUpdate status "Done."
    }
}

if {[llength $filename]} {
    FindInCVS $filename
}
if {$gui == 0} {
    exit
}
