import re
import pathlib


def get_library_folders(steam_path, library_index_path):
    """
    Reads the library index file `libraryfolders.vdf` to get a list of all
    Steam library locations on your system.
    
    Because of the lazy way in which the RegEx is written, it only supports
    up to 100 Steam libraries... but come on.

    Returns the library locations
    """

    locations = []

    # Find the lines matching a library folder (the library index, and the location)
    p = re.compile('"\d{1,3}".+".+"')
    with open(library_index_path.resolve(), encoding="utf-8") as index:
        locations = [p.findall(line.strip()) for line in index if p.search(line)]

    libraries = [
        pathlib.Path(l[0].split("\t\t")[1].replace('"', "")) for l in locations
    ]
    libraries.append(steam_path)

    return sorted(libraries)