#!/usr/bin/perl

#----------------------------------------#
#   Color Text: Perl Cookbook, pg 522 	 #
#----------------------------------------#
use Term::ANSIColor;

# to upcase the read/write input, I use uc which needs locale
use locale;

#----------------------------------------#
#   Clear Screen: Perl Cookbook, pg 521	 #
#----------------------------------------#
use Term::Cap ;
$OSPEED=9600;
eval {
	require POSIX;
	my $termios=POSIX::Termios->new();
	$termios->getattr;
	$OSPEED= $termios->getospeed;
};
$terminal = Term::Cap->Tgetent({OSPEED=>$OSPEED});
$terminal->Tputs('cl',1,STDOUT);
$clear=$terminal->Tputs('cl');
$clear= `clear`;
print $clear;

#------------------------------------------#
#   Main loop:				   #
#------------------------------------------#
print 	color("red"),	"###############################################################",color("reset"),"\n", 
	color("white"),	"Presenting:                                                    ",color("reset"),"\n",
	color("white"),	"       Exersize 4.12  Problem 1                                ",color("reset"),"\n",
	color("red"),	"###############################################################",color("reset"),"\n",
	color("white"),	"I will ask if you want to \"read\" or \"write\" data.          ",color("reset"),"\n",
	color("white"),	"Then ask for a file to read or write to.                       ",color("reset"),"\n",
	color("white"),	"For writing, I\'ll prompt for input, or 'quit' to exit.         ",color("reset"),"\n",
	color("white"),	"If you chose read, I\'ll dump the file\'s contents to the screen. ",color("reset"),"\n";
	
print 	color("blue"),	"Do you want to ",color("clear"),"READ or WRITE",color("blue")," a file? \t",color("clear");
$Operation=<>;chomp $Operation; $Operation=uc($Operation);

if ( "\U$Operation" eq "WRITE" ){
	print color("green"),"What is the ",color("clear"),"NAME",color("green")," of the output file?\t",color("clear");
	$File=<STDIN>; chomp ($File);
	open(OUT,">$File") || die color("red"),"Can't open $File: $!",color("clear");
	
	# This variable will contain the keyboard input string
	my $data ="";	
	
	#
	#  Input from the user once before entering the Until loop.
	#  If the first input was QUIT, it will not go into the data file. 
	#  Once theydo type in quit.
	#
	print color("magenta"),"Enter a string, type ",color("clear"),"QUIT",color("magenta")," if you are done:\t",color("clear");
	$data=<STDIN>; chomp ($data);
	#
	# Perl Cookbook, recipe 1.9 (requires module: locale)
	#	uppercase all the input data, so we can deal with 
	# 	quit or QUIT
	until ( "\U$data" eq "QUIT" ){ 
		print color("magenta"),"Enter a string, type ",color("clear"),"QUIT",color("magenta")," if you are done:\t",color("clear");
		print OUT "$data\n" ;
		$data=<STDIN>; chomp ($data);
	}
	close (OUT)|| die "Can't close $File: $!";	
	print color("yellow"),"Finished entering data. Data written to \"",$File,"\".\n",color("clear");	
	
} elsif ( "\U$Operation" eq "READ" ){
	print color("green"),"What is the ",color("clear"),"NAME",color("green")," of the input file?\t",color("clear");
	$File=<STDIN>; chomp ($File);
	open(IN,"<$File") || die color("red"),"Can't open $File: $!",color("reset") ;
	print "This file contains: \n";
	while (<IN>){
		print ;
	}
	# 
	# You gota close those file descriptors.
	#
	close (IN) || die "Can't close $File: $!";
} else {
	die "I need you to answer ",color("clear"),"READ or WRITE",color("blue"),", and you typed $Operation.\nError: $!\n",color("clear");
}
