#!/usr/bin/perl

#Written by Brian Epstein
#Saturday, January 4th, 1997
#Questions/Comments: ep@eden.rutgers.edu
#--------------------------------------------------------
# Usage: morris [-t <num>] [--help]
# takes Standard input and prints to standard output

#############################################################
##         Help Subroutine				   ##
#############################################################

sub help {
	print "\nUsage: morris [OPTION]\n";
	print "Concatenate standard input, with pauses between lines,",
	      " to standard output.\n\n";
	print "	-t <num>		uses <num> as the time interval",
	      " inbetween\n";
	print "				lines, <num> may be real number\n";
	print "	-b, -bn			bitwise, pauses between each bit\n";
	print "	 			-bn causes a newline to be added\n";
	print "	                        after each bit\n";
	print "	--help			display this help and exit\n\n";
} 

#############################################################
##		Morris Subroutine			   ##
#############################################################

sub morris {
	while ($input=<STDIN>) {
		print $input;           #prints one line at a time
		($seconds,@rest)=times() until $seconds>=($waitfor+$past);
                                        #loops for $waitfor seconds
                $past=$waitfor+$past;   #adds the past seconds on
	}
}

##############################################################
##		Morris Bitwise Subroutine		    ##
##############################################################

sub bitwise {
	if ($allparams =~ /-bn/) {	#tests to see if <CR>'s are to be used
		$cr = "\n";		#yes they are
	} else {
		$cr = "";		#no they aren't
	}
	while ($input=<STDIN>) {	#while there is input
		@input=split(//,$input);#break input into an array of char's
		foreach $char (@input) {#loop for each character
		      print $char, $cr;	     #prints one char at a time
	              ($seconds,@rest)=times() until $seconds>=($waitfor+$past);
               		                     #loops for $waitfor seconds
       		      $past=$waitfor+$past;  #adds the past seconds on
		} #foreach loop end
	} #while loop end
} #sub bitwise end

##############################################################
##		Main Subroutine				    ##
##############################################################

sub main {
	$| = 1;                         #disables <STDOUT> buffering
                                        #   for ircii compatibility
	$allparams=join('',@ARGV);      #gets params for -- options
	%params = @ARGV;                #gets command line params for options
	$waitfor = $params{"-t"} || 1;  #checks if default time is needed
        $past=0;                        #initializes the seconds gone by
	if ($allparams =~ /--help/) {	#if user wants help
		&help;
	} elsif ($allparams =~ /-b/) {	#if the user wants bitwise
		&bitwise;
	} else {			#if the user wants normal operation
		&morris;
	}
}

&main;					#calls main subroutine
