import glob
from pathlib import Path


def merge_all_dot_sol(outputs_dir, dot_sol_output, num_rows):
    """
    Merge all .SOL into one
    :param outputs_dir: directory containing the dynamic .SOL
    :param dot_sol_output: file to write the content to. This is the static .SOL
    :param num_rows: number of points. This is to make sure that all dynamic SOL are written
    :return: pathname to the final .SOL file
    """
    # get all the dynamic .SOL
    match = str(outputs_dir) + '/*.SOLD'

    all_dot_sols = glob.glob(match)
    all_dot_sols.sort()

    # for i in all_dot_sols:
    #     print(i)

    # if len(all_dot_sols) != num_cells: # something wrong
    #     print("Stopping: The static database seems not complete")
    #     return

    with open(dot_sol_output, "wb") as outfile:
        for f in all_dot_sols:
            with open(f, "rb") as infile:
                outfile.write(infile.read())
                outfile.write('\n'.encode())
    dot_sol_output = str(outputs_dir) + '/' + dot_sol_output
    return Path(dot_sol_output)