#  $Id: wcpwts.exp,v 1.12 2003/07/25 07:55:30 peterc Exp $
#  Copyright (C) 1998-2003 Tarantella, Inc. 
#
# wcpwts.exp: expect script to start a Windows Terminal session
#
global argv env

set timeout 60
set uname [lindex $argv 0]
set pword [lindex $argv 1]
set promptForAuth 0
set xauthcookie ""

# Domain to Use
#set env(TTA_Domain) ""

set args(DirCachePath) $env(TTADATADIR)
append  args(DirCachePath) "/log/cache/"
set spos [string last "/" $env(TTA_LOGFILE)]
set pos [expr $spos+1]
set lon [string last ".log" $env(TTA_LOGFILE)]
set args(ExpFile) $args(DirCachePath)[string range $env(TTA_LOGFILE) $pos $lon]exp
set args(scan_ttacachePath) "STTATOOLSDIR"
set args(scan_ttacacheCmd) "scanttacache"

tarantella -nosocket
set uname [lindex $argv 0]
set pword [lindex $argv 1]

set env(tta_username) $uname
set env(tta_password) $pword

system "$args(scan_ttacachePath)/$args(scan_ttacacheCmd) >$args(ExpFile)"
system "chown bin:bin $args(ExpFile)"
source $args(ExpFile)
system "rm -f $args(ExpFile)"

if { $msg_access != "" } {
     send_error "$msg_access"
     exit $exitCode(ErrApplicationServerLoginFailed)
}

# Prompt for authentication if any of:
# 1. no username
# 2. no domain (explicit or implicit in username)

if { [string length $uname ] == 0 } {
    set promptForAuth 1
} else {
    if { [info exists env(TTA_Domain)] } {
        set initialDomain $env(TTA_Domain)
    } else {
        set initialDomain ""
    }

    # Check for domain\user
    if { $initialDomain == "" && [string first "\\" $uname] == -1 } {
        set promptForAuth 1
    }
}

if { $promptForAuth == 1 } {
    authrequest -normal
    set uname $username
    set pword $password
    if { [string length $ntdomain ] > 0 } {
	set env(TTA_Domain) $ntdomain
    }
}

tarantella -nosocket

################################################################
#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
    } elseif {$name == "TTA_XAUTHCOOKIE"} {
        set xauthcookie $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)
}
if { ![info exists env(TTA_Domain)] } {
    scan $env(TTA_HOSTNAME) "%\[A-z\]" DOMAIN
} else {
    set DOMAIN $env(TTA_Domain)
}

if { [info exists env(TTA_ApplicationPlacement)] } { 
    if { $env(TTA_ApplicationPlacement) == "MultipleWindows" } {
       send_error "Cannot run with the client window manager\n"
       exit $exitCode(ErrCommandExecutionFailed)
    }
}

################################################################
#Build the command line. You may want to substitute
#DISPLAY for ALTDISPLAY
################################################################
set ProtocolCmd "ttatsc"

if {[string length "$uname"] > 0} { 
    set USERCMD "-user"
    #is the user in the form of domain\user
    set idx [string first "\\" $uname]
    if { $idx != -1 } {
        set DOMAIN [string range $uname 0 [expr $idx - 1] ]
        set uname [string range $uname [expr $idx + 1] [ string length $uname ]]
    }
} else {
    set USERCMD {}
}

if {[string length "$pword"] > 0} { 
    set PASSWORDCMD "-password"
} else {
    set PASSWORDCMD {}
}

if {[string length "$DOMAIN"] > 0} { 
    set DOMAINCMD "-domain"
} else {
    set DOMAINCMD {}
}

if {[string length $xauthcookie] > 0} {
    set XAUTHCOOKIECMD "-xauthcookie"
} else {
    set XAUTHCOOKIECMD {}
}

# Do we have an initial command to run or is it a full desktop session?
set APPCMD {}
set APP {}
if { [info exists env(TTA_FilePath)] } {
    if { $env(TTA_FilePath) != "" } {
	# Is this just a placeholder to start a new session?
	if { $env(TTA_FilePath) != "-" } {
             set APPCMD "-application"
             set APP "$env(TTA_FilePath)"
             if { [info exists env(TTA_Arguments)] } {
                  if { $env(TTA_Arguments) != "" } {
                       append APP " $env(TTA_Arguments)" 
                  }
             }
             if { $appextra_cmd != "" } {  
                     append APP " $appextra_cmd "
             }
	} 
    }
}

set TTATSC_ARGUMENTS {}
if { [info exists env(TTA_ProtocolArguments)] } {
    set TTATSC_ARGUMENTS "$env(TTA_ProtocolArguments)"
}

set NAMECMD "-name"
set TTATSC_NAME "tarantella user"
if { [info exists env(TTA_DisplayName)] } {
    set TTATSC_NAME "$env(TTA_DisplayName)"
} else {
    if { [info exists env(TTA_UserName)] } {
        set NAME $env(TTA_UserName)
        if [ expr [string first ".../_anon/" $NAME ] != 0 ] {
            set idx [string last "/" $NAME]
            set TTATSC_NAME [string range $NAME [expr $idx + 1] [string length $NAME ] ]
        } else {
            set TTATSC_NAME "anonymous"
        }
    }
}
################################################################
#Execute the command						
################################################################
set env(TTATSC) "$TTATSC_ARGUMENTS"
set TTATSC1 "$USERCMD $uname\n$PASSWORDCMD $pword\n$XAUTHCOOKIECMD $xauthcookie\n\n"
if [catch {exec -- echo $TTATSC1 | $ProtocolCmd "-stdin" "-display" $DISPLAY $DOMAINCMD $DOMAIN $APPCMD $APP $NAMECMD "$TTATSC_NAME" $env(TTA_HOSTNAME) } res ] {
    set err [lindex $errorCode [expr [llength $errorCode] - 1]]
    send_error "\n"
   if {$err == 8} {
       send_error "Failed to connect to host $env(TTA_HOSTNAME)\n"
       exit $exitCode(ErrApplicationServerConnectionFailed)
   } elseif {$err == 2 } {
       send_error "Cannot run with a window manager\n"
       exit $exitCode(ErrWindowsManagerExists)
   } elseif {$err == 3 } {
       send_error "Invalid desktop size\n"
       exit $exitCode(ErrInvalidDesktopSize)
   } elseif {$err == 4} {
       send_error "The temporary Terminal Services license has expired\n"
       exit $exitCode(ErrTscLicenseError)
   } elseif {$err == 5} {
       send_error "No Terminal Services licenses are available\n"
       exit $exitCode(ErrTscLicenseError)
   } elseif {$err == 6} {
       send_error "The Terminal Services license is invalid\n"
       exit $exitCode(ErrTscLicenseError)
   } elseif {$err == 7} {
       send_error "Unknown Terminal Services license error\n"
       exit $exitCode(ErrTscLicenseError)
   } else {
       send_error "$res (error = $err)\n"
       send_error "$exitMessage(ErrCommandExecutionFailed)\n"
       exit $exitCode(ErrCommandExecutionFailed)
   }
}



