                 queue.next()
             queue.push => groupQueue.next()
             daisy queue
-            
 
-      groupQueue.push => generateMainSitemap(groupQueue.sitemapNames)
+
+      groupQueue.push => generateSitemapIndex(groupQueue.sitemapNames)
       daisy groupQueue
 
 

module.exports = class SitemapGeneratorWorker extends EventEmitter
  constructor: (@bongo, @options = {}) ->

  generateSitemapString: (urls, isMain=false)->

    # sitemap.XML beginning and ending parts
    sitemap = '<?xml version="1.0" encoding="UTF-8"?>
      <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
      xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" 
      xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">'
    sitemapFooter = '</urlset>'

    # while generating main sitemap, we don't need hashbang in the url.
    for url in urls
      if isMain
        sitemap += "<url><loc>#{@options.uri.address}/#{url}</loc></url>"
      else
        sitemap += "<url><loc>#{@options.uri.address}/#!/#{url}</loc></url>"
    sitemap += sitemapFooter

  generateSitemapName: (groupPageNumber, skip)->
    return  "sitemap_" + groupPageNumber + "_" + skip + "_" + (skip + NAMEPERPAGE) + ".xml"

  saveSitemap: (name, content)->
    {JSitemap} = @bongo.models
    JSitemap.update {name}, $set : {content}, {upsert : yes}, (err)-> 
      console.log err if err

  generate:=>

    groupSelector = {
      privacy:'public'
    }
    generateSitemapIndex = (sitemapNames)=>
      name = "sitemap.xml"
      content = @generateSitemapIndexString sitemapNames
      @saveSitemap name, content

    JGroup.count groupSelector, (err, groupCount)=>
