#!/usr/bin/perl -w

# Copyright (C) 2000-2003 Simon Huggins
# simple just chooses a random tagline in the simplest possible way
# or uses fortune(1)

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place, Suite 330, Boston, MA 02111-1307  USA

use strict;

my (@tags);

# srand( time() ^ ($$ + ($$ << 15) )); # Since 5.004 not required.

my $sig="";
open(SIG, "<$cfg{'tmpsigfile'}") or htagdie "$0: Could not open $cfg{'tmpsigfile'}: $!\n";
while(<SIG>) {
	$sig .= $_;
}
close(SIG);

if (grep { /\@NOTAG\@/ } $sig) {
	$cfg{'notag'}=1;
	return 15;
}

htagdie <<EOF if (not defined $cfg{'tagfile'} and not defined $cfg{'tagfiles'} and not defined $cfg{'fortune'} and not defined $cfg{'tagdir'}); 
No tagfile defined!  I'm good but I'm not psychic
Did you configure me at all?  Look at the sample.htrc
EOF

htagdie <<EOF if ($cfg{'tagfiles'} && $cfg{'tagfile'});
You have defined both \$cfg{'tagfile'} and \$cfg{'tagfiles'} in your config
file(s).  Please use one or the other.
EOF

if (defined $cfg{'tagfile'}) {
	push @{$cfg{'tagfiles'}}, $cfg{'tagfile'};
}

if (defined $cfg{'tagdir'}) {
	opendir(DIR, $cfg{'tagdir'})
		or htagdie "Could not open directory $cfg{'tagdir'}: $!\n";
	if (not defined $cfg{'tagmatch'}) {
		push @{$cfg{'tagfiles'}},
			map  { $cfg{'tagdir'} . "/" . $_ }
			grep { ! /^\./ }
			readdir(DIR);
	} else {
		# Check regexp wouldn't kill us
		exec { "" =~ $cfg{'tagmatch'};}
		htagdie "tagmatch contained bad pattern.  perl said: $@" if $@;
		push @{$cfg{'tagfiles'}},
			grep { m,$cfg{'tagmatch'}, }
			map  { $cfg{'tagdir'} . "/" . $_ }
			readdir(DIR);
htagdie <<EOF if (not @{$cfg{'tagfiles'}});
tagdir didn't match anything and you had no tagfiles defined.
EOF

	}
	closedir(DIR);
}

my $tag;

if ($cfg{'fortune'} && $cfg{'fortuneval'} && $cfg{'fortuneval'} > rand()) {
	my $cmd = $cfg{'fortune'};
	$cmd .= " ".$cfg{'fortuneargs'} if defined $cfg{'fortuneargs'};
	
	$tag = `$cmd`;
	htagdie "fortune died: $!" if $?;
} else {
	foreach (@{$cfg{'tagfiles'}}) {
		open(HANDLE, "<$_")
			or htagdie "Could not open $_: $!\n";
		push @tags, <HANDLE>;
		close(HANDLE);
	}
	if ($cfg{'tagline_comment_char'}) {
		@tags = grep { ! /^$cfg{'tagline_comment_char'}/ } @tags;
	}
	$tag = $tags[rand(@tags)];
}

open(OUT, ">$cfg{'tmptagfile'}") or htagdie "$0: Could not open $cfg{'tmptagfile'}: $!\n";
reg_deletion("$cfg{'tmptagfile'}");
chomp $tag;
print OUT $tag;
close(OUT);
return;
