#!/usr/bin/env python
import os
import sys
import time
from omdox import models
from omdox import feedback

print 'hello'

# set working directory
cwd = os.getcwd()
# if there is no layout.html in this directory then fail
if not os.path.exists('./layout.html'):
    feedback.error('layout.html not found, are you in the right directory?')
    sys.exit()


tree = models.Tree(cwd)
feedback.header('Rendering starting', indent=False)
for branch in tree.get_branches():
    # processs the branches
    branch.process()
    for node in branch.get_nodes():
        node.process()
# if we're watching enter the loop
if set(sys.argv).intersection(['-w', '--watch']):
    feedback.header(
            '\nomdox is in watch mode. ctrl+c to exit',
            indent=False)
    try:
        while True:
            for branch in tree.get_branches():
                # processs the branches
                branch.process()
                for node in branch.get_nodes():
                    if node.has_changed():
                        node.process()
            time.sleep(0.5)
    except KeyboardInterrupt:
        feedback.header('Exiting.', indent=False)
