 async = require "async"
 _ = require "underscore"
 {db, ObjectId, BSON} = require "./mongojs"
+fs = require "fs"
 logger = require "logger-sharelatex"
 logger.initialize("track-changes-packworker")
 if Settings.sentry?.dsn?

processUpdates = (pending) ->
	async.eachSeries pending,	(result, callback) ->
		{_id, project_id, doc_id} = result
		if not project_id? or not doc_id?
			logger.log {project_id, doc_id}, "skipping pack, missing project/doc id"
			return callback()
		PackManager.processOldPack project_id, doc_id, _id, (err, result) ->
			if err?
				logger.error {err, result}, "error in pack archive worker"
				return callback(err)

# this worker script is forked by the main process to look for
# document histories which can be archived

source = process.argv[2]
DOCUMENT_PACK_DELAY = Number(process.argv[3]) || 1000
TIMEOUT = Number(process.argv[4]) || 30*60*1000

if source.match(/[^0-9]/)
	file = fs.readFileSync source
	result = for line in file.toString().split('\n')
		[project_id, doc_id] = line.split(' ')
		{doc_id, project_id}
	pending = _.filter result, (row) -> row?.doc_id?.match(/^[a-f0-9]{24}$/)
else
	LIMIT = Number(process.argv[2]) || 1000

shutDownRequested = false
shutDownTimer = setTimeout () ->
