#!/usr/bin/env node

/**
 * Jit hash-object
 */
var program = require('commander'),
    fs = require('fs'),
    path = require('path'),
    Blob = require('./common/blob'),
    Repo = require('./common/repo');

program
  // TODO
  .option('-t <type>', 'object type')
  // TODO
  .option('-w', 'write the object into the object database')
  .option('--stdin', 'read the object from stdin')
  // TODO
  .option('--stdin-paths', 'read file names from stdin')
  // TODO
  .option('--no-filters store file as is without filters')
  // TODO
  .option('--path <file>', 'process file as it were from this path')
  .parse(process.argv);


content = '';

if(program.stdin) {
  process.stdin.on('data', function(chunk) {
    content += chunk;
  });
  process.stdin.on('end', function() {
    run();
  });
} else if(program.args.length > 0) {
  var file_path = path.resolve(program.args[0]);
  fs.readFile(file_path, 'utf8', function(err, data) {
    content = data;
    run();
  });
}
else {
  run();
}

function run() {
  var blob = new Blob();
  blob.hash_object({content: content}, function(err) {
    if(err) {
      console.log(err.message);
    } else {
      if(program.W) {
        var repo = new Repo();
        repo.init_from_dir(program, function(err) {
          if(err) {
            console.log(err.message);
          } else {
            repo.write_blob(blob, function(err) {
              if(err) {
                console.log(err.message);
              } else {
                console.log(blob.key);
              }
            });
          }
        });
      } else {
        console.log(blob.key);
      }
    }
  });
}

@@ -133,20 +133,67 @@
 '),%0A    
-Blob
+storage = require('./storage/filesystem'),%0A    Repo
  = requi
@@ -209,20 +209,20 @@
 mon/
-blob
+repo
 '),%0A    
 Repo
@@ -209,36 +209,36 @@
 mon/repo'),%0A    
-Repo
+Blob
  = require('./co
@@ -242,20 +242,20 @@
 /common/
-repo
+blob
 ');%0A%0Apro
@@ -670,16 +670,20 @@
 rgv);%0A%0A%0A
+var 
 content 
@@ -686,16 +686,58 @@
 ent = ''
+,%0A    type = program.T ? program.T: 'blob'
 ;%0A%0Aif(pr
@@ -1079,16 +1079,18 @@
 n();%0A%7D%0A%0A
+%0A%0A
 function
@@ -1102,151 +1102,8 @@
 ) %7B%0A
-  var blob = new Blob();%0A  blob.hash_object(%7Bcontent: content%7D, function(err) %7B%0A    if(err) %7B%0A      console.log(err.message);%0A    %7D else %7B%0A    
   if
@@ -1112,28 +1112,24 @@
 rogram.W) %7B%0A
-    
     var repo
@@ -1140,27 +1140,38 @@
 ew Repo(
+'.git', storage
 );%0A    
-  
-  
 repo.ini
@@ -1175,27 +1175,9 @@
 init
-_from_dir(program, 
+(
 func
@@ -1198,111 +1198,42 @@
     
-    if(err) %7B%0A            console.log(err.message);%0A          %7D else %7B%0A            repo.write_blob(blob
+repo.write_blob(%7Bcontent: content%7D
 , fu
@@ -1238,34 +1238,34 @@
 function(err
-) %7B%0A      
+, blob) %7B%0A
         if(e
@@ -1272,38 +1272,32 @@
 rr) %7B%0A          
-  
-    
 console.log(err.
@@ -1314,22 +1314,16 @@
         
-      
 %7D else %7B
@@ -1329,30 +1329,24 @@
 %7B%0A          
-   
-   
 console.log(
@@ -1368,36 +1368,24 @@
     
-      %7D%0A   
+%7D%0A
       
-   
 %7D);%0A    
     
@@ -1384,47 +1384,63 @@
     
-      %7D%0A        %7D);%0A      %7D else %7B%0A    
+%7D);%0A  %7D else %7B%0A    var blob = new Blob(content, type);%0A
     
@@ -1468,24 +1468,8 @@
 ;%0A  
-    %7D%0A    %7D%0A  %7D);
+%7D
 %0A%7D%0A

Refactor jit-hash-object to reflect new repo/blob api