#!/usr/bin/perl
#
# description here
#
######################### 
# $Id: blog2blosxom.pl 94 2003-11-14 17:55:24Z steve $
#########################
# $Log$
# Revision 1.3  2003/11/14 17:55:24  steve
# * datestamp is optional
# * whitespace conversion to either underscores, studly caps, or spaces
#
# Revision 1.2  2003/11/14 02:59:54  steve
# now creates files properly
#
# Revision 1.1  2003/11/13 22:20:23  steve
# added
#
# Revision 1.2  2003/01/24 17:18:42  steve
# version info bug
#
# Revision 1.1  2003/01/24 17:17:28  steve
# added
#
#########################

use strict;
use Getopt::Std;
use XML::Weblog::Simple;
use Time::ParseDate;

my ($version) = '$Revision: 94 $' =~ /Revision:\s*(.+)\$/;

our ( $opt_h, $opt_V, $opt_x, $opt_d, $opt_t, $opt_D );
getopts("hVx:d:t:D");

usage() if $opt_h;
usage() unless ( $opt_x && $opt_d );

die "$0 version: $version\n" if $opt_V;

#### end init

my $outdir = $opt_d;
my $wstype = "space";
my $datestamp = $opt_D;

$wstype = lc($opt_t) if $opt_t;

my %used_names = ();

my $blog = new XML::Weblog::Simple( $opt_x,
				    blog_handlers => 
				    {blosxom => \&blog_blosxom });


die "destination directory does not exist" unless -d $outdir;

$blog->set_max_posts(-1);

$blog->generate('blosxom');

sub blog_blosxom{
    my( $t, $elt ) = @_;

#    my $id = $elt->att('id');
#   $id =~ s/\D//g;
    my $rev = $elt->first_child('revhistory')->first_child('rev');
    my $title = $elt->first_child('post')->first_child('title')->text();
 #   my $author = $rev->att('author');
    my $timestamp = $rev->att('timestamp');
    my $body = $elt->first_child('post')->first_child('body')->text();

#    my $prev = $elt->att('id');

    my $name = "";
    $body =~ s|href=\"/|href=\"http://staticfree.info/|gi;

    my $utime = parsedate( $timestamp );
    $utime = time unless $utime;
    #  0    1    2     3     4    5     6     7     8
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	localtime($utime);
    $year += 1900;
    $mon ++;

    my $filename = $title;

    # remove HTMLy stuff
    $filename =~ s/\<.+?\>//g;
    $filename =~ s/\&\#?\w{1,5}\;//g;

    # remove ucky characters
    $filename =~ s/[^\w\s\-\+\']//g;

    if( $datestamp ){
	# add on the date stamp
	$filename = sprintf("%04d.%02d.%02d-$filename", $year, $mon, $mday);
    }

    if( $wstype eq "studly" ){
    # LikeThis
	$filename =~ s/\s+(.)/\U$1/g;
    }elsif( $wstype eq "underscore" ){
	$filename =~ s/\s+/_/g;
    }elsif ( $wstype eq "space" ){
	$filename =~ s/\s+/ /g;
    }else{
	print STDERR "\"$wstype\" invalid for whitespace type\n\n";
	usage();
    }

    $used_names{$filename} = 0 unless exists $used_names{$filename};
    $used_names{$filename}++;
    if( $used_names{$filename} > 1 ){
	$filename .= ".".$used_names{$filename};
    }

    $filename .= ".txt";

    $filename = $outdir."/".$filename;

    print "-" x 60, "\n";
    print "making: $filename\n";
    open (OUT, ">", $filename) or die "cannot write to $filename: $!\n";
    
#    print "Contents:\n";
#    print "$title\n";
#    print "$body\n";

    print OUT "$title\n$body\n";
    close OUT;

    print "setting file time to ".localtime( $utime )."\n";
    utime $utime, $utime, $filename;

}


#### helper subs

sub usage(){
    die <<USAGE;
usage: $0 [options]

This will convert an XML blog file into blosxom\'s simple text files.
 
options:
    -h          this help
    -V          print version information
    -d DIR      output directory
    -D          prepend filenames with a datestamp
    -t TYPE     filename whitespace type. either "studly", "underscore" or "space" def: space
    -x file.xml input blog file

Copyright(C) 2003 Steve Pomeroy <steve\@staticfree.info>
Licensed under the GNU GPL. See documentation for complete details.
USAGE

}

__END__

=head1 NAME

template - a general template for perl utilities

=head1 SYNOPSIS

B<template> S<[ B<-h> ]> I<file> S<[ I<files> ]>

=head1 DESCRIPTION

C<template>
a very general template for a perl utilitiy

=head1 OPTIONS

=over 8

=item B<-h>

This help

=back

=head1 ENVIRONMENT

No environment variables are used.

=head1 AUTHOR

Steve Pomeroy <steve@staticfree.info>
http://staticfree.info/

=head1 LICENSE

Copyright © 2003 Steve Pomeroy <steve@staticfree.info>

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

=head1 SEE ALSO

perl(1)

=head1 BUGS

It's a template. There'd better not be any.

=cut
