const configChecks = require('./configCheck.js')
const getRSS = require('../rss/rss.js')
const sqlCmds = require('../rss/sql/commands.js')
const sqlConnect = require('../rss/sql/connect.js')
const fs = require('fs')
const rssConfig = require('../config.json')

module.exports = function (bot) {
  var rssConfig = require('../config.json')

  var guildList = []
  var feedLength = 0
  var feedsProcessed = 0
  var feedsSkipped = 0

  var con

  function endCon () {
    sqlCmds.end(con, function(err) {
      console.log("RSS Info: Finished feed retrieval cycle. " + new Date())
    });
  }

  function connect () {
    //console.log("RSS Info: Starting feed retrieval cycle.")
    feedLength = feedsProcessed = feedsSkipped = 0
    guildList = []
    fs.readdir('./sources', function(err, files) {
      if (err) throw err;
      files.forEach(function(guildRSS) {
        var guild = require(`../sources/${guildRSS}`)
        guildList.push(guild)
        for (var y in guild.sources) feedLength++
      })
      if (feedLength == 0) return console.log("RSS Info: Finished feed retrieval cycle. No feeds to retrieve. " + new Date());
      else con = sqlConnect(startFeed);
    })

  }

  function startFeed () {
    for (let guildIndex in guildList) {
      let guildId = guildList[guildIndex].id
      let rssList = guildList[guildIndex].sources
      for (let rssIndex in rssList) {
        if (configChecks.checkExists(guildId, rssIndex, false) && configChecks.validChannel(bot, guildId, rssIndex) !== false) {
          getRSS(con, configChecks.validChannel(bot, guildId, rssIndex), rssIndex, false, function () {
            feedsProcessed++
            //console.log(feedsProcessed + feedsSkipped + " " + feedLength)
            if (feedsProcessed + feedsSkipped == feedLength) endCon();
          });
        }
        else feedsSkipped++;
      }
    }

    if (feedsSkipped + feedsProcessed == feedLength) endCon();
  }

  connect()
  setInterval(connect, rssConfig.refreshTimeMinutes*60000)

}

@@ -333,16 +333,46 @@
 json')%0A%0A
+  var cycleInProgress = false%0A
   var gu
@@ -601,16 +601,46 @@
 Date())%0A
+      cycleInProgress = false%0A
     %7D);%0A
@@ -664,24 +664,84 @@
 onnect () %7B%0A
+    if (cycleInProgress) return;%0A    cycleInProgress = true%0A
     //consol

add check to make sure no two retrieval cycle can occur at same time