#!/usr/bin/env node

var minimist = require('minimist')
var args = [], bare = []
for (var i = 2; i < process.argv.length; i++) {
  if (/^-\d/.test(process.argv[i])) bare.push(process.argv[i])
  else args.push(process.argv[i])
}
var argv = minimist(args.concat('--',bare), {
  alias: { h: 'help' }
})

var spawn = require('child_process').spawn
var through = require('through2')
var fs = require('fs')
var path = require('path')
var dir = '/home/substack/data/osmtiles'
var peermaps = require('../')({
  list: function (p, cb) {
    fs.readdir(path.join(dir,p), cb)
  },
  createReadStream: function (file) {
    return fs.createReadStream(path.join(dir,file))
  }
})
process.stdout.on('error', function () {})

if (argv.help || argv._[0] === 'help') {
  usage(0)
} else if (argv._[0] === 'data') {
  var wsen = argv._[1].split(',').map(Number)
  var nprocs = 4
  peermaps.files(wsen, function (err, files) {
    files = files.map(function (x) { return path.join(dir,x.file) })
    spawn('osmconvert', files, { stdio: 'inherit' })
  })
} else if (argv._[0] === 'files') {
  var wsen = argv._[1].split(',').map(Number)
  peermaps.files(wsen).pipe(through.obj(function (row, enc, next) {
    next(null, row.file + '\n')
  })).pipe(process.stdout)
} else usage(1)

function usage (code) {
  var r = fs.createReadStream(path.join(__dirname, 'usage.txt'))
  r.pipe(process.stdout)
  if (code) r.once('end', function () { process.exit(code) })
}

function error (err) {
  console.error(err)
  process.exit(1)
}

@@ -905,20 +905,24 @@
 ) %7B%0A    
-file
+var oarg
 s = file
@@ -974,16 +974,54 @@
 ile) %7D)%0A
+      .concat('-b=' + wsen.join(','))%0A
     spaw
@@ -1036,20 +1036,20 @@
 nvert', 
-file
+oarg
 s, %7B std

apply bounding box to data stream