#!/usr/bin/env node

'use strict';

var fs = require('fs');
var path = require('path');

var program = require('commander');
var unzip = require('unzip');

var checkUsage = require('./lib/checkUsage');
var esLoader = require('./lib/esLoader');
var ogr = require('./lib/ogr');
var splitOGRJSON = require('./lib/splitOGRJSON');
var transformer;


program
  .version('1.0.0')
  .option('-s, --shapefile <s>', 'Shapefile')
  .option('-h, --host <h>', 'ElasticSearch host')
  .option('-p, --port <p>', 'ElasticSearch port', parseInt)
  .option('-t, --transformer <t>', 'Data transformer')
  .parse(process.argv);


if(program.transformer){
  transformer = require('./transformers/' + program.transformer);
}else{
  transformer = require('./transformers/default');
}


if(!checkUsage(program)) return;


esLoader.connect(program.host, program.port);


var shapefile = program.shapefile;
var ext = path.extname(shapefile);
var basename = path.basename(shapefile, ext);
var dirname = path.dirname(shapefile);


//fast dir check... requires passing ext with shapefile
if(!ext){
  dirname = shapefile;
}


if(ext.toLowerCase() === '.zip'){
  dirname = path.join(dirname, basename);

  fs.mkdir(dirname, function(err){
    if(err) throw err;
    fs.createReadStream(shapefile).pipe(unzip.Extract({path: dirname}))
      .on('close', processShapefile);
  });
}else{
  processShapefile(); 
}


function processShapefile(){
  var shp = path.join(dirname, basename + '.shp');

  ogr(shp).pipe(splitOGRJSON()).pipe(transformer()).pipe(esLoader.load()).on('error',function(err){
    console.log("Error loading data",err); 
  });
}


@@ -1232,44 +1232,23 @@
     
-fs.createReadStream(shapefile).pipe(
+var unzipped = 
 unzi
@@ -1277,16 +1277,22 @@
 me%7D)
-)
+;
 %0A    
-  
+unzipped
 .on(
@@ -1318,16 +1318,67 @@
 pefile);
+%0A%0A    fs.createReadStream(shapefile).pipe(unzipped)
 %0A  %7D);%0A%7D
@@ -1490,16 +1490,68 @@
 .shp');%0A
+  console.log(%22Streaming %25s to elasticsearch.%22,shp);
 %0A  ogr(s

Fix unzip bug