#!/usr/bin/perl
use Cwd;
use Config;

print "Setting up a Bryar blog in this directory\n\n";

sub write_file {
    my ($name, $content) = @_;
    open OUT, ">".$name or die "Couldn't write to $name - $!\n";
    print OUT $content;
    close OUT;
}

# These are defaults which are written out to be customized, so I don't
# feel bad about including them here inline.

# Blatant assumption of standard Unix
write_file("bryar.cgi", "#!/usr/bin/perl\nuse Bryar; Bryar->go()\n");
chmod 0755, "bryar.cgi";

write_file("bryar.conf",<<EOC);
name : My Bryar Weblog!
description : A blog without a more useful description
baseurl : http://where.will.i.be/
datadir : @{[ cwd() ]}
EOC

write_file("1.txt", <<EOC);
My first blog post!

<P>First post! First post!</P>
EOC

write_file("template.html", <<'EOC');
[% MACRO day(entry_time) BLOCK;
    entry_time.day _ ", " _ entry_time.mday _ " " _
    entry_time.month _ " " _ entry_time.year;
END;
%]

[% INCLUDE head.html %]

[% FOREACH entry = documents;
    entry_time = entry.timepiece;
    IF entry == documents.first or entry_time.ymd != previous.entry_timepiece.ymd %]
    <h1> [% day(entry_time) %] </h1>
    [% END %]
    [% previous = entry %]

    <a name="[% entry.id %]">
        <h2><b>[% entry.title %]</b></h2>
    </a>
    
    <br/>

    [% entry.content %]

    <P>Posted at [%entry_time.time%]
    <a href="[%bryar.config.baseurl%]/id_[%entry.id%]">#</a> 
    <a href="http://www.google.com/search?q=[%entry.title | html | uri%]">G</a> 
 </p>
[% END %]

[% INCLUDE foot.html %]
EOC

write_file("head.html", <<EOC);
<html>

<head>
<title>[% bryar.config.name %]</title>
<link rel="alternate" type="application/rss+xml" title="RSS" href="$url/xml" />
</head>

<H1> [% bryar.config.name %]
<body bgcolor="#ffffff" text="#222222">
EOC

write_file("foot.html", "<HR><P>Powered by Bryar!</P>\n</body>\n</HTML>\n");

write_file("template.rss", <<EOC);
<?xml version="1.0"?>
<!-- name="generator" content="bryar" -->
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:admin="http://webns.net/mvcb/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns="http://purl.org/rss/1.0/">

  <channel rdf:about="[%bryar.config.baseurl%]">
    <title>[% bryar.config.name %]</title>
    <link>[% bryar.config.baseurl %]</link>
    <description>[% bryar.config.description %]</description>
    <dc:language>en-us</dc:language>
    <dc:date>[%documents.first.timepiece.datetime%]</dc:date>
    <items>
      <rdf:Seq>
      [% FOREACH item=documents %]
      <rdf:li rdf:resource="[%bryar.config.baseurl _ "/id_" _ item.id %]"/>
      [% END %]
      </rdf:Seq>
    </items>
  </channel>

[% FOREACH item = documents %]
  <item rdf:about="[%bryar.config.baseurl _ "/id_" _ item.id %]">
    <title> [% item.title %] </title>
    <link> [%bryar.config.baseurl%]/id_[%item.id%]</link>
    <description>
[% item.content | html %]
</description>
    <dc:date> [%item.timepiece.datetime %] </dc:date>
    <dc:creator> [%item.author %] </dc:creator>
  </item>
[% END %]
</rdf:RDF>
EOC

chmod 0644, $_ for ("bryar.conf", "1.txt", "head.html", "foot.html",
                    "template.html","template.rss");
print "\nDone. Now you want to probably customize 'bryar.conf'.\n";
print "You should probably also customize template.html, head.html and foot.html\n";
print "Then point your browser at bryar.cgi, and get blogging!\n";
