#!/usr/bin/env node

var address = require('network-address')
var ms = require('ms')
var home = require('osenv').home
var name = process.env.alligator_appname || 'alligator'
var path = require('path')
var Peer = require('./')
var muxrpcli = require("muxrpcli")
var mkdirp = require("mkdirp")
var mdm = require('mdmanifest')
var fs   = require('fs')
var path = require('path')
var docs  = path.dirname(fs.realpathSync(__filename))
var commands =fs.readFileSync(path.join(docs,'bin.md')).toString()
var valid = require('muxrpc-validation')()
var network = require('icebreaker-network')

var api = {
  usage: valid.async(function(command,cb){ cb(null,mdm.usage(commands,command))  }, ['string?']),
  start: valid.async(function(target,opts,cb){
    if (typeof target == 'function') cb = target, target = null
    if (typeof opts == 'function')   cb = opts, opts = null
    if (typeof target == 'object') opts = target, target = null
    
    var config = require('rc')(name, {
      listen: [
        'shs+tcp://[' + address.ipv6() + ']:4239',
        'shs+ws://[' + address.ipv6() + ']:4238',
      ], path: path.join(home(), '.' + name)
    })

    config.dht = {}
    mkdirp.sync(config.path)
    config.info = require('./lib/peerInfo.js').loadOrCreateSync(path.join(config.path, 'peerInfo'))
    if(opts && opts.b)config.bootstrap = opts.b.split(",")
    
    
    var peer = new Peer(config)
    if(!target && fs.existsSync("./package.json")){
      target="."
    }

    if(target){
      var main = require(path.join(path.resolve(target),"/package.json")).main
      peer.logger.log("Plugin loaded",path.join(path.resolve(target),"/"+main)  )
      peer.addProtocol(require(path.join(path.resolve(target),"/"+main)  ))
    }
   
    

    peer.start()

    process.on("SIGINT", peer.stop.bind(peer))
    process.on("SIGHUP", peer.stop.bind(peer))
    process.on('SIGUSR2', peer.stop.bind(peer, peer.start.bind(peer)))
    
  },['string?'],['object?'],['string', 'object']),
  init:valid.async(function(cb){
    if(!fs.existsSync("./package.json")){
      var p = require(path.join(docs,'/template/package.json'))
      p.dependencies.alligator=require('./package.json').version
      
      p.name = path.basename(process.cwd())
      fs.writeFileSync('./package.json',JSON.stringify(p,null,2))
      console.log('package.json created.')
    }
    else console.warn('package.json already exists.')

    if(!fs.existsSync('./index.js')){
    
      fs.writeFileSync('./index.js',fs.readFileSync(path.join(docs,'/template/index.js'),'utf8'))
      console.log('index.js created.')
    }
    else console.warn('index.js already exists.')

    cb()
  })
}

muxrpcli(process.argv.slice(2), mdm.manifest(commands), api)

@@ -927,54 +927,164 @@
 ull%0A
+%0A
     
-%0A    var config = require('rc')(name, %7B%0A  
+var listen = %5B%0A      'shs+tcp://%5B' + address.ipv6() + '%5D:4239',%0A      'shs+ws://%5B' + address.ipv6() + '%5D:4238'%0A    %5D %0A%0A    if(address.ipv6() ===%22::1%22)%0A
     
@@ -1093,14 +1093,60 @@
 sten
-:
+ =
  %5B%0A  
+    'shs+utp://' + address.ipv4() + ':4239',%0A
     
@@ -1150,33 +1150,32 @@
      'shs+tcp://
-%5B
 ' + address.ipv6
@@ -1169,32 +1169,31 @@
  address.ipv
-6
+4
 () + '
-%5D
 :4239',%0A    
@@ -1190,26 +1190,24 @@
 239',%0A      
-  
 'shs+ws://%5B'
@@ -1196,33 +1196,32 @@
       'shs+ws://
-%5B
 ' + address.ipv6
@@ -1223,31 +1223,86 @@
 .ipv
-6
+4
 () + '
-%5D
 :4238'
-,
 %0A    
-  %5D
+%5D%0A   %0A%0A    var config = require('rc')(name, %7B listen: listen
 , pa
@@ -1338,17 +1338,11 @@
 ame)
-%0A    
 %7D)%0A
-%0A
     
@@ -1926,18 +1926,8 @@
   %7D%0A
-   %0A    %0A%0A
     

Use IPv6 if available, otherwise it will just use IPv4