#!/usr/bin/python3

"""
DebugPrint.py
   Utility to add text to the Gratia log file

   usage: DebugPrint.py [-h|--help]
       DebugPrint.py [-l #|--level=#] [-c <probeconfig>|--conf=<probeconfig>] <message>
       cat message.txt | DebugPrint.py [-l #|--level=#] [-c <probeconfig>|--conf=<probeconfig>]
"""

from __future__ import print_function

import sys
from gratia.common import GratiaCore
import getopt

def Usage():
    """
    Print the usage.
    """
    print("""usage: DebugPrint.py [-h|--help]
       DebugPrint.py [-l #|--level=#] [-c <probeconfig>|--conf=<probeconfig>] <message>
       cat message.txt | DebugPrint.py [-l #|--level=#] [-c <probeconfig>|--conf=<probeconfig>]""")

def main():
    """
    Body of DebugPrint
    """
    level = 0
    customConfig = None

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hc:l:", ["help", "conf=" , "level="])

    except getopt.GetoptError:
        Usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ["-h", "--help"]:
            Usage()
            sys.exit()

        if opt in ["-c", "--conf"]:
            customConfig = arg

        if opt in ["-l", "--level"]:
            level = int(arg)

    GratiaCore.quiet = 1

    if customConfig:
        GratiaCore.Initialize(customConfig)
    else:
        GratiaCore.Initialize()

    GratiaCore.quiet = 0

    if len(args) > 0:
        GratiaCore.DebugPrint(level, " ".join(args).rstrip())
    else:
        while 1:
            try:
                line = input()
            except EOFError as ex:
                break
            GratiaCore.DebugPrint(level, line.rstrip())


if __name__ == "__main__":
    main()
