var program = require('commander')
	, pkg = require('./package.json')
	, url = require('url')

var urlFixer = function (theUrl) {
	var urlObj = url.parse(theUrl)
		, obj = {}
		, keys = ['protocol', 'slashes', 'auth', 'hostname']
		, str

	keys.forEach(function (key) {
		obj[key] = urlObj[key]
	})

	if (obj.protocol === undefined || obj.hostname === undefined) {
		return false
	}

	obj.port = urlObj.port? urlObj.port: 8080
	str = url.format(obj)
	if (str === '') {
		return false
	}

	// TODO: validate str as a url
	return str
}

program
  .version(pkg.version)
  .description(pkg.description)

program
	.command('setup')
	.description('setup configuration for developing behind the proxy')
  .option('--http <url>', 'set url for http proxy (e.g. http://myproxy.com:8080)', urlFixer)
  .option('--https <url>', 'set url for https proxy (e.g. https://me:mypass@myproxy.com:443', urlFixer)
  .option('--same', 'use the same http proxy for https')
  .option('--save', 'save settings')
	.action(function (options) {
		console.log('http=', options.http)
		console.log('https=', options.https)
		console.log('same=', options.same)
		console.log('save=', options.save)

		if (options.http === false) {
			console.log('Error: Bad http proxy url defined')
			this.outputHelp()
			process.exit(1)
		}

		if (options.https === false) {
			console.log('Error: Bad https proxy url defined')
			this.outputHelp()
			process.exit(1)
		}

		if (options.save === true && options.http === undefined && options.https === undefined) {
			console.log('Error: Cannot save without http or https definition')
			this.outputHelp()
			process.exit(2)
		}

		if (options.http === undefined && options.https === undefined) {
			// TODO: loadSettings
			console.log('Loading settings...')
		} else if(options.save) {
			// TODO: saveSettings
			console.log('Saving settings...')
		}

		if (options.same) {
			if (options.http && options.https === undefined) {
				options.https = options.http
			} else {
				console.log('Error: Cannot use the same http proxy for https if http is not defined')
				this.outputHelp()
				process.exit(3)
			}
		}

		options.home = process.env.HOME
		console.log('HOME=', options.home)

		// TODO: setup
	})

program
  .command('clear')
  .description('clear configuration for developing behind the proxy')
	.action(function () {
		console.log('Clearing...')
		// TODO: clear
	})

program.parse(process.argv)

if (!program.args.length) {
	program.help()
	process.exit()
}
@@ -95,20 +95,24 @@
 %0Avar url
-Fixe
+Validato
 r = func
@@ -136,404 +136,179 @@
 var 
-urlObj = url.parse(theUrl)%0A%09%09, obj = %7B%7D%0A%09%09, keys = %5B'protocol', 'slashes', 'auth', 'hostname'%5D%0A%09%09, str%0A%0A%09keys.forEach(function (key) %7B%0A%09%09obj%5Bkey%5D = urlObj%5Bkey%5D%0A%09%7D)%0A%0A%09if (obj.protocol === undefined %7C%7C obj.hostname === undefined) %7B%0A%09%09return false%0A%09%7D%0A%0A%09obj.port = urlObj.port? urlObj.port: 8080%0A%09str = url.format(obj)%0A%09if (str === '') %7B%0A%09%09return false%0A%09%7D%0A%0A%09// TODO: validate str as a url%0A%09return str
+reUrl = /%5Ehttps?:%5C/%5C/(?:(%5Cw+)(?:%5B:%5D(%5Cw+))?%5B@%5D)?(%5BA-Za-z0-9-%5D+(?:%5B.%5D%5BA-Za-z0-9-%5D+)*%7C(%5Cd%7B1,3%7D(?:%5B.%5D%5Cd%7B1,3%7D)%7B3%7D))%5B:%5D(%5Cd+)$/%0A%09return reUrl.exec(theUrl) === null? false: theUrl
 %0A%7D%0A%0A
@@ -550,28 +550,32 @@
 :8080)', url
-Fixe
+Validato
 r)%0A  .option
@@ -666,12 +666,16 @@
  url
-Fixe
+Validato
 r)%0A 

Change urlFixer to urlValidator