from datetime import datetime
import shutil


def fix_md5_file(filename, append_path, savebu=False):
    """
    Fixes the md5 file so that it will check the zip at the correct path
    If you run md5sum in a different directory, then the md5 file needs to
    specify the directory or it will be unable to find it.  This adds the
    full path in front.
    Note:  md5sum is insanely specific:
        <md5sum_checksum><space><space><file_name>

    Parameters
    ----------
    filename : str
        Valid filename with resolvable path from cwd
    append_path : str
        path to append in the file
    savebu : boolean, default False
        If you a bu of the file should be made prior to modification

    Returns
    -------
        Fixed str saved within the md5 file
    """
    with open(filename, 'r+', encoding='UTF-8') as fileobj:
        if savebu:
            bufilename = (f"{filename}-{datetime.now().strftime('%Y%m%d-%H%M%S')}.backup" )
            shutil.copy(filename, bufilename)

        old = fileobj.read().split()
        fileobj.seek(0)
        fpfn = f"{old[0]}  {append_path}/{old[1]}"
        fileobj.write(fpfn)
        fileobj.truncate()
        return fpfn