#!/usr/bin/perl
use warnings;
use strict;

use File::Basename qw(dirname);
use File::Path qw(mkpath);
use YAML;
use IO::All;

# allow passing of -y and -n 
my ( $always_yes, $always_no );
if ( defined $ARGV[0] ) {
    if ( $ARGV[0] =~ /^--?y(es)?$/i ) {
        $always_yes = 1;
    }
    elsif ( $ARGV[0] =~ /^--?no?$/i ) {
        $always_no = 1;
    }
    else {
        print STDERR <<END;
usage: $0 [ -y | -n ]
-y will always overwrite files, -n will ignore existing files.
END
    }
}

# extract the files
print "Extracting SoggyOnion default files..\n";
my $data = Load(join('',<DATA>));
foreach my $path ( sort keys %$data ) {
    mkpath( dirname($path), 0, 0711 );
    if ( -f $path && not $always_yes ) {
        next if $always_no;
        my $response = '';
        while ( $response !~ /^(y|n)/i ) {
            print "$path exists. overwrite? [Y/n] ";
            $response = <STDIN>;
            $response = 'y' unless $response =~ /\S/;
        }
        next if $response =~ /^n/i;
    }

    # customize the configuration
    if ( $path eq 'config.yaml' ) {
        #print "Customizing $path\n";
        chomp(my $pwd = `pwd`);
        for ($data->{$path}) {
            s/^(\s+templatedir:\s+).+$/$1'$pwd\/templates'/m;
            s/^(\s+outputdir:\s+).+$/$1'$ENV{HOME}\/public_html'/m;
            s/^(\s+cachedir:\s+).+$/$1'\/tmp\/$ENV{USER}-cachedir'/m;
        }
    }

    #print "Writing $path\n";
    io($path)->print( $data->{$path} );
}

# instructions
print <<END;

Congratulations! Here's all that's left to do:
    
    1. Edit config.yaml
    2. Run: soggyonion /path/to/config.yaml
    3. Add the command you ran in #2 as a twice-a-day (or so) cron job
    4. Point your browser at the directory you specified as outputdir
       in the configuration

END


__DATA__
---
config.yaml: |+
  --- #YAML:1.0
  ##
  ## This is an example SoggyOnion configuration, which is
  ## formatted as YAML. Use it like:
  ##
  ##    $ soggyonion /path/to/this/config.yaml
  ##
  
  ################################################################
  # OPTIONS
  #
  # These are general options to control the general functionality
  # of soggyonion.
  
  options:
  
    # where to look for template files
    templatedir: '/home/bass/soggyonion/dist/defaults/templates'
  
    # where to put processed (output) files
    outputdir: '/home/bass/soggyonion/htdocs/'
  
    # where to store our cache files
    cachedir: '/tmp/soggycache'
  
  ################################################################
  # LAYOUT OF OUTPUT
  #
  # These parameters define what pages to create and what items
  # to include in them. Items in layout are hashes with the
  # following keys:
  #
  #     title - title of the processed page
  #     name  - filename of template in templatedir
  #     items - array of plugins or sources
  #
  layout:
  
    - title: Perl and O'Reilly
      name: perl.html
      items:
        - rss: http://search.cpan.org/recent.rdf
          id: cpan
        - rss: http://use.perl.org/search.pl?op=journals&content_type=rss
          id: journals
        - rss: http://www.perlmonks.org/headlines.rdf
          icon: http://perlmonks.org/images/perlmonkssm.gif
          id: perlmonks
        - rss: http://use.perl.org/useperl.rss
          id: useperl
        - rss: http://www.perl.com/pace/perlnews.rdf
          icon: http://perl.com/images/75-logo.jpg
          id: perldotcom
          limit: 10
        - rss: http://www.theperlreview.com/rss/tpr-all.rdf
          id: perlreview
          limit: 15
  
    - title: Open-Source and More
      name: opensource.html
      items:
        - rss: http://www.freshmeat.net/backend/fm-releases.rdf
          id: freshmeat
        - rss: http://www.osnews.com/files/recent.rdf
          icon: http://img.osnews.com/images/osn.gif
          id: osnews
        - rss: http://linuxtoday.com/backend/biglt.rss
          id: linuxtoday
          html: no
        - rss: http://www.debianplanet.com/module.php?mod=node&op=feed
          icon: http://www.debian.org/logos/openlogo-50.png
          id: debianplanet
          descriptions: no
        - rss: http://www.oreillynet.com/meerkat/?_fl=rss10&t=ALL&c=916
          id: oreilly
          limit: 10
        - rss: http://slashdot.org/index.rss
          id: slashdot
  
    - title: Weather
      name: weather.html
      items:
        - plugin: SoggyOnion::Plugin::GeoWeather
          id: weatherdotcom
          zip: '02115'
          #city: Boston
          #state: MA
  
    - title: PhotoBlogs
      name: pb.html
      items:
        - images: http://www.chromasia.com/iblog/
          id: chromasia
        - images: http://www.hchamp.com/
          id: hchamp
        - images: http://www.joecunningham.com/
          id: joecunningham
        - images: http://www.ferociouscheese.com/
          id: ferociouscheese
  #      - images: http://sensitivelight.com/blog/
  #        id: sensitivelight
  #        limit: 3
  
    - title: Miscellaneous
      name: misc.html
      items:
        - rss: http://del.icio.us/popular/rss
          id: delicious
          descriptions: no
        - rss: http://boingboing.net/rss.xml
          icon: http://boingboing.net/dimages/bbhead.gif
          id: boingboing
          html: no
          maxlen: 150
          limit: 15
        - rss: http://realbeer.com/rdf/realbeernews.rdf
          id: realbeer
  
  #  - title: Other Blogs
  #    name: blogs.html
  #    items:
  
    - title: TEMP
      name: temp.html
      items:
        - images: http://www.google.com/
          id: goog
        - images: http://www.slashdot.org/
          id: slash
  

