#!/afs/athena/contrib/perl/p

#
# check that there are some arguments 
srand;

$answered = 0;
$DOTFILE = 1;

$EMAIL_TO_TOP = 50;
$USENET = 50;
@TO_HOSTS = ( 'usenet@athena' );


# Topics 
$topic = 0;  # the default
$DOTFILES = 1;
$XSCREENSAVER = 2;
$USENET = 3;
		
&parse_arguments();
print "======================================================================\n";
&main();
print "======================================================================\n";



sub main {
	($QUESTION = $question) =~ y/a-z/A-Z/;

	@parsed_q = split(/ /, $QUESTION);

	if ( $QUESTION =~ /MOTHER/  ) {
		print "Tell me about your mother\n";
		$answered = 1;
		return; 
	}
	if ($question =~ /Chee/) {
		print "All Chinese people are short.\n";
		return;
	}
	if ($QUESTION =~ /DOTFILE/ ||
	    $QUESTION =~ /.CSHRC/ ||
	    $QUESTION =~ /.XSESSION/ ||
	    $QUESTION =~ /.STARTUP./ ||
	    $QUESTION =~ /.ENVIRONMENT/ ||
	    $QUESTION =~ /.PATH/ ||
	    $QUESTION =~ /.LOGIN/ ) {
		$topic = $DOTFILES;
		&real_question($DOTFILE);
		return;
	}
	if ($QUESTION =~ /SCREENSAVER/ ) {
		$topic = $XSCREENSAVER;
		&screensaver_probs();
		return;
	}
	if  ($QUESTION  =~ /SCRIBE/ )  {
		&check_olc_answers();
		$answered = 1;
	}
	if ($QUESTION =~ /BEACON/ ) {
		&use_news();
		$answered = 1;
	}
	if ($QUESTION =~ /NET NEWS/ || 
	    $QUESTION =~ /NET-NEWS/ ||
	    $QUESTION =~ /NETNEWS/  ||
	    $QUESTION =~ /PICAYUNE/ 
		) { 
		$topic = $USENET;
		&email_to($USENET);
		return; 
	}
	if (&check_why_does_mine_break()) {
		$answered = 1;
		return;
	}
	if ($QUESTION !~ /\?/) { 
		&not_a_question();
		return;
	}

	if( $topic != 0 ) {
		&switch_on_topic(); 
		return; 
	}

###### catch all ##################
	if ( $answered == 0 ) {
		if( defined $noflame_user   ) { 
		  if( 
		    defined $noflame_other_answers ) {
			print <<EOA;
Don't tell me who to flame and who not to flame. 
EOA
		} }
	
	$randval = rand(2) ;
	print "RTFM.\n";
	if( $randval < 1) {
		print "And if you don't like that answer, don't ask the question\n";
	}		
	return;
	}
}



############ Functions ################

sub parse_arguments {

if ($#ARGV == -1) { &usage(); }

$question = $ARGV[$[];

shift(ARGV);


for ($i=$[, $arg = $ARGV[$i++]; $i <= ( $#ARGV + 1); $arg = $ARGV[$i++]) {

       if ("$arg" eq "-realanswer") {
	#print "defining realanswer \n";
         $realanswer=1;
      } elsif ("$arg" eq "-noflame_user") {
	#print  "defining noflame_user\n";
         $noflame_user = 1;
      } elsif ("$arg" eq "-noflame_other_answers") {
	#print  "defining noflame_other_user\n";
         $noflame_other_answers=1;
      } elsif ("$arg" eq "-noflame") { 
	#print  "defining noflame_other_user\n";
	#print  "defining noflame_user\n";
        $noflame_user = 1;
        $noflame_other_answers = 1;
      } elsif ("$arg" eq "-I_already_RTFM") {
	#print  "defining I_already_RTFM\n";
         $I_already_RTFM=1;
      } elsif ("$arg" eq "-why_does_mine_break") {
	#print  "defining why_does_mine_break\n";
         $why_does_mine_break=1;
      } else {
        &bad_args($arg);
      }

## For debugging purposes only 
#  print "\$,i=$i; \$arg=$arg\n\n";
	
}

}

sub switch_on_topic {

}

sub use_news  {
	print  <<EOA;
All new  references  to beacon  should now point to news.mit.edu
EOA
}
sub check_olc_answers  {
	print <<EOA;
Have you tried olc_answers?
EOA
}
sub email_to {
	local($to_whom) = @_;
	$to_whom -= $EMAIL_TO_TOP ;
	$answered = 1;
	print <<EOA;
Those comments and questions should go to $TO_HOSTS[$to_whom].
EOA
}

sub screensaver_probs {
	if ( defined $I_already_RTFM ) {
		if ($QUESTION =~ /PASSWORD/) { 
			$answered = 1;
			print <<EOA;
I've never seen it happen.  It's never happened to me. 
Did you RTFM on xfree?  I didn't think so.
EOA
		} else {
			$answered = 1;
			print <<EOA;
RTFM again.
EOA
		}
	} else {
		if ($QUESTION =~ /PASSWORD/) {
			$answered = 1;
			print <<EOA;
I've never seen it happen.  It's never happened to me. 
RTFM on xfree.
EOA
		} else {
			$answered = 1;
			print <<EOA;
RTFM.  It's a good man page.  I wrote it myself. 
EOA
		}
	}			
}

sub real_question {
	local($topic) = @_;
	if ( $topic == 1) {
		if ( $question =~ /Where can I find the documentation on dotfiles/) {
			$answered = 1;
			print "Try the consulting office\n";
		} else { 
			$answered = 1;
			print <<EOA;
The Real question you want to ask is:
	Where can I find the documentation on dotfiles.
EOA
		}
	}
}

sub check_why_does_mine_break {
	if (!defined $why_does_mine_break) {
		return(0);
	}
	if (!defined $I_already_RTFM ) {
		if ( defined $realanswer ) {
			if  (defined $noflame_user) {
				print <<EOA;
What?  You didn't RTFM and you're asking me not to flame?  
RTFM first and then  I'll  consider not flaming.
EOA
			$answered = 1;
			return(1);
			}
			print "Don't ask me for the real answer until you've RTFM\n";
		} else {
			print "RTFM.\n";
		}
		return(1);
	}
	if (!defined $noflame_user) {
		print "RTFM again.\n";
		return(1);
	}
		
	print "It works fine for me.  It mus be something you're doing wrong\n";
	return(1);
}

sub not_a_question {
	print "Was that a question?\n"; 
	$answered = 1;
}

sub usage {
	$answered = 1;
	print "Usage: RTFM\n";
}

sub bad_args { local($bad) = @_;

	$answered = 1;
	print <<EOT; 

$bad is a bad argument
Please RTFM prior to asking me again.

EOT
  exit;
}
