#!/usr/bin/perl -w
use strict;
use warnings;
use File::Find::Rule;
use File::Spec;
use IO::AtomicFile;
use HTML::TagCloud;

# This script will walk the given directory of blosxom blog posts and build
# up a page footer suitable for inclusion in a blosxom powered blog. It
# can also read in a static file and include that at the bottom of the
# file.
#
# License: GPL
# Application home page: 
# Date: 2005/08/20
# Version 0.1
# Author: Dean Wilson (www.unixdaemon.net)


##################################################
# script configuration - only edit this section
##################################################

# this is the file extension you use for blosxom entries
my $blogglob = '*.blox';

# this is the base directory that contains all your blog posts
my $blogdata = '/www/www.ud.net/static/htdocs/blosxomdata';

# to include static text in the footer put it in the file given below
my $blosxom_footer = '/www/www.ud.net/static/htdocs/blosxomdata/foot.base';

# this is script created footer file that blosxom should use
my $blosxom_foot_output = '/www/www.ud.net/static/htdocs/blosxomdata/foot.html';

# this is the url that your categories live under. add a trailing slash
my $blogbaseurl = 'http://blog.unixdaemon.net/cgi-bin/blosxom.pl/';

# you may need to make the output footer file group or world readable.
# define the desired umask here.
my $footer_perms = '0666';

##################################################


my %categories;

# this what makes the HTML for us.
my $cloud = HTML::TagCloud->new;

#get the blog postings
my @files = File::Find::Rule->file()
                            ->name( $blogglob )
                            ->relative
                            ->in( $blogdata );

# make this use the File::Find::Rule call back interface?
for my $post (@files) {
  my ($volume,$category,$file) = File::Spec->splitpath( $post );
  $category =~ s!/$!!; # remove the last slash for pretty output
  $categories{$category}++;
}

foreach my $category (keys %categories) {
  $cloud->add($category, "$blogbaseurl$category", $categories{$category});
}

my $taghtml = $cloud->html_and_css(35);

my $footer_html = get_footer($blosxom_footer);
write_footer($blosxom_foot_output, $footer_perms, $footer_html, $taghtml);

##################################################
#                  subroutines
##################################################

sub get_footer {
  # read in the footer from a file

  my $blosxom_footer = shift;
  my ($footer_file, $footer_content);

  open($footer_file, "<$blosxom_footer")
    || die "Failed to open the footer file '$blosxom_footer'\n$!\n";

  local $/ = undef;
  $footer_content = <$footer_file>;

  close $footer_file;

  return $footer_content;
}

#---------------------#

sub write_footer {
  # write the new, combined, footer out to the file system

  my $blosxom_foot_output = shift;
  my $footer_perms = shift;
  my $footer_html = shift;
  my $tag_html = shift;

  # allow the web server to see the file.
  umask $footer_perms;

  my $footer_fh = IO::AtomicFile->open($blosxom_foot_output, "w");

  print $footer_fh $tag_html, "\n";
  print $footer_fh $footer_html, "\n";

  $footer_fh->close
    || die "Failed to create atomic file '$blosxom_foot_output'\n$!\n";

  return 0;
}
