 #
 
 config = require '../config'
+CP = require 'child_process'
 crypto = require 'crypto'
 echo = console.log
 FS = require 'fs'

    path = path.replace /_([A-Z])/g, '$1'
    Path.basename path, Path.extname path

# Now query all the IDs we have:
echo 'Querying content files...'
idFileNames = FS.readdir DIR_BY_ID_PATH, _
totalNumIds = idFileNames.length
echo "#{totalNumIds} files found."

# Now go through each ID,and process it, outputting percentage every minute:
echo 'Processing...'
nextLogTime = Date.now() + 1000 * 60

for idFileName, i in idFileNames
    # Skip non-content files:
    continue if (Path.extname idFileName) isnt '.json'

    id = getIdForFilePath idFileName
    idPath = getFilePathForId id  # full path, not just file name

    # Massage the data by reading it, modifying it, then writing it back.
    # NOTE: This assumes full knowledge of the starting data.
    data = require idPath   # require() auto-parses JSON!
    failed = not (data.width and data.height and data.tileSize) and
        # if we're re-running this after our conversion:
        not (data.dzi?.width and data.dzi?.height and data.dzi?.tileSize)

        remaining = totalNumIds - (i + 1)
        echo "#{percent.toFixed 2}% processed; #{remaining} files remaining."
        nextLogTime = Date.now() + 1000 * 60
, _

process.on 'exit', ->
    echo 'All done!'
