#!/afs/athena/contrib/perl/p
$debug =0;
$qline = '^[ \t]*[a-z]?[\-Q \W.]*[a-z]?\d[\d\.\-a-z]*[\W\.\):]*[ \t]+';

while(<>){
    print("%% $_") if $debug;
    if($body || $q){
	$body=0;
	if(/$qline((.+\?)+)[ \t]*$/){
	    print("\$q; $_\n") if $debug;
	    $question = $1;
	    &qanda;
	}
	elsif(/^(Subject:|Q:)[ \[\]\d\.\-]+(.+)/){
	    print("\$sq; $_") if $debug;
	    $question = $2."\n";
	    $subj=1;
	}
	elsif($subj){
	    print("\$jq; $_\n") if $debug;
	    if(/^$/){
		$subj=0;
		&qanda;
	    }
	    elsif($question=~/\?/){
		$subj=0;
		&qanda;
		$answer .= $_;

	    }
	    else{
		$question .= $_;
	    }
	}
	elsif(/^(.+\?)[ \t]*$/){
	    print("\$oq; $_\n") if $debug;
	    $longq=1;
	    $lq = $1."\n";
	    $q=1;
	}
	elsif(/$qline(.+)/){
	    print("\$lq; $_\n") if $debug;
	    $longq=1;
	    $lq = $1."\n";
	    $q=1;
	}
	elsif($longq && /((.+\?)+)[ \t]*$/){
	    print("\$eq; $_\n") if $debug;
	    &lqanda;
	}
	elsif($longq && /^$/){
	    print("\$bq; $_\n") if $debug;
	    if($lq=~/\?/){
		&lqanda;
	    }
	    elsif(length($lq)<100){
		&lqanda;
	    }
	    else{
		print("\$nq; $_\n") if $debug;
		$answer.=$lq;
	    }
	    $lq='';
	    $longq=0;
	}
	elsif($longq){
	    print("\$iq; $_\n") if $debug;
	    $lq.=$_;
	}
	else{
	    print("\$;; $_\n") if $debug;
	    $answer .= $_;
	}
    }else{
	print(":::: $_") if $debug;
	$answer='';
	$body =1 if /^$/;
	$q =1 if /^$/;
    }
}
&doa;


sub qanda {
    $answer.=$lq;
    &doa;
    &doq;
}
sub doa {
    $longq=0;
    if($answer && $pquestion){print("$pquestion\n$answer\n");}
#    else{print("No Answer\n");}
    
}
sub doq{
    $answer='';
    $stars = "*"x30;
    $pquestion = "\n$stars\n$question\n$stars\n";

}
sub lqanda{
    &doa;
    $question = $lq.$_;
    &doq;
}


