http = require 'http'
program = require 'commander'
Logger = require 'tmpl-log'
Proxy = require './proxy'
logr = new Logger
program
.version('0.1.2')
program
.command('start')
.option('-p, --port
', 'port on which to listen', Number, 8080)
.option('-H, --host ', 'host on which to start the proxy', String, 'localhost')
.option('-t, --threads ', 'max concurrent threads', Number, 12)
.option('-s, --partSize ', 'thread part size', Number, 1024 * 1024 * 2)
.option('-l, --contentLength ', 'min content length for threaded downloading', Number, 1024 * 1024 * 4)
.action (args..., { port, host, threads, partSize, contentLength }) ->
proxy = new Proxy { threads, partSize, contentLength }
proxy.on 'error', (e) ->
if e.syscall is 'listen'
switch e.code
when 'EADDRNOTAVAIL' then logr.log "Error:>> address or port not available (http://#{host}>:#{port}>)"
when 'EACCES' then logr.log "Error:>> reserved address or port (http://#{host}>:#{port}>)"
throw e
proxy.listen port, host, ->
logr.log """
Proxy started on http://#{host}>:#{port}>. Press CTRL+C to stop it.
"""
program.parse process.argv