#!/usr/bin/env python
import sys
from os import path
from urllib import urlretrieve

class Package(type):
	name = None
	def __init__(cls, name, classes, d):
		super(Package, cls).__init__(name, classes, d)
		if not classes:
			return
		cls._parents = classes[:-1]
		packages[name] = cls
	
	def fetch(cls):
		if cls._parents:
			for parent in cls._parents:
				parent.fetch()
		name = '%s.js' % cls.__name__.replace('_', '.')
		if path.exists(name):
			print "PASS %s" % name
			return
		urlretrieve(cls.url, name)

package = Package('Package', (), {})
packages = {}

class jquery(package):
	url = 'http://code.jquery.com/jquery-1.9.1.min.js'

class underscore(package):
	url = 'http://underscorejs.org/underscore-min.js'

class jquery_facebook(jquery, package):
	url = 'https://raw.github.com/vamf12/jquery.facebook/master' + \
		'/jquery.facebook.js'

class jquery_ex(jquery, package):
	url = 'https://raw.github.com/vamf12/jQueryEx/master/jquery.ex.js'

class knockout(jquery, package):
	url = 'http://knockoutjs.com/downloads/knockout-2.2.1.js'

if len(sys.argv) < 2:
	for name, pack in packages.iteritems():
		print name
		print "\t ", pack.url
		if pack._parents:
			print "\t depens on %s" % ', '.join(
				[p.__name__ for p in pack._parents]
			)
		print ""

for arg in sys.argv[1:]:
	packages[arg].fetch()
