"""
Package Tree

Usage:
  packagetree [--dryrun] [--dest-dir=<path>] <file>

Options:
  --dryrun              Don't actually generate the packages, just output some details about what would be run.
  --dest-dir=<path>     Destination directory for created packages. If not specified, defaults to '.'.
"""

from docopt import docopt

import yaml
import tempfile
import sys
import os
import subprocess

def create_package(package, spec, cli):
	print package
	print '  Version:', spec['version']

	cmd = [
		'fpm',
		'-s', 'empty',
		'-t', 'deb',
		'-n', package,
		'-v', spec['version'],
	]

	print '  Dependencies:'
	for dep in spec['dependencies']:
		print '   ', dep

		cmd.append('-d')
		cmd.append('%s' % dep)

	if cli['--dryrun']:
		print '  Command:', ' '.join(cmd)
	else:
		subprocess.call(cmd)
	
	print

def main():
	arguments = docopt(__doc__, version='Package Tree v0.1')
	
	f = open(arguments['<file>'], 'rb')
	tree = yaml.load(f.read())
	f.close()

	if not arguments['--dryrun']:
		if os.system('which fpm >/dev/null') != 0:
			sys.stderr.write('FPM could not be found, is it installed? If not, try \'gem install fpm\'\n')
			sys.exit(1)

		if arguments['--dest-dir'] is not None:
			dest = arguments['--dest-dir']
			if not os.path.exists(dest):
				os.makedirs(dest)
			os.chdir(dest)

	for package,spec in tree.iteritems():
		create_package(package, spec, arguments)
@@ -62,16 +62,42 @@
 =%3Cpath%3E%5D
+ %5B--iteration=%3Citeration%3E%5D
  %3Cfile%3E%0A
@@ -130,16 +130,18 @@
         
+  
 Don't ac
@@ -244,16 +244,18 @@
 th%3E     
+  
 Destinat
@@ -325,16 +325,97 @@
 to '.'.%0A
+  --iteration=%3Citeration%3E Specify a default iteration, otherwise none specified.%0A
 %22%22%22%0A%0Afro
@@ -695,16 +695,114 @@
 '%5D,%0A%09%5D%0A%0A
+%09if cli%5B'--iteration'%5D is not None:%0A%09%09cmd.append('--iteration')%0A%09%09cmd.append(cli%5B'--iteration'%5D)%0A%0A
 %09print '

Add iteration default support.