#! /usr/bin/perl
#########################################################################################
#
# $Id: cataddr,v 1.1 1997/12/06 00:18:27 bcwhite Exp $
#
# cataddr
#
# Written by: Behan Webster <behanw@pobox.com>
#

use Getopt::Long;

require "SPAMPERL/Util.pl";

#
# Help file
#
sub usage {
	$0 =~ m|^.*?([^/]+)$|;
	print "Usage: $1 [[--append] [--force] --output=<filename>] <input_filename>\n";
	exit 0;
}

#
# Program Defaults
#
my $append	= 0;
my $force	= 0;
my $output;
my $quiet	= 0;

#
# Parse options
#
#$Getopt::Long::bundling = 1;
%options = (
	"append"	=> \$append,
	"force"		=> \$force,
	"output=s"	=> \$output,
	"quiet"		=> \$quiet,
);
usage if !GetOptions(%options);
usage if !@ARGV;

#
# Open output file
#
if($output) {
	die "$output already exists.\n".
		"If you really want to overrite it, use the --force option.\n"
		if -f $output && !$append && !$force;

	$_ =  ($append) ? ">>" : ">";
	open(STDOUT, $_.$output ) || die "$output: $!\n";
}

my $eol = $/;				# save the EOL character
my @addr = ();
my $input;

#
# Look for addresses in each file
#
foreach (@ARGV) {
	#
	# Read in file
	#
	open(TXT, "<$_") || die "$_: $!\n";
    undef $/;				# delete the EOL character
	$input = <TXT>;			# slurp in the file
	$/ = $eol;				# restore the EOL character
	close(TXT);

	#
	# Find addresses
	#
	$input =~ s|\W<?([\w.-=/]+\@[\w.-]+)\W|push(@addr, $1)|gise;	# Generic E-mail addr
	##$input =~ s|"?mailto:([^"\s]+)["\s]|push(@addr, $1)|gise;		# HTML E-mail addr
}

#
# Print out addresses
#
print join("\n", uniq(@addr) )."\n";

close(STDOUT);

exit 0;
