#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of INGInious. See the LICENSE and the COPYRIGHTS files for
# more information about the licensing of this file.
#

""" Starts the webapp """

import os
import argparse
from inginious.common.base import load_json_or_yaml

import inginious.frontend.webapp.app
from inginious.frontend.webapp.installer import Installer

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--config", help="Path to configuration file. By default: configuration.yaml or configuration.json")
    parser.add_argument("--host", help="Host to bind to. Default is localhost.", default="localhost")
    parser.add_argument("--port", help="Port to listen to. Default is 8080.", type=int, default=8080)
    parser.add_argument("--sshhost", help="Port to listen to for container remote debugging. If you do not set it, remote debugging will be "
                                          "deactivated.", default=None)
    parser.add_argument("--sshport", help="Port to listen to for container remote debugging. Default is 8081.", type=int, default = 8081)
    args = parser.parse_args()

    config = None
    if args.config is None:
        if os.path.isfile("./configuration.yaml"):
            config = "./configuration.yaml"
        elif os.path.isfile("./configuration.json"):
            config = "./configuration.json"
        else:
            raise Exception("No configuration file found")
    else:
        config = args.config

    inginious.frontend.webapp.app.start_app(load_json_or_yaml(config), hostname=args.host, port=args.port, sshhost=args.sshhost, sshport=args.sshport)
