#!/usr/bin/perl
#
#	raregetty
#	written by Jan Engelhardt, 2004-2007
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the WTF Public License version 2 or
#	(at your option) any later version.
#

chdir "/var/lib/empty";
chroot "/var/lib/empty";

use Getopt::Long;
use strict;

my($Clearscr, $Noreopen);
my $Defaulthost = "127.0.0.1";
my $Host        = "*";

&Getopt::Long::Configure(qw(bundling pass_through));
&GetOptions(
  "c"   => \$Clearscr,
  "d=s" => \$Defaulthost,
  "h=s" => \$Host,
  "n"   => \$Noreopen,
);

my $Tty = pop @ARGV;
&query4ssh();

sub query4ssh ()
{
	my $login;

	&reopen_ttys();
	$< = $> = 65534;

	while ($login eq "") {
		if ($Clearscr) {
			print "\e[1;1H\e[2J";
                }
		print "login: ";
		chomp($login = <STDIN>);
	}

	if ($Host eq "*") {
		print "host [$Defaulthost]: ";
		chomp($Host = <STDIN>);
		if( $Host eq "") {
			$Host = $Defaulthost;
		}
	}

	exit(exec "ssh", $login."\@".$Host);
}

sub reopen_ttys ()
{
	if (!$Noreopen) {
		open(STDIN, "< /dev/$Tty");
		open(STDOUT, "> /dev/$Tty");
		open(STDERR, "> /dev/$Tty");
	}
	select((select(STDOUT), $| = 1)[0]);
	select((select(STDERR), $| = 1)[0]);
	return;
}
