import re


def read_basiset_file(filepath):
    """
    Reads a file containing information on basis sets, auxiliar basis sets and 
    ECPs. The file follows the following format: 
    $basis
    sym1 basis1
    sym2 basis2
    ...
    $auxbasis
    sym1 auxbasis1
    sym2 auxbasis2
    ...
    $ecp
    sym1 ecp1
    sym2 ecp2
    ...
    $end

    Parameters
    ----------
    filepath : str or Path
        a valid path to a file with the previously specified format.

    Returns
    -------
    dict or None
        basis, auxbasis, ecp
    """
    regex = r'\$([^\$]*)'
    regex = re.compile(regex)
    with open(filepath,'r') as F: 
        txt = F.read()
    keywords = dict()
    kwblocks = regex.findall(txt)
    for block in kwblocks: 
        lines = [line for line in block.split('\n') if line]
        kw_line = lines[0]
        keyword = kw_line.split(' ')[0]
        if keyword == 'end':
            continue
        keywords[keyword] = dict()
        for line in lines[1:]:
            sym,basis = line.split(' ',1)
            keywords[keyword][sym.lower()] = basis.strip()
    basis = keywords.get('basis',None)
    auxbasis = keywords.get('auxbasis',None)
    ecp = keywords.get('ecp',None)
    return basis,auxbasis,ecp