import os
import shutil
import utility
import storage
import scanner
import builder
import subprocess


def output_name(path):
    return os.path.abspath(path).split('/')[-1]


# Deletes on compilation products.
def clean(path):
    shutil.rmtree(path + '/build', ignore_errors=True)


# Compiles directory.
def build(path):
    builder.build_directory(path)


# Geneerates a self-contained C project [which doesn't depend
# on kit] and places it in build/dist.
def dist(path):
    print utility.color('TODO: command `dist` has not yet been implemented', 'red')


# Attempts to clone repository from remote index.
def fetch(arg):
    if arg == 'all':
        for name in storage.remote_module_names():
            fetch(name)
    elif arg.find('.git') >= 0:
        storage.fetch_unindexed_module(arg)
    else:
        storage.fetch_module(arg)


# Deletes module with given name from local index.
def remove(path):
    print 'removing', path
    if path.find(storage.modules) == 0:
        name = path.split('/')[-1]
        storage.clear_module(name)
    else:
        shutil.rmtree(path, ignore_errors=True)


# Sets up boilerplate project structure.
def init(path):
    wd = os.getcwd()
    os.chdir(path)
    os.makedirs('documentation')
    os.makedirs('sources')
    os.makedirs('tests')
    os.makedirs('documentation/generated')
    utility.touch('README.md')
    utility.touch('LICENSE.md')
    with open('sources/main.c', 'w') as f:
        f.write('''
			#include <stdio.h>

			int main()
			{
				printf("Hello, world!");
			}
			''')
    with open('tests/main.c', 'w') as f:
        f.write('''
			#include <stdio.h>

			int main()
			{
				printf("No tests to run.");
			}
			''')
    with open('.gitignore', 'w') as f:
        f.write('build\n')
    os.chdir(wd)


# If building an application, its executable is made available
# globally. Regardless, the library is placed in the local index.
def install(path):
    build(path)
    name = os.path.abspath(path).split('/')[-1]
    if scanner.has_main(path):
        shutil.copy('build/bin/' + name, '/usr/local/bin/' + name)
    dest = storage.module_path(name)
    shutil.copytree(path, dest)
    storage.index(name, 'none')
    execute('clean', name)
    execute('build', name)


# Lists available modules (both local and remote).
def modules(arg):
    local = storage.local_modules()
    print 'local:  (' + str(len(local)) + ')'
    for m in local:
        if storage.module_compiled(m[0]):
            print ' - ' + m[0], '[' + utility.color('compiled', 'green') + ']'
        else:
            print ' - ' + m[0], '[' + utility.color('not compiled', 'red') + ']'

    if arg == 'all':
        remote = storage.remote_modules()
        print '\nremote:  (' + str(len(remote)) + ')'
        for m in remote:
            print ' -', m[0], '[' + utility.color(m[1], 'yellow') + ']'


# Builds and runs generated executable.
def run(path):
    build(path)
    name = output_name(path)
    subprocess.call(path + '/build/bin/' + name)


# Builds target and runs its tests.
def test(path):
    build(path)
    subprocess.call(path + '/build/bin/tests')


# Hack.
def execute(command, argument):
    if command in ['fetch', 'modules']:
        globals()[command](argument)
    elif argument == 'all':
        print ' - detected ALL'
        for module in storage.local_module_names():
            execute(command, module)
    else:
        path = os.path.abspath('.')
        if argument:
            path = storage.module_path(argument)
        globals()[command](path)

@@ -96,16 +96,104 @@
 rocess%0A%0A
+HELLO_WORLD = '''#include %3Cstdio.h%3E%0A%0Aint main()%0A%7B%0A    printf(%22Hello, world!%22);%0A%7D%0A'''%0A%0A%0A%0A
 %0Adef out
@@ -1546,242 +1546,89 @@
 ite(
-'''%0A%09%09%09#include %3Cstdio.h%3E%0A%0A%09%09%09int main()%0A%09%09%09%7B%0A%09%09%09%09printf(%22Hello, world!%22);%0A%09%09%09%7D%0A%09%09%09''')%0A    with open('tests/main.c', 'w') as f:%0A        f.write('''%0A%09%09%09#include %3Cstdio.h%3E%0A%0A%09%09%09int main()%0A%09%09%09%7B%0A%09%09%09%09printf(%22No tests to run.%22);%0A%09%09%09%7D%0A%09%09%09'''
+HELLO_WORLD)%0A    with open('tests/main.c', 'w') as f:%0A        f.write(HELLO_WORLD
 )%0A  

Correct helloworld source