#! /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.

"""
    Small tool to allow verification of .task and .course files.
"""
import sys
import os

sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), '..', '..'))

from inginious.common.course_factory import create_factories
import inginious.frontend.webapp.plugins.task_file_readers.json_reader
import inginious.frontend.webapp.plugins.task_file_readers.rst_reader

import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--taskdir", help="Path to the directory where the courses are stored. Default to ./tasks", default="./tasks")
    parser.add_argument("course", help="Course id")
    parser.add_argument("task", help="Task id")
    args = parser.parse_args()

    course_factory, task_factory = create_factories(args.taskdir)

    task_factory.add_custom_task_file_manager(inginious.frontend.webapp.plugins.task_file_readers.json_reader.TaskJSONFileReader())
    task_factory.add_custom_task_file_manager(inginious.frontend.webapp.plugins.task_file_readers.rst_reader.TaskRSTFileReader())

    try:
        course_factory.get_task(args.course, args.task)
    except Exception as inst:
        print("There was an error while validating the file:")
        print(type(inst))
        print(inst)
        exit(1)
    else:
        print("File verification succeeded")
        exit(0)
