#!/usr/bin/perl -w

# Copyright (C) 2000-2003 Simon Huggins
# catsig deals with choosing a sig

# 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


# You can either specify sigs with
# 	$cfg{'sigs'}
# 		An arrayref to an array of filenames of sigs.
# 	$cfg{'sigdir'}
# 		A scalar containing a directory of all your sig files.
# 	$cfg{'sigmatch'}
# 		A scalar containing a pattern to match the files in sigdir
# 		(full path) against.
#
#	So specifying sigdir without sigmatch takes all the files in that
#	directory that don't start with a fullstop (period) and specifying
#	it with sigmatch only takes those that match.
#	Note that if you specify sigs *AND* sigdir you'll get all of sigs
#	and anything that sigdir/sigmatch matches.
#	The program dies if after trying to match it comes up with nothing.

use strict;

if (defined $cfg{'sigdir'}) {
	opendir(DIR, $cfg{'sigdir'})
		or htagdie "Could not open directory $cfg{'sigdir'}: $!\n";
	if (not defined $cfg{'sigmatch'}) {
		push @{$cfg{'sigs'}}, 	map  { $cfg{'sigdir'} . "/" . $_ }
					grep { ! /^\./ }
					readdir(DIR);
	} else {
		# Check regexp wouldn't kill us
		exec { "" =~ $cfg{'sigmatch'};}
		htagdie "sigmatch contained bad pattern.  perl said: $@" if $@;
		push @{$cfg{'sigs'}}, grep { m,$cfg{'sigmatch'}, }
					map  { $cfg{'sigdir'} . "/" . $_ }
					readdir(DIR);
		print STDERR "Got sigmatch == $cfg{'sigmatch'}\n";
	}
	closedir(DIR);
}

htagdie <<EOF if (not defined $cfg{'sigs'} or scalar @{$cfg{'sigs'}} == 0);
No signatures found.  Either you didn't specify sigs, or there weren't any
files in sigdir or your sigmatch didn't match anything.
EOF

sub quit() {
	return 255;
}

sub write_sig($) {
	my $sig = shift;

	open(OUT, ">$cfg{'tmpsigfile'}") or htagdie "Couldn't open $cfg{'tmpsigfile'}";
	print OUT $sig;
	close(OUT);
	open(OUT, ">$cfg{'tmpsigfile'}.old") or htagdie "Couldn't open $cfg{'tmpsigfile'}.old";
	print OUT $sig;
	close(OUT);
}

sub choose_sig {
### Choose a sig and read it into $sig for grepping for @72@ or whatever.
	my ($sigfile,@oldsiglist,@siglist,$num,$sig);

	@oldsiglist = @siglist = @{$cfg{'sigs'}};

	do {	
		$sig="";

		if (@siglist) {
			# Find and delete
			$sigfile=splice(@siglist,rand(@siglist),1);
		} elsif (-r "$cfg{'HOME'}/.sig") {
			print STDERR "Falling back on ~/.sig\n" if $cfg{'debug'};
			$sigfile="$cfg{'HOME'}/.sig";
		} else {
			print STDERR "No sigs specified and no ~/.sig!  Using simple tag method\n";
		}
		
		if (not $sigfile) {
			$sig="";
			write_sig($sig);
			return;
		}
		
		print STDERR "Using $sigfile\n" if $cfg{'debug'};

		open(HANDLE, "<$sigfile") or htagdie "Could not open $sigfile: $!\n";
		while(<HANDLE>) { $sig .= $_ };
		close(HANDLE);

		if ($cfg{'asksig'}) {
			print STDERR $sig . "\nUse this sig? ([y]/n/q)";
			$_ = <STDIN>;
			if ($_ =~ /q(?:uit)?/i) { return &quit; }
			if (not @siglist and $_ =~ /no?/i) {
				print STDERR "That was the last sig.  Reset siglist or quit? ([r]/q)";
				$_ = <STDIN>;
				if ($_ =~ /q(?:uit)?/i) { return &quit; }
				@siglist = @oldsiglist;
				$_ = 'n';
			}

		}

	} while ($cfg{'asksig'} and $_ !~ /y(?:es)?/i and $_ !~ /^\n$/);
	write_sig($sig);
	return;
}

reg_deletion($cfg{'tmpsigfile'});
reg_deletion("$cfg{'tmpsigfile'}.old");
&choose_sig;
