var outputFile = 'lifestyle', RssBraider = require('rss-braider'), fs = require(
  'fs'), ejs = require('ejs'), read = require('fs').readFileSync, join = require(
  'path').join, str = read(join(__dirname, '/templates/rss.ejs'),
  'utf8'), feeds = {};

// Pull feeds from config files: ,
//      feeds.simple_test_feed = require("./config/feed").feed;
// Or define in-line
feeds.simple_test_feed = {
  'feed_name': 'feed',
  'default_count': 1,
  'no_cdata_fields': [], // Don't wrap these fields in CDATA tags
  'meta': {
    'title': 'Lifestyle',
    'description': 'Combined Lifestyle Feed',
    'site_url': 'http://pipes.silvrtree.co.uk/lifestyle.xml'
  },
  'plugins': ['filter_last_week', 'fix_images', 'fix_scripts'],
  'sources': [{'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds2.feedburner.com%2Facquire'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.askmen.com%2Fstyle.rss'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.askmen.com%2Ffine_living.rss'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.askmen.com%2Fmoney.rss'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.dmarge.com%2Ffeed'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.esquire.com%2Frss%2Fall.xml'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2Fgearpatrol'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2Fkinowear'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.mensfitness.co.uk%2Ffeeds%2Fall'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2FTheArtOfManliness'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.trunkclub.com%2Fblog%2Ffeed'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2Facontinuouslean%2F'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.fashionbeans.com%2Ffeed%2F'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2FMensFlair%3Fformat%3Dxml'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2FTheChoosyBeggar'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.valetmag.com%2Fdistribution%2Frss_all.xml'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fputthison.com%2Frss'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fdieworkwear.com%2Frss'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fmenofhabit.tumblr.com%2Frss'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.hodinkee.com%2Fblog%2Frss.xml'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2Ffirstinwatches'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds2.feedburner.com%2Fprimermagazine%2FotzU'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fchrisguillebeau.com%2F3x5%2Ffeed%2F'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2FTheAwesomer'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fbondclothes.blogspot.com%2Ffeeds%2Fposts%2Fdefault'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Faffordablebond007.tumblr.com%2Frss'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fthecozyspace.tumblr.com%2Frss'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.gentlemansgazette.com%2Ffeed%2F'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2Fwellspent'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fhespokestyle.com%2Ffeed%2F'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Feffortlessgent.com%2Ffeed%2F'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/https%3A%2F%2Ffeeds.feedburner.com%2Fthisfits'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fgeorgehahn.com%2Ffeed%2F'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.mensjournal.com%2Fservices%2Frss%2Fgear'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.mensjournal.com%2Fservices%2Frss%2Ffood-drink'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.mensjournal.com%2Fservices%2Frss%2Ftravel'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.mensjournal.com%2Fservices%2Frss%2Fadventure'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.mensjournal.com%2Fservices%2Frss%2Fhealth-fitness'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.mensjournal.com%2Fservices%2Frss%2Fstyle'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/https%3A%2F%2Ffeeds.feedburner.com%2Funcrate'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/https%3A%2F%2Ffeeds.feedburner.com%2FManOfMany'}, {
    'count': 100,
    'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Ffeeds.feedburner.com%2Fnymag%2Fvulture'
  }, {'count': 100, 'feed_url': 'http://45.33.114.116:8080/http%3A%2F%2Fwww.mantelligence.com%2Ffeed%2F'}]
};
var braider_options = {
  feeds: feeds,
  indent: '    ',
  date_sort_order: 'desc', // Newest first
  log_level: 'warn',
  dedupe_fields: ['link', 'guid'],
  plugins_directories: [__dirname + '/plugins/']
};
var rss_braider = RssBraider.createClient(braider_options);

// Override logging level (debug, info, warn, err, off)
//rss_braider.logger.level('off');

rss_braider.processFeed('simple_test_feed', 'json', function (err, data) {
  if (err) {
    return console.log(err);
  }

  var j = JSON.parse(data);
  var ejsOutput = ejs.compile(str)(j);

  fs.writeFile(__dirname + '/dist/' + outputFile + '.json',
    data,
    function (err) {
      if (err) {
        return console.log(err);
      }

      console.log('The file was saved!');
    });

  fs.writeFile(__dirname + '/dist/' + outputFile + '.html',
    ejsOutput,
    function (err) {

      if (err) {
        return console.log(err);
      }

      console.log('The file was saved!');
    });
});

/*
 rss_braider.processFeed('simple_test_feed', 'rss', function (err, data) {

 fs.writeFile(__dirname + "/html/" + outputFile + ".xml", data, function (err) {
 if (err) {
 return console.log(err);
 }

 console.log("The file was saved!");
 });
 });
 */
