#!/usr/local/bin/perl -w
#!/usr/bin/perl -w

#use strict;	#To try and stop me from installing bugs

#-------------------------------------------------
#-- pon.pl (Print on Nyquist)
#--
#-- Written by: Ryan Northcott
#-- Begun: 2 Mar 2001
#-- Completed: 26 Mar 2001
#-------------------------------------------------

require 5.002;
use Getopt::Long;
use POSIX;
use IPC::Open2;
use IPC::Open3;
use FileHandle;

#-------------------------------------------------
#-- The main program:
#-------------------------------------------------

setupTerminal();
moveIntoPrintDirectory();
getFilesToPrint();
selectPrinter();
getLoginInfo();
ftpToNyquist();
telnetToPoisson();
promptToDelete();
print "Done spooling postscript files to Nyquist.\n";


#-------------------------------------------------
#-- Subroutines:
#-------------------------------------------------

sub setupTerminal # Make it possible to turn local echo off
{
	#if (!($opt_passwd) && $opt_noecho)
	#{
		$t = POSIX::Termios->new();
		$t->getattr();
		$term_orig = $t->getlflag();
	#}
}

#-------------------------------------------------

sub echo_off # Turn local echo off
{
	my($echo) = (&POSIX::ECHO|&POSIX::ECHOK);
	$no_echo = $term_orig;
	$no_echo &= ~$echo;
	$t->setlflag( $no_echo );
	$t->setattr( 0, &TCSANOW);
}

#-------------------------------------------------

sub echo_on # Turn local echo off
{
	$t->setlflag( $term_orig );
	$t->setattr( 0, &TCSANOW);
}

#-------------------------------------------------

sub getLoginInfo # Prompt for username and password
{
	printf "Please enter you EE login name: ";

	$_ = <STDIN>;

 	s/\n//;		# Delete the trailing return character
 	$USERID = $_;	

	#printf "Your login name is \'".$USERID."\'\n";

	printf "Please enter you EE password: ";

	echo_off();
	$_ = <STDIN>;
	echo_on();
	printf "\n";

	s/\n//;		# Delete the trailing return character
	$PASSWORD = $_;

	#printf "Your password is \'".$PASSWORD."\'\n";
}

#-------------------------------------------------

sub moveIntoPrintDirectory
{
	chdir;		# Move into the root directory
	die "The \'PrintFiles\' directory doesn't exist." unless chdir 'PrintFiles/';	# Move into the printing directory
}

#-------------------------------------------------

