#!/usr/bin/perl
#
# pathetic.org's PoetryBackup to directories / text files
#
######################### 
# $Id: pathetic2text.pl 120 2004-08-17 02:20:08Z steve $
#########################
# $Log$
# Revision 1.1  2004/08/17 02:20:08  steve
# added
#
#########################

use strict;
use Getopt::Std;
use Time::ParseDate;
my ($version) = '$Revision: 120 $' =~ /Revision:\s*(.+)\$/;

our ( $opt_h, $opt_V, $opt_d );
getopts("hVd:");

usage() if $opt_h;
die "$0 version: $version\n" if $opt_V;

#### end init

my $dir = $opt_d;


my $cur_folder;
my $cur_poem;
my $cur_poem_fn;
my $cur_poem_fh;
my $cur_poem_date;
my $alternate = 0;
my $extention = "txt";

my $infh = \*STDIN;

my $infile = @ARGV[0];

if( $infile ){
    open $infh, $infile or die "cannot read from file: $infile $!\n";
}

my ($author) = <$infh> =~ /backup for\b\s+([\w\s]+)\s+on\b/i;

print STDERR "exporting the library of $author\n";

while( my $line = <$infh> ){

    if( $line =~ /\[===== (\w+) Folder "(.+)" =====\]/i ){
	if( $1 eq "Begin" ){

	    $cur_folder = $2;
	    unless( -d "$dir/$cur_folder" ){
		print STDERR "making directory '$dir/$cur_folder'...\n";
		mkdir "$dir/$cur_folder"
		    or die "cannot make folder '$dir/$cur_folder': $!\n";
	    }

	}else{
	    $cur_folder = "";
	}

    }elsif( $line =~ /\[----- (\w+) Poem "(.+)" -----\]/i ){
	if( $1 eq "Begin" ){
	    $cur_poem = $2;
	    $cur_poem_fn = "$dir/$cur_folder/$cur_poem\.$extention";

	    print STDERR "writing out '$cur_poem'...\n";
	    open $cur_poem_fh, ">$cur_poem_fn" or
		die "cannot open $cur_poem_fn for writing: $1\n";
	    $alternate = 0;
	}else{
	    close $cur_poem_fh;
	    

	    if( $cur_poem_date ){
		utime $cur_poem_date, $cur_poem_date, $cur_poem_fn 
		    or die "$!\n";
	    }
	    $cur_poem = "";
	    $cur_poem_fn = "";
	    $cur_poem_date = undef;
	}

    }elsif( $line =~ /Posted On:(.*)/ ){
	# ignore...

    }elsif( $line =~ /\Q[[[ END BACKUP ]]]\E/ ){

	# ignore..

    }elsif( $line =~ /Created On:\s*(.+)/ ){
	$cur_poem_date = parsedate($1, DATE_REQUIRED => 1, NO_RELATIVE => 1);

	print $cur_poem_fh "\n$line";

	# lines in a poem
    }else{
	if( $cur_poem && $cur_folder && $cur_poem_fh ){
	    $alternate = ($alternate + 1) % 2;
	    print $cur_poem_fh $line if $alternate == 0;

	}else{
	    print STDERR "ignoring line $.\n" if $line =~ /\S/;
	}
    }
}

#### helper subs

sub usage(){
    die <<USAGE;
usage: $0 [options] -d DIR [input_file]
 
options:
    -h          this help
    -V          print version information
    -d DIR      output directory

Turns pathetic.org\'s PoetryBackup into something useful.

Reads from STDIN or input_file, makes directories in DIR according
to the folders in input_file and writes the poems out to individual 
text files.

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

}

__END__

=head1 NAME

pathetic2text - Turns pathetic.org\'s PoetryBackup into something useful.

=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 © 2004 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

mail me

=cut