templates/imagescraper.tt2: |+
  <h2><a href="[% src %]">[% title %]</a></h2>
  <p>
  [% FOREACH link = links %]
      <a href="[% src %]">
      <img src="[% GET link FILTER uri %]" alt="(scraped image)" border="0"/>
      </a>
      <br/>
  [% END %]
  </p>

templates/main.tt2: |
  <?xml version="1.0"?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  
  <!--
       SOGGYONION EXAMPLE WRAPPER
  
       This is an example file that will be a wrapper for all
       processed output. This makes use of Template Toolkit, which
       you can find more about by reading the docs for Template.pm
       or by visiting http://www.template-toolkit.org/
  -->
  
  <head>
      <title>SoggyOnion - [% thispage.title %]</title>
  
      <link rel="SHORTCUT ICON" href="http://soggyonion.com/favicon.ico"/>
      <meta http-equiv="refresh" content="1800; url=[% thispage.name %]"/>
  
      <style type="text/css">
          body {
              font-family: sans-serif;
              margin: 0em;
          }
          #menu {
              border-bottom: 1px solid #060;
              padding: 0.3em; margin: 0em;
          }
          #menu, #menu a {
              color: #060; background: #9f9;
          }
          #menu a {
              padding: 0.1em;
          }
          #menu a.selected {
              color: #9f9; background: #393;
          }
          #body {
              margin: 1em;
          }
          #body small {
              font-size: 60%;
              font-weight: normal;
              color: #999; background: inherit;
          }
          #disclaimer {
              border-top: 1px solid #999;
              padding: 0.1em; margin: 0em;
              font-size: x-small;
          }
          #disclaimer, #disclaimer a {
              color: #999; background: inherit;
          }
      </style>
  </head>
  <body>
  
  <!-- a menu is created by iterating over the given 'pages' array -->
  <div id="menu">
      <strong>
        <a href="http://soggyonion.com/">SoggyOnion</a>
      </strong> |
      [% FOREACH page = allpages %]
          <a href="[% page.name %]" 
              [% IF thispage.name == page.name %]
                  class="selected"
              [% END %]
          >[% page.title %]</a> |
      [% END %]
  </div>
  
  
  <!-- content is created by processing the items for each page as
       defined in your config.yaml. every item is automatically
       placed in its own <div> whose id is the plugin name/key -->
  <div id="body">
      [% content %]
  </div>
  
  <div id="disclaimer">
  Please note that none of the content of this site is the property of SoggyOnion.com other than the <a href="http://soggyonion.com/logo.png">SoggyOnion logo</a>. The content present on this site has been collected from various sources, which are clearly labeled and hyperlinked above each section of content. This site is completely non-profit and non-commerical. Please email <tt>ian</tt> at this domain with questions or comments.
  </div>
  
  </body>
  </html>
templates/rss.tt2: |+
  <h2>
      [% IF image.url %]
          <a href="[% image.link %]">
              <img src="[% image.url %]" align="middle" 
               alt="[% channel.description %]" border="0">
          </a><br/>
      [% ELSE %]
          <a href="[% channel.link %]">
              [% channel.title %]
          </a><br/>
      [% END %]
      <small>[% channel.description %]</small>
  </h2>
  
  <ul>
  [% FOREACH item = items %]
      <li>
          <a href="[% item.link %]" 
            title="[% item.stripped_description %]">
              [% item.title %]
          </a>
          [% IF allow_html %]
              [% IF show_descriptions && item.description %]
                  - [% item.description %]
              [% END %]
          [% ELSE %]
              [% IF show_descriptions && item.stripped_description %]
                  - [% item.stripped_description %]
              [% END %]
          [% END %]
      </li>
  [% END %]
  </ul>

