#!/usr/bin/python3

import getopt
import logging
import os
import re
import sys

global log


def setup_logging():
    """creates and returns stderr logger"""
    global log

    log = logging.getLogger()
    hdlr = logging.StreamHandler()
    form = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s")
    hdlr.setFormatter(form)
    log.addHandler(hdlr)
    log.setLevel(logging.WARN)


def parse_args():
    """Parses the command line arguments"""
    global log
    global new_delayed_delete

    new_delayed_delete = ""
    try:
        opts, args = getopt.getopt(sys.argv[1:], "f:hv", ["file", "help", "verbose"])
    except getopt.GetoptError as e:
        log.error("While parsing arguments: %s." % str(e).strip())
        usage()
        sys.exit(1)
    for opt, arg in opts:
        if opt == "-f" or opt == "--file":
            new_delayed_delete = arg
        elif opt == "-h" or opt == "--help":
            usage()
            sys.exit()
        elif opt == "-v" or opt == "--verbose":
            log.setLevel(logging.DEBUG)

    if new_delayed_delete == "":
        log.error("Delayed delete file not specified!")
        usage()
        sys.exit(1)


def usage():
    """prints the command line options of the program"""

    print(
        """
            Usage:""",
        os.path.basename(sys.argv[0]),
        """[options]

            Options:
              -f --file    New Delayed Delete file
              -h --help    Display this help
              -v --verbose Run in verbose mode

            """,
    )


def create_ldap_modif():
    # Temporarily removed Shares (BUG #102173)
    # 'GLUE2ShareID' : ['GLUE2ComputingShareServingState','GLUE2StorageShareServingState']
    status = {
        "GlueServiceUniqueID": ["GlueServiceStatus", "GlueServiceStatusInfo"],
        "GlueCEUniqueID": ["GlueCEStateStatus"],
        "GlueSEUniqueID": ["GlueSEStatus"],
        "GLUE2ApplicationEnvironmentID": ["GLUE2ApplicationEnvironmentState"],
        "GLUE2ComputingActivityID": [
            "GLUE2ComputingActivityRestartState",
            "GLUE2ComputingActivityState",
        ],
        "GLUE2ServiceID": ["GLUE2ServiceStatusInfo"],
        "GLUE2EndpointID": [
            "GLUE2EndpointHealthState",
            "GLUE2EndpointHealthStateInfo",
            "GLUE2EndpointServingState",
        ],
    }

    try:
        dns = open(new_delayed_delete, "r")
        for dn in dns:
            if dn != "":
                try:
                    log.debug("DN: %s" % (dn))
                    id, _ = dn.split("=", 1)
                    log.debug("Detected attribute: %s" % (id))
                    attribute = re.search(
                        r"(GlueServiceUniqueID|GlueCEUniqueID|GlueSEUniqueID\
                           |GLUE2ApplicationEnvironmentID|GLUE2ComputingActivityID\
                           |GLUE2ServiceID|GLUE2EndpointID)",
                        id,
                    )
                    if attribute is not None:
                        log.debug("Update state attributes: OK")
                        print("dn: %s" % (dn.strip()))
                        print("changetype:modify")
                        for att_state in status[attribute.group()]:
                            print("replace: %s" % (att_state))
                            print("%s: Unknown" % (att_state))
                            print("-")
                            print("")
                    else:
                        log.debug("No state attributes")

                except ValueError:
                    log.error("Unable to parse DN %s" % (dn))
        dns.close()

    except IOError:
        log.error("Unable to open Delayed Delete file %s" % (new_delayed_delete))
        sys.exit(1)


if __name__ == "__main__":

    setup_logging()
    parse_args()
    create_ldap_modif()
