Utility function to set up brightway2 to work correctly with lcopt using the FORWAST database instead of ecoinvent

    By default it'll try and download the forwast database as a .bw2package file from lca-net

    If you've downloaded the forwast .bw2package file already you can set use_autodownload=False and forwast_path to point to the downloaded file

    To overwrite an existing version, set overwrite=True

def lcopt_bw2_forwast_setup(use_autodownload=True, forwast_path=None, db_name=FORWAST_PROJECT_NAME, overwrite=False):        

    """
    Utility function to set up brightway2 to work correctly with lcopt using the FORWAST database instead of ecoinvent

    By default it'll try and download the forwast database as a .bw2package file from lca-net

    If you've downloaded the forwast .bw2package file already you can set use_autodownload=False and forwast_path to point to the downloaded file

    To overwrite an existing version, set overwrite=True
    """

    if use_autodownload:
        
        forwast_filepath = forwast_autodownload(FORWAST_URL)

    elif forwast_path is not None:

        forwast_filepath = forwast_path

    else:
        raise ValueError('Need a path if not using autodownload')

    if storage.project_type  == 'single':
        db_name = storage.single_project_name
        if bw2_project_exists(db_name):
            bw2.projects.set_current(db_name)
        else:
            bw2.projects.set_current(db_name)
            bw2.bw2setup()

    else:
    
        if db_name in bw2.projects:
            if overwrite:                                           
                bw2.projects.delete_project(name=db_name, delete_dir=True)
            else:
                print('Looks like bw2 is already set up for the FORWAST database - if you want to overwrite the existing version run lcopt.utils.lcopt_bw2_forwast_setup in a python shell using overwrite = True')
                return False

        # no need to keep running bw2setup - we can just copy a blank project which has been set up before
        if not bw2_project_exists(DEFAULT_BIOSPHERE_PROJECT):
            lcopt_biosphere_setup()
        
        bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT)
        bw2.create_core_migrations()
        bw2.projects.copy_project(db_name, switch=True)

    bw2.BW2Package.import_file(forwast_filepath)

    return True