#!/bin/sh -f
# \
exec oagwish "$0" "$@"
wm withdraw .

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 auto_path [linsert $auto_path 0 .]

set verbose 0
#
#$Log: not supported by cvs2svn $
#Revision 1.13  2008/08/19 21:41:32  emery
#Added secondary choices for solaris and linux string matching.
#
#Revision 1.12  2006/04/06 22:22:43  emery
#If localhost is detected as part of the display name,
#an attempt will be made to determine the real name of the display.
#This works only for solaris.
#If running from 0.1 display, a message is written stating
#that VSA will not display to :0.1
#
#Revision 1.11  2006/04/05 23:07:33  emery
#Added default for HOST_ARCH switch of ping arguments.
#Added ping sdtout redirection to /dev/null.
#Added message and exit if display has localhost as part of its name.
#Replaced niscat and ypcat command with "getent hosts" command.
#Added command to set the hpvec LAN rate to 1 Hz.
#
#Revision 1.10  2006/03/06 21:27:24  emery
#Added switch for ping command arguments. (linux ping is different
#from solaris ping.)
#
#Revision 1.9  2004/04/14 19:54:27  emery
#Handle case where host is a numerical value.
#
# Revision 1.8  1996/05/14  09:59:12  emery
# Made the grep command for host IP identification more robust.
#
# Revision 1.7  1996/05/10  19:44:20  borland
# Added ping command to check for communications problem.
#
#
# handles these cases:
# -display option specified or not
# DISPLAY env. var. defined or not
# remote login or not.
# Note that the hp VSA ignores the display part of the X address.

if ![llength $argv] {
    puts "Usage: hpVecDisplay -unit {sr | boo | tmp} \[-verbose 1\] \[-Xephyr \{0|1\}\]"
    exit
}
set args $argv
set Xephyr 1
APSParseArguments {unit Xephyr verbose}

if {![info exists unit] || ![string length $unit]} {
    puts "**error ** option unit unspecified or uncorrectly specified"
    exit
}
if [regexp {^[0-9]+$} [string index $unit 0]] {
    #unit is digital IP address
    set hpvec $unit
    set pingInst $unit
} else {
    set hpvec hpvec$unit
    set pingInst $hpvec.aps4.anl.gov
}

switch $env(HOST_ARCH) {
    linux-x86_64 -
    linux-x86 -
    Linux {set pingArguments  "-c 1 -W 5"} 
    solaris-sparc -
    solaris -
    default {set pingArguments "5"}
}

if [catch {eval exec ping $pingInst $pingArguments > /dev/null} result] {
    puts stderr "Error: $result"
    exit
}

if $verbose {
    puts stderr "Found $hpvec.aps4.anl.gov."
}

# determine a display value if none is defined
if {[info exists env(DISPLAY)]} {
    set display $env(DISPLAY)
} else {
    set display :0.0
}

if [regexp {localhost} $display] {
    if [regexp solaris $env(HOST_ARCH)] {
        set display [exec xdisplay]
    } else {
        puts "Cannot determine the real display name of the machine that you are log in. Perhaps you are logged in with ssh."
        exit
    }
}

if [regexp {0.1} $display] {
    puts "Display selected ($display) is not the first display :0.0. The VSA will not display to :0.1"
    exit  
}

# determine parts of display variable
# this gets only the first part of the node name
set match ""
set host ""
set displayNumber ""
regexp {(.*):(.*)} $display match host displayNumber
#regexp {([^\.]*)} $host match host
if {![string length $host]} {
    set host [exec hostname]
    if {![string length $host]} {
        puts "** error ** problem with command hostname."
        exit
    }
}

if [regexp {[a-z]} $host] {
    set numericalHost 0
} else {
    set numericalHost 1
}

if {![string length $displayNumber]} {
    set displayNumber "0.0"
}
if {$verbose} {
    puts "HOST_ARCH: $env(HOST_ARCH)"
}

if !$numericalHost {
    set hostnumber [lindex [exec getent ahostsv4 $host] 0]
} else {
    set hostnumber $host
} 
set Xaddress $hostnumber:$displayNumber
# let's not have display values. RHEL uses :1
set Xaddress $hostnumber
    
# At this point variable hostnumber (with :m.n part) whould be known
if {$verbose} {
    puts "hpvec: $hpvec"
    puts "host: $host"
    puts "displayNumber: $displayNumber (may or may not be used)"
    puts "hostnumber: $hostnumber"
    puts "Xaddress: $Xaddress"
}
#allows local host to accept the hp display
if [catch {exec xhost +${hpvec} } result] {
    puts "$result"
    puts "Trying anyway"
}


if $Xephyr {
    if {$verbose} {
        puts "hpvec: Launching the Xephyr X window server for display $host:0. Yay! You may have to take steps to make your vnc cursor visible"
    }
    if [catch {exec Xephyr -br -ac -noreset -listen tcp -screen 1040x460 :0 &} results] {
        puts stderr $results
        exit
    }
    after 2000
}
exec hpSocketSend ${hpvec} "syst:gpib:echo on"
exec hpSocketSend ${hpvec} "syst:comm1:lan:xwin:rate 1"
exec hpSocketSend ${hpvec} "syst:comm1:lan:xwin:state off"
after 500
if {$verbose} {
    puts stderr "syst:comm1:lan:xwin:host '$hostnumber'"
    puts stderr "wait about 5 seconds ..."
}
exec hpSocketSend ${hpvec} "syst:comm1:lan:xwin:host '$hostnumber'"
after 1000
exec hpSocketSend ${hpvec} "syst:comm1:lan:xwin:state on"
exit

# Local Variables:
# mode: tcl
# End:

