def ensure_ext(filepath, ext, case_sensitive=False):
    """
    Return the path with the extension added if it is not already set.

    :arg ext: The extension to check for, can be a compound extension. Should
              start with a dot, such as '.blend' or '.tar.gz'.
    :type ext: string
    :arg case_sensitive: Check for matching case when comparing extensions.
    :type case_sensitive: bool
    """
    if case_sensitive:
        if filepath.endswith(ext):
            return filepath
    elif filepath[-len(ext):].lower().endswith(ext.lower()):
        return filepath
    return filepath + ext
