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

import sys
import argparse
import inginious_container_api.feedback

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, 
                                 description='Set the feedback message of the task or specified problem.',
                                 epilog='If message not specified, reads on stdin.')
parser.add_argument('-a', '--append', help="append to current feedback", action='store_true')
parser.add_argument('-i', '--id', help="problem id", default="")
parser.add_argument('-e', '--escape', help="interprets backslash escapes", action='store_true')
parser.add_argument('-m', '--message' , help="feedback message", default="")
args = parser.parse_args()

append = args.append
message = args.message if args.message else sys.stdin.read()
problem = args.id

# Doing the real stuff

feedback = message.encode('utf-8').decode('unicode-escape').encode('latin1').decode('utf-8') if args.escape else message

if problem == '':
    inginious_container_api.feedback.set_global_feedback(feedback, append)
else:
    inginious_container_api.feedback.set_problem_feedback(feedback, problem, append)
