 config = require './config'
 fs = require 'fs'
 Stream = require 'stream'
+async = require 'async'
 
 escapeXmlValue = (str) ->
   str.replace(/&/g, '&amp;')

  notifyWhenDone: (cb) ->
    sitemapThis = this
    process.nextTick =>
      if sitemapThis.flushed && sitemapThis.fileFlushed
        console.log "!! sitemap done #{sitemapThis.fileName}, #{sitemapThis.urlCount} urls"
        cb(null, true)
      else
        sitemapThis.gzipper.on 'end', =>
          sitemapThis.file.on 'end', =>
            console.log "!! sitemap done #{sitemapThis.fileName}, #{sitemapThis.urlCount} urls"
            cb(null, true)

  close: ->
    return if @closed


    @gzipper.on 'end', =>
      sitemapThis.flushed = true
    @file.on 'close', =>
      sitemapThis.fileFlushed = true
    @stream.pipe(@gzipper).pipe(@file)
    @stream.emit 'data', config.sitemapHeader