sub getFilesToPrint # Create an array of .ps filenames
{
	print "Getting list of postscript files to print...\n";
	$i = 0;
	while (defined($nextname = <\.\/*.ps>))
	{
		$_ = $nextname;
		s/.\///;
		$FileList[$i] = $_;
		print "Found: ".@FileList[$i]."\n";
		$i++;		
	}
	print "Total Files: ".@FileList."\n";
}

#-------------------------------------------------

sub selectPrinter # Choose a printer to print on
{
	print STDOUT "\nAvailable Printers:\n";
	print STDOUT "(1) Room CEB B54\n";
	print STDOUT "(2) Room CEB 540\n";
	print STDOUT "(3) Room CEB 548\n";

	$i = 0;
	while( not(($i == 1) || ($i == 2) || ($i == 3)) )
	{
		print STDOUT "Which printer do you want to print on (1-3): ";

		$_ = <STDIN>;
		s/\n//;		# Delete the trailing return character
		$i = $_;
	}

	if( $i == 1 )
	{
		$PRINTER = "b54lj";
	}
	if( $i == 2 )
	{
		$PRINTER = "HPPS_1";
	}
	if( $i == 3 )
	{
		$PRINTER = "548ps";
	}
}

#-------------------------------------------------

sub ftpToNyquist # Login and send the files to nyquist
{
	# No trailing pipe is used with open2
	
	if (! open3 (FOUT, FIN, FERR, "ftp -v -i nyquist.ee.ualberta.ca"))
	{
		die "Can't run ftp! $!\n";
	}

	$i = 0;
	while (<FIN>)
	{
		if ( not($i == 2) ) # Don't show username & password
		{
			print STDOUT $i."## ", $_;
		}
		if ( $i == 1 ) # Line 2 is the username prompt 
		{
			print STDOUT "\n\nSending username...\n";
			print STDOUT "Please enter your Nyquist password again: \n";
			print FOUT $USERID."\n";
		}
		#
		# Since FTP does not use STDIN to read the password, we cannot do the following
		# Instead, we prompt the user (in the code above) to type it in again by hand.
		# The FTP program must use some kind of getc() function to directly read the keys pressed.
		#
		#if ( $i == 2 ) # Line 3 is the password prompt
		#{
		#	print STDOUT "Sending password...\n\n";
		#	$len = length( $PASSWORD ) + 1;
		#	syswrite FOUT, $PASSWORD."\n", $len;
		#	print FOUT $PASSWORD."\n\n";
		#}
		#
		if ( $i == 3 ) 
		{
			if( s/Login incorrect.// )
			{
				die "Fatal Error:  Your password was not accepted by FTP!\n";
			}
			print FOUT "cd PrintFiles\n";
			print FOUT "mput *.ps\n";	# Copy all .ps files to Nyquist
			print FOUT "quit\n";		# End the FTP Session
		}

		$i = $i+1;
	}
}

#-------------------------------------------------

sub telnetToPoisson # Login, print, and delete the files from Poisson (the print server)
{
	# No trailing pipe is used with open2

	if (! open2 (TIN, TOUT,"telnet poisson.ee.ualberta.ca"))
	{
		die "Can't run telnet! $!\n";
	}

	$i = 0;
	$j = 0;
	while (<TIN>)
	{
		if ( not(($i == 16) || ($i == 17))  ) # Don't show username & password
		{
			print STDOUT $i."## ", $_;
		}
		if ( $i == 15 ) # Line 16 is the username prompt 
		{
			pause3Seconds();
			print STDOUT "\nSending username...\n";
			print TOUT $USERID, "\n";
		}
		if ( $i == 16 ) # Line 17 is the password prompt
		{
			pause3Seconds();
			print STDOUT "Sending password...\n\n";
			print TOUT $PASSWORD, "\n";
		}
		if ( $i == 18 ) # Check for bad login
		{
			if( s/Login incorrect// )
			{
				die "Fatal Error:  Bad password or username for telnet. \n";
			}
		}
		if ( $i == 25 ) # Line 26 is the terminal type prompt
		{
			pause3Seconds();
			print TOUT "vt100\n";
		}
		if( $i == 28 )
		{
			printFiles();
			print TOUT "exit\n";		    # Delete the file from EE account
		}		
		$i = $i+1;
	}
}

#-------------------------------------------------

sub printFiles # Send the print command for each file
{
	print STDOUT "\n\nSpooling the files to printer ".$PRINTER.":\n\n";
	print TOUT "cd PrintFiles\n";
	foreach( @FileList )
	{
		print TOUT "lp -d ".$PRINTER." ".$_."\n";  # Spool a file
		pause1Second();
		print TOUT "rm ".$_."\n";		    # Delete the file from Nyquist
	}	
}

#-------------------------------------------------

sub pause3Seconds # Pause for 3 seconds
{
	$end = time + 3;
	while( $end > time )
	{
	}
}

#-------------------------------------------------

sub pause1Second # Pause for 1 second
{
	$end = time + 1;
	while( $end > time )
	{
	}
}

#-------------------------------------------------

sub promptToDelete # Prompt to delete the .ps files locally
{
	$i = "a";
	while( not(($i eq "y") || ($i eq "n")) )
	{
		print STDOUT "Delete the .ps files from your local \'PrintFiles\' directory? (y or n): ";

		$_ = <STDIN>;
		s/\n//;		# Delete the trailing return character
		$i = $_;
	}

	if( $i eq "y" )
	{
		foreach( @FileList )
		{
			system "rm ".$_."\n";	# Delete the file locally
		}
		print "\nAll postscript files have been deleted loaclly!\n";
	}
	if( $i eq "n" )
	{
		print "\nAll postscript files have been left in the local \'PrintFiles\' directory!\n";
	}
	print "The current working directory is the root directory.\n";
}

