#!/usr/bin/env python3

import sys, os, subprocess

def main():
    # Translate file arguments to abolsute paths.
    # Eco runs under the assumption that it has changed dir into
    # the eco lib dir. Ideally this should not be the case. XXX

    def translate_arg(arg):
        if os.path.exists(arg):
            return os.path.abspath(arg)
        else:
            return arg

    call_args = [sys.executable, "eco.py"]
    argv = sys.argv[1:]
    for i in range(len(argv)):
        arg = argv[i]
        if not arg.startswith("-"):
            call_args += [os.path.abspath(arg)]
            i += 1
        elif arg == "-e" or arg == "--export":
            call_args += [arg,
                          os.path.abspath(argv[i + 1]),
                          os.path.abspath(argv[i + 2]),
                          ]
            i += 2
        else:
            call_args += [arg]

    change_to = os.path.join(os.path.dirname(__file__), "..", "lib", "eco")
    os.chdir(change_to)

    subprocess.call(call_args)

if __name__ == "__main__":
    main()
