import random
import sys
import multiprocessing
import glob
import time

from compile import do_compile
from link_and_load import link_and_load
from make_parallel import main

models = []
code = []

address = sys.argv[1]
taskname = sys.argv[2]
files = [a.replace("\\", "/") for a in sum([glob.glob(f) for f in sys.argv[3:]], [])]

for f in files:
    if f.endswith(".mvc"):
        models.append(f)
    elif f.endswith(".alc"):
        code.append(f)
    else:
        print("Unknown file format for file " + f)
        print("Requires either .mvc or .alc")

def initialize_SCD():
    import urllib2
    import urllib
    import json
    taskname = random.random()

    while 1:
        try:
            # Create new task
            urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % taskname, "taskname": "task_manager"}))).read()
            break
        except:
            time.sleep(0.01)

    data = []
    data.append(4)
    data.append("initialize_SCD")
    data.append("models/SimpleClassDiagrams")
    data.append("exit")
    data.append(2)

    print("Initializing SCD")
    urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "taskname": taskname}))).read()
    urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": taskname}))).read()
    print("Initialized!")

def do_compile_wrapper(filename, mode, grammar):
    do_compile(address, filename, str(random.random()), filename, mode, ["--debug"], grammar=grammar)

# Parse all models and upload them
initialize_SCD()

for m in models:
    print("[MODEL] %s" % m)
    do_compile_wrapper(m, "MO", "grammars/modelling.g")

main(address, taskname, code)
