#!/usr/bin/expect -f
#
# Routing through telnet and clxd
#

# --------------------- User configurable ---------------------
set destination "xx0xx-1"
set host "localhost"
set port "clx"
set password "xx0xx"
# --------------------- User configurable ---------------------

set timeout 10
log_user 1

#
# Just for debugging
#

proc syslog {msg} {

# To put something into the log

	global destination
        set filename "$destination script"
        exec logger -p local5.info "$filename (pid=[pid]): $msg"
}

set con_ctl $user_spawn_id
set con_ctl_pid [pid]

# Read the destination from con_ctl. It writes to us something like:
#
#       connect ax25 db0bcc
#
expect timeout {
	syslog "timed out waiting for connect command"
	exit 1
} -re "(connect ax25 .*)\n"

set destination [ lindex $expect_out(0,string) 2 ]
set mycall [ lindex $expect_out(0,string) 3 ]

spawn /usr/bin/telnet $host $port

expect timeout {
	syslog "timed out waiting for \"Your callsign:\" from $host"
	exit
} "sign:"

send "$mycall\r"

expect "word:"
send "$password\r"

expect -re "\\\*\\\*\\\* connected to"
syslog "Connected to $destination"

interact "\3" {
		syslog "ctrl-C pressed - exiting"
		exit
	}
syslog "eof rcvd from telnet - exiting"
