#! /usr/bin/perl -w
#
#
#########################################
#
#	this crazy program finds links
#	on determined pages of the 
#	DTN site and puts them into
#	a nice file to be able to display them
#
#########################################

@files = ( "whoweare.pl", "resources.pl", "prayer.pl", "travel.pl", "media.pl" );

open( DATA, ">./data/links_data.txt" );
print DATA "<links>\n";

foreach $file ( @files )
{
	my @links;

	print DATA "\t<page name=\"$file\">\n";
	open( INFILE, $file ) or print "<span style='font-weight: bold; color: red'>ERROR: Could not open $file file.</span>";

		while( my $fl = <INFILE> )
		{
			$double = 0;

			if( $fl =~ m/<a.*href="mailto:(.*)"\s?.*>(.*)<\/a>/ )
			{
				my $name = $2;
				my $link = $1;
				$link =~ s/\\@/@/g;

				foreach my $x ( @links ) { if( $x eq $link ) { $double = 1; } }

				if( !$double ) { push( @links, $link ); print DATA "\t\t<email>$name=$link</email>\n"; }
			}
			elsif( $fl =~ m/<a.*href="(http:\/\/.*\.\w{3})"\s?.*>(.*)<\/a>/ )
			{
				my $name = $2;
				my $link = $1;

				foreach my $x ( @links ) { if( $x eq $link ) { $double = 1; } }

				if( !$double ) { push( @links, $link ); print DATA "\t\t<link>$name=$link</link>\n"; }
			}
			elsif( $fl =~ m/<a.*href="(#\w+)"\s?.*>(.*)<\/a>/ )
			{
				my $name = $2;
				my $link = $1;

				unless( $link =~ m/top/ )
				{
					foreach my $x ( @links ) { if( $x eq $link ) { $double = 1; } }

					if( !$double ) { push( @links, $link ); print DATA "\t\t<anchor>$name=$link</anchor>\n"; }
				}
			}
				
		}

	close( INFILE );
	print DATA "\t</page>\n";
}

print DATA "</links>";
close( DATA );

print "Content-type: text/html\n\n";
print<<"HTML";
	<html>
	<head><title>links updated</title>
	</head>
	<body style="background-color: #000000">
	<div align="center">
	<span style="font-size: 18px; font-family: arial; font-weight: bold; color: #999999;">Links updated!</span>
	<br />&nbsp;<br />
	<input type="button" value="Close Window" onclick="window.close()" />
	</div>
	</body>
	</html>
HTML

exit;