#  $Id: wincenter.exp,v 1.7 2001/04/04 09:38:58 julian Exp $
#
# wincenter.exp: expect script to start WinCenter over an rsh or 
# telnet connection
#
set timeout 60
global argv env

set uname [lindex $argv 0]
set pword [lindex $argv 1]
if { $uname == "" || $pword == "" } {
    authrequest -normal
    set uname $username
    set pword $password
}

if {$env(TTA_Transport) == "telnet"} {
    tarantella -portnumber 5999
}

################################################################
#Set our Environment Variables
################################################################
set vars [split $env(TTA_ENVIRONMENT) ";" ]
foreach i $vars {
    set idx [string first "=" $i]
    set name [string range $i 0 [expr $idx - 1] ]
    set value [string range $i [expr $idx + 1] [string length $i] ]
    if {$name == "ALTDISPLAY"} {
	set ALTDISPLAY $value
    } elseif {$name == "DISPLAY"} {
	set DISPLAY $value
    }
}
################################################################
#Check everything we need to run is there
################################################################
if { ![info exists env(TTA_AppletWidth)] || ![info exists env(TTA_AppletHeight)] } {
    send_error "No Resolution Parameters Specified\n"
    exit $exitCode(ErrCommandExecutionFailed)
}
if { ![info exists env(TTA_CONNECTIONSERVICE)] } {
    send_error "No Transport Parameter Specified\n"
    exit $exitCode(ErrCommandExecutionFailed)
}

# Always have the attribute domain as it is a default
# Check whether it has anything in if so include it
if { [ string length $env(TTA_Domain)] > 0 } {
    set DOMAIN "-domain $env(TTA_Domain)"
} else {
    set DOMAIN ""
}

################################################################
#Build the command line. You may want to substitute
#DISPLAY for ALTDISPLAY
################################################################
set IsMetaFrame 0

switch $env(TTA_WindowsApplicationServer) {
    wincenter {
	set ProtocolCmd wincenter
    }
    wincentermf {
	set ProtocolCmd x11
	set IsMetaFrame 1
    }
}

if { [info exists DISPLAY] } {
    set tts \
	    "$ProtocolCmd -display $DISPLAY -username $uname -password $pword \
	    -resolution $env(TTA_AppletWidth)x$env(TTA_AppletHeight) $DOMAIN"
} else {
    set tts \
	    "$ProtocolCmd -username $uname -password $pword \
	    -resolution $env(TTA_AppletWidth)x$env(TTA_AppletHeight) $DOMAIN"
}


if { $IsMetaFrame == 0 } {
    # Does the user want the Wincenter software cursor?
    if { [info exists env(TTA_WinCursor)] } {
	if { $env(TTA_WinCursor) == 1 } {
	    append tts " -wincursor"
	}
    }
}

# Do we have any addition _wincenter_ command line arguments?
if { [info exists env(TTA_ProtocolArguments)] } {
    if { $env(TTA_ProtocolArguments) != "" } {
	append tts " $env(TTA_ProtocolArguments)"
    }
}
# Do we have an initial command to run or is it a full desktop session?
if { [info exists env(TTA_FilePath)] } {
    if { $env(TTA_FilePath) != "" } {
	# Is this just a placeholder to start a new session?
	if { $env(TTA_FilePath) != "-" } {
	    if { $IsMetaFrame == 0 } {
		append tts " -- wcprun $env(TTA_FilePath)"
	    } else {
		append tts " -exec $env(TTA_FilePath)"
	    }
	}
    }
}

# Do we have any additional command line arguments?
if { [info exists env(TTA_Arguments)] } {
    if { $env(TTA_Arguments) != "" } {
	append tts " $env(TTA_Arguments)"
    }
}

append tts "\r"

################################################################
#Wincenter's telnet port is 5999
#Rexec sends the command as part of the protocol. We need to 
#set this to our own wincenter command
################################################################

setbuffer -output 0
if {$env(TTA_Transport) == "telnet"} {
    send -- "$tts"
    # Delay exiting the script to ensure that the application
    # has time to launch before we break the connection
    sleep 2
    scriptcomplete
} elseif {$env(TTA_Transport) == "rcmd" } {
    tarantella -thirdTierShell $tts
} else {
    send_error "Unknown Transport Specified\n"
    exit $exitCode(ErrCommandExecutionFailed)
}

