#!/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;

MACRO category(entry) BLOCK;
 "Category : " _ entry.category;
END;

MACRO author(entry) BLOCK;
 "Author : " _ entry.author;
END;

MACRO comment_form(entry) BLOCK;
  'Post a new comment!
  <DIV class="comment">
  <FORM METHOD="POST">
  Your name: <INPUT NAME="author"><BR>
  Your address/home page: <INPUT NAME="url"><BR>
  <HR WIDTH="98%">
  Your rant:<BR>
  <textarea NAME="content" COLS=80 ROWS=7></textarea>
  <BR>
  <INPUT TYPE="submit" VALUE="Vent">
  <INPUT TYPE="hidden" NAME="id" VALUE="'; entry.id;
  '"><INPUT TYPE="hidden" NAME="newcomment" VALUE="1">
  
  </FORM>
  </DIV>
  ';

END;

MACRO permalink(entry, params) BLOCK;
 '<a href="';
 bryar.config.baseurl;
 '/id_';
 entry.id;
 "?" _ params IF params;
 '">';
END;

MACRO comments(entry) BLOCK;
 permalink(entry, "comments=1");
 entry.comments.size || 0;
 " Comments</A>";
 IF bryar.arguments.comments;
     "</P>";

     FOREACH comment = entry.comments; 
     '<DIV CLASS="comment">';
     comment.content;
     '<BR><HR COLOR="#AAAAAA" WIDTH=98%>';
     '<I>Posted by <A HREF="'; comment.url; '">'; comment.author;
     '</A> at ';
     day(comment.timepiece);
     ' '; 
     comment.timepiece.time;
     '</I></DIV>';
     '<BR>';
     END;
     comment_form(entry);
     "<P>";
 END;
END; 

%]

[%

MACRO google_link(query, label) BLOCK;
 '<a href="http://www.google.com/search?q=';
 query | html | uri;
 '">'; 
 label; 
 "</a>";
END;

MACRO keywords(entry) BLOCK;
    "Keywords :";
    FOREACH key = entry.keywords.slice(0,2);
       '[ ';
       google_link(key, key);
       '] ';
    END;
END;
%]

[% INCLUDE head.html %]

[% INCLUDE calendar %]

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

    <p class="blosxomEntry"><a name="[%entry.id%]">
    <span class="blosxomTitle"><b>[%entry.title%]</b></span>
    </a><br/>
    <span class="blosxomBody">
    [% entry.content %]
</span><br />

    <span class="blosxomTime">Posted at [%entry_time.time%]</span> 
    
    | 
     [% permalink(entry, "") %] #</a>
    | 
     [% google_link(entry.title, "G") %]
    |
    [% comments(entry) %]

</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>

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

write_file("foot.html", <<EOC);
<HR> <I> Powered by Bryar! </I> <FORM>Search: <INPUT name="search"></FORM>
</body></HTML>
EOC

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

write_file("template.atom", <<EOC);
<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
  <title>[% bryar.config.name %]</title>
  <link rel="alternate" type="text/html"
   href="[% bryar.config.baseurl %]"/>
  <modified>[%documents.first.timepiece.datetime%][% FILTER format("%03d") %][% documents.first.timepiece.tzoffset / 3600 %][%END%]:00</modified>
  <author>
    <name>[% bryar.config.author %]</name>
  </author>
[% FOREACH item = documents %]
  <entry>
    <title>[% item.title %]</title>
    <link rel="alternate" type="text/html"
     href="[%bryar.config.baseurl _ "/id_" _ item.id %]"/>
    [% idURL = bryar.config.baseurl | replace('http://', '') | replace('/.*', '') %]
    <id>tag:[% idURL _ "," _ item.timepiece.year _":id_" _ item.id %]</id>
    [% hours = item.timepiece.tzoffset / 3600 %]
    [% minutes = (item.timepiece.tzoffset % (hours * 60 * 60)) / 60 %]
    <issued>[% item.timepiece.datetime %][% FILTER format("%03d") %][% hours  %][%END%]:[% FILTER format("%02d") %][% minutes %][%END%]</issued>
    <modified>[% item.timepiece.datetime %][% FILTER format("%03d") %][% hours  %][%END%]:[% FILTER format("%02d") %][% minutes %][%END%]</modified>
    <summary type="text/plain"> [% FILTER truncate(252) %]
            [%  item.content | replace('<.*?>', '') %]
        [% END %]
    </summary>
    <content type="text/html" mode="escaped" xml:lang="en" xml:base="[% bryar.config.baseurl %]">
      <![CDATA[ [% item.content %] ]]>
    </content>
  </entry>
[% END %]
</feed>
EOC

write_file("calendar", <<EOC);
<table class="calendar">
<thead>
<tr class="days"><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td
>Fri</td><td>Sat</td></tr>
</thead>
<tbody>
[% FOREACH week = bryar.posts_calendar %]
  <tr>
  [% FOREACH day = week %]
    <td[% IF day.sunday %] class="sunday"[% END %]>
    [% '<a href="' _ bryar.config.baseurl _ '/' _ day.link _ '">' IF day.link%]
    [% day.day || '&nbsp;' %][% '</a>' IF day.link %]</td>
  [% END %]
  </tr>
[% END %]
</tbody>
</table>
EOC

chmod 0644, $_ for ("bryar.conf", "1.txt", "head.html", "foot.html",
                    "template.html","template.rss", "template.atom", "calendar");
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";
