#!/usr/bin/env ruby

# Add the path to your feedcache directory here
FEEDCACHE_DIR = '/path/to/your/wordpress/wp-content/plugins/feedcache'
# How many characters from each feed item do you want to display
CHAR_COUNT = 75
# Set to 'true' if you want to receive error emails from the CRON job
CRON_EMAILS = false


#################################
#                               #
#  DO NOT EDIT BELOW THIS LINE  #
#                               #
#################################
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))

require 'net/http'
require 'lib/feedparser'
require 'uri'
require 'yaml'
require 'tempfile'

CONFIG_FILES, CACHE_FILES = [], []
1.upto(4) do |i|
  CONFIG_FILES << "#{FEEDCACHE_DIR}/feedcache-config#{i}.txt"
  CACHE_FILES <<   "#{FEEDCACHE_DIR}/feedcache-cache#{i}.txt"
end

# RSS formatting function
def shorten_text(txt)
  @text = txt + ' '
  @text = @text.slice(0,CHAR_COUNT)
  # need to break on the last space
  # @text = 
  @text = @text + '...' if @text.size > CHAR_COUNT
  return @text
end

begin # read the config file settings
  @all_feeds = {}
  CONFIG_FILES.each_with_index do |cfg_file, idx|
    config = File.open(cfg_file, 'r') do |f|
      @feeds = []
      @params = f.gets.split('~')
      while line = f.gets
        @feeds << line.strip
      end
    end
    @all_feeds[idx] = @feeds
  end
rescue => e
  if CRON_EMAILS
    puts "Error reading configuration file"
    puts YAML.dump(e)
  end
end  

# parse the parameters from the config file
@display_num, @title_pre, @title_post, @format_text = @params[0].strip, @params[1].strip, @params[2].strip, @params[3].strip

@all_feeds.each do |k,v|
  @tmp = Tempfile.new("feedcache#{k}")
  @processed = 0

  # parse the feeds here
  v.each do |feed|
    #puts "\nFEED -> #{feed}\n"
    @html_text = ''
    data = feed.split('|')
    feed_url, feed_title, feed_num, feed_format = data[0], data[1], data[2], data[3]
    begin
      source = Net::HTTP::get URI::parse(feed_url)
      fp = FeedParser::Feed::new(source)
        @html_text << @title_pre + (feed_title || fp.title) + @title_post
        @html_text << "<ul>"
        fp.items.each_with_index do |item, idx|
          break if feed_num ? feed_num.to_i == idx.to_i : @display_num.to_i == idx.to_i
          output = ''
          output << "<li><a href='#{item.link}'>"
          if feed_format && feed_format == 'true'
            txt = "#{item.title.downcase.gsub(/^[a-z]|\s+[a-z]/) {|a| a.upcase}}"
            output << shorten_text(txt)
          elsif @format_text == 'true'
            txt = "#{item.title.downcase.gsub(/^[a-z]|\s+[a-z]/) {|a| a.upcase}}"
            output << shorten_text(txt)
          else
            output << "#{item.title}"
          end
          output << "</a></li>\n"
          @html_text << output
        end # end fp.items.each
        @html_text << "</ul><br />\n"
        @tmp << @html_text
        @processed += 1
    rescue => e
      puts "Error processing feed - Group #{k.to_i + 1} - #{feed_url}"
      puts YAML.dump(e)
    end  
  end

  @tmp.close
  # if we had new feeds, move them to the cache file
  if @processed > 0
    @tmp.open
    @cache = File::open(CACHE_FILES[k], "w")
    @cache << @tmp.gets(nil)
    @cache.close
    @tmp.close(true) # remove the /tmp file
  end
end #--> @all_feeds.each do |k,v|