#!/usr/bin/perl

use lib "../src";
use Siikir;

my $cms = Siikir->new (
	root => "..",
);
$cms->loadPlugin("JsonDB");
$cms->loadPlugin("Blog");

my $dir = shift(@ARGV) or die "Usage: $0 <path to blog entries>";
opendir (DIR, $dir);
foreach my $post (sort(grep(/\.txt$/i, readdir(DIR)))) {
	print "post: $post\n";
	my $id = $post;
	$id =~ s/\.txt$//;

	my %headers = ();
	my $inHead = 1;
	my @body = ();
	open (READ, "$dir/$post");
	while (my $line = <READ>) {
		chomp $line;
		$line =~ s/[\x0D\x0A]//g;
		if (length $line == 0) {
			$inHead = 0;
		}
		if ($inHead) {
			my ($what,$is) = split(/:\s+/, $line, 2);
			$what = lc($what);
			$headers{$what} = $is;
		}
		else {
			push (@body, $line);
		}
	}

	# Add it.
	$cms->Blog->postEntry(1,
		id => $id,
		time => $headers{time},
		categories => [ split(/\s*\,\s*/, $headers{categories}) ],
		comments   => ($headers{noreply} ? 0 : 1),
		emoticons  => ($headers{noemote} ? 0 : 1),
		avatar     => $headers{avatar},
		privacy    => $headers{privacy},
		subject    => $headers{subject},
		body       => join("\n",@body),
		ip         => $headers{ip},
	);	
}
closedir (DIR);
