def get_check_plugins_path():
    global config_file
    curr_path = current_path()
    path = 'path config for Check_plugins is empty'
    with open(curr_path+config_file) as f:
        for line in f:
            if 'check_plugins_path' in line:
                path = line.split("=")[1].strip()
                break
    return path
def read_services_config():
    global config_file
    curr_path = current_path()
    hostgroups = {}
    all_services = {}
    serv_switch = 'off'
    service_checks = {}
    with open(curr_path+config_file) as f:
        for line in f:
            if 'tcp-service' in line or 'udp-service' in line:
                if 'tcp-service' in line:
                    proto = 'tcp'
                else:
                    proto = 'udp'
                service = line.split(":")[1][:-5].lstrip().rstrip()
                port = line.split(":")[2][:-11].lstrip().rstrip()
                all_services[service]=port
                used_for_temp1 = line.split(":")[3][:-2].lstrip().rstrip().split(",")
                used_for_temp2 = []
                for i in used_for_temp1:
                    used_for_temp2.append(i.lstrip("\""))
                used_for = []
                for i in used_for_temp2:
                    hostgroup = i.strip().lstrip("\"").rstrip("\"")
                    used_for.append(hostgroup)
                
                    if hostgroup in hostgroups:
                        hostgroups[hostgroup]["services"].append({"service": service, "port": port, "proto": proto})
                        hostgroups[hostgroup]["port_list"].append(port)
                    else:
                        hostgroups[hostgroup] = {
                            "port_list": [port],
                            "services": [{"service": service, "port": port, "proto": proto}]}
            if '-Service List Start-' in line:
                serv_switch = "on"
            if '-Service List End-' in line:
                serv_switch = "off"
            if serv_switch == 'on':
                if ":" in line:
                    serv = line.split(":",1)
                    service_checks[serv[0]]= serv[1]
                
        return hostgroups, all_services, service_checks
from tempfile import mkstemp
from shutil import move
from os import remove, close, chmod
from tools import current_path

curr_path = current_path()
path_to_hostgroups = curr_path+"conf.d/autoidm/autoidm_hostgroups.cfg"


def list_services():
    global path_to_hostgroups
    all_service = []
    all_service.append("None")
    with open(path_to_hostgroups) as f:    
        for line in f:
            if "alias" in line:
                if "#" not in line:
                    tmp_service = line.rsplit("alias")
                    service = tmp_service[1].strip()
                    if service != 'all':
                        all_service.append(service)

    return all_service


def add_del_to_hostgroups(hostname, service, action):
    global path_to_hostgroups
    switch = "off"
    
    if service == "None":
        return "OK"