#!/usr/bin/perl -w
#  Copyright (c) 2002 Nik Clayton
#  All rights reserved.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
# 
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#  SUCH DAMAGE.
# 
#  $Id$

use strict;

use WWW::Sitemap;
use LWP::UserAgent;
use GraphViz;

my $root = "http://ukug.uk.freebsd.org/";
my $ua   = new LWP::UserAgent;

my $sitemap = new WWW::Sitemap(EMAIL      => 'nik@FreeBSD.org',
			       USERAGENT  => $ua,
			       ROOT       => $root,
			       DEPTH      => 2);

$sitemap->url_callback(
		       sub {
			   my ( $url, $depth, $title, $summary ) = @_;
			   print STDERR "URL: $url\n";
			   print STDERR "DEPTH: $depth\n";
			   print STDERR "TITLE: $title\n";
			   print STDERR "SUMMARY: $summary\n";
			   print STDERR "\n";
		       }
		      );

$sitemap->generate();

my $graph = GraphViz->new(node => {shape => 'box'},
			  rankdir => 0,
			  concentrate => 1,
			 );

my @links;
my %titles = ();

# These are URLs that almost every page links to.  Later on, we only
# include a link in the graph if it's the second link on the page, to
# ignore the default links.
my %deflinks = ();
my %deflinks2 = ($root . "index.html"                     => 1,
		$root . "ports/index.html"              => 1,
		$root . "support.html"                  => 1,
		$root . "docs.html"                     => 1,
		$root . "commercial/commercial.html"    => 1,
		$root . "search/search.html"            => 1,
		$root . "search/index-site.html"        => 1,
		$root . "mailto.html"                   => 1,
		$root . "copyright/index.html"          => 1,
		);

my %ignore = ($root . "search/index-site.html" => 1,
	      );

foreach my $url ($sitemap->urls()) {
    if($sitemap->is_internal_url($url)) {

	next if exists $ignore{$url};

	@links = ();
	@links = $sitemap->links($url);

	my %linkcount = ();

	if(! exists $titles{$url}) {
	    $titles{$url} = $sitemap->title($url);
	}

	$graph->add_node($url);
#	$graph->add_node($titles{$url});

	next if $url =~ /org\/ports/;
	next if $url =~ /org\/doc\//;

	foreach my $link (@links) {
	    # Skip a few default links
#	    next if $link eq $root;
#	    next if $link eq $root . "index.html";

#	    if($url ne $root) {
#		$linkcount{$link}++;
#		if((exists $deflinks{$link}) && ($linkcount{$link} == 1)) {
#		    next;
#		}
#	    }

	    if(! exists $titles{$link}) {
		$titles{$link} = $sitemap->title($link)
	    }

            # Add the page's title, or it's URL?
            $graph->add_node($link);	
	    $graph->add_edge($url, $link);

#	    $graph->add_node($titles{$link});
#	    $graph->add_edge($titles{$url} => $titles{$link});
	}
    }
}

print STDERR "Generating graph...\n";

open(F, '>graph.ps'); print F $graph->as_ps(); close(F);
open(F, '>graph.png'); print F $graph->as_png(); close(F);
open(F, '>graph.dot'); print F $graph->as_canon(); close(F);
