 	util.log "starting pack operation for #{doc_id}"
 	getDocHistory doc_id, (err, docs) ->
 		return callback(err) if err?
-		origDocs = docs.length
+		origDocs = 0
+		origPacks = 0
+		for d in docs
+			if d.pack? then	origPacks++	else origDocs++
 		PackManager.convertDocsToPacks docs, (err, packs) ->
 			return callback(err) if err?
-			util.log "docs #{origDocs} packs #{packs.length}"
+			total = 0
+			for p in packs
+				total = total + p.pack.length
+			util.log "docs #{origDocs} packs #{origPacks} => packs #{packs.length} of #{total} ops"
 			if packs.length
 				if options['dry-run']
 					util.log 'dry-run, skipping write packs'

	.then () ->
		callback(null, ids)

todoFile = cli.args[1]
doneFile = cli.args[2]
util.log "reading from #{todoFile}"
util.log "logging progress to #{doneFile}"
fs.appendFileSync doneFile, '# starting pack run at ' + new Date() + '\n'

shutdownRequested = false
process.on  'SIGINT', () ->
	util.log "Gracefully shutting down from SIGINT"
	shutdownRequested = true

readFile todoFile, (err, todo) ->
	readFile doneFile, (err, done) ->
		pending = _.difference todo, done
		async.eachSeries pending,	(doc_id, callback) ->
			packDocHistory doc_id, (err, result) ->
				if err?
					console.log "ERROR:", err, result
					return callback(err)
				else if not options['dry-run']
					fs.appendFileSync doneFile, doc_id + '\n'
				if shutdownRequested
					return callback('shutdown')
				setTimeout () ->
					callback(err, result)
				, DOCUMENT_PACK_DELAY
		, (err, results) ->
			if err?
				console.log 'error:', err
			util.log 'closing db'
			db.close()

lineReader = require "line-reader"
cli = require "cli"
options = cli.parse {
	'update': ['u', 'find documents to pack from database']
	'dry-run':   ['n', 'do not write to database'],
	'fast':  [false, 'no delays on writes']
}
