#!/usr/bin/env python3
#
# Copyright (C) 2012-2017 Chris Cummins.
#
# This file is part of emu.
#
# Emu is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# Emu is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with emu.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function

import sys
import os

from emu import *
from emu import io


def main(arguments=[]):
    """
    Emu main entry point.

    Accepts a list of arguments, and parses and performs the
    appropriate actions.

    Examples:

        >>> emu.main()
        # Opens man page.

        >>> emu.main(["clean", "--dry-run"])
        # Runs "clean" script with args "--dry-run".

    Arguments:

        arguments (list of str, optional): Arguments to parse and
          execute.

    Returns:

        int: Return code of command.
    """

    # Go to man page if help argument or no args.
    if (not len(arguments) or
        arguments[0] == "-h" or
        arguments[0] == "--help"):
        return os.system("man emu")

    # emu help [command] handling.
    elif (arguments[0] == "help"):
        if len(arguments) > 1:
            # Lookup the path to emu command, as this will fail if the
            # command does not exist.
            lookup_emu_program(arguments[1])
            return os.system("man emu-" + arguments[1])
        return os.system("man emu")

    # emu version command.
    elif arguments[0] == "--version" or arguments[0] == "version":
        io.printf(Meta.versionstr())
        return 0

    else:
        # Assemble script path and arguments.
        script_path = lookup_emu_program(arguments[0])
        script_args = arguments[1:]
        command = ' '.join([script_path] + script_args)
        return os.system(command)


if __name__ == "__main__":
    try:
        ret = main(sys.argv[1:])
        sys.exit(int(ret % 255))
    except InvalidEmuCommand as error:
        io.fatal(error)
