#!/usr/bin/perl

# parent process (uucico)
sub parent {
	close rd_tel2uu;
	close wr_uu2tel;

	open ( STDIN, "<&rd_uu2tel" );
	open ( STDOUT, ">&wr_tel2uu" );
	select (STDOUT); $|=1;

	print "set echo off\n";
	print "set escape off\n";
	print "set rlogin off\n";
	# print "set binary\n";

	print "open $net_host\n";

	$cmd="/usr/lib/uucp/uucico -S $uucp_host -p pipe -c -x chat -x incoming -x proto -x handshake -x uucp-proto";
	print STDERR "$cmd\n";
	exec ( $cmd );
}

# child process (telnet)
sub child {
	close wr_tel2uu;
	close rd_uu2tel;
	select (STDOUT); $|=1;

	open ( STDIN, "<&rd_tel2uu" );
	open ( STDOUT, ">&wr_uu2tel" );

	$cmd="/usr/bin/telnet -E";
	print STDERR "$cmd\n";
	exec ( $cmd );
}

if ( $#ARGV < 1 ) {
	die "usage: $0 net-hostname uucp-hostname\n";
}

$net_host=$ARGV[0];
$uucp_host=$ARGV[1];

# create in/out pipes
pipe ( rd_tel2uu, wr_tel2uu );
pipe ( rd_uu2tel, wr_uu2tel );

# fork into parent & child ends of pipe
$done=0;
while(!$done) {
	$pid = fork;
	if ( $pid == 0 ) {
		# child
		do child();
		$done=1;
	} elsif ( defined $pid ) {
		#parent
		do parent();
		$done=1;
	} elsif ( $! =~ /cannot fork/i ) {
		sleep 1;
		next;
	} else {
		die "$0: fork failed: $!\n";
	}
}
