# AUTOGENERATED! DO NOT EDIT! File to edit: ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py.

# %% top_export
from pathlib import Path

from repoyard import const

def init_repoyard(
    config_path: Path|None = None,
    data_path: Path|None = None,
):
    """
    Initialize repoyard.
    
    Will create the necessary folders and files to start using repoyard.
    """
    
    # AUTOGENERATED! DO NOT EDIT! File to edit: ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py.
    
    # %% auto 0
    __all__ = ['config_path', 'data_path', 'config', 'paths']
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py 9
    config_path = config_path or const.DEFAULT_CONFIG_PATH
    data_path = data_path or const.DEFAULT_DATA_PATH
    
    if config_path.as_posix() != const.DEFAULT_CONFIG_PATH.as_posix():
        print(f"Using a non-default config path. Please set the environment variable {const.ENV_VAR_REPOYARD_CONFIG_PATH} to the given config path for repoyard to use it. ")
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py 11
    from ..config import get_config, _get_default_config_dict, Config
    import toml
    if not config_path.exists():
        print("Creating config file at:", config_path)
        Path(config_path).expanduser().parent.mkdir(parents=True, exist_ok=True)
        default_config_dict = _get_default_config_dict(config_path=config_path, data_path=data_path)
        del default_config_dict['config_path'] # Don't save the config path to the config file
        config_toml = toml.dumps(default_config_dict)
        
        Path(config_path).expanduser().write_text(config_toml)
    config = get_config(config_path)
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py 13
    if not config.default_repoyard_exclude_path.exists():
        config.default_repoyard_exclude_path.write_text(const.DEFAULT_REPOYARD_EXCLUDE)
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py 17
    paths = [
        config.repoyard_data_path,
        config.local_store_path,
    ]
    
    for path in paths:
        if not path.exists():
            print(f"Creating folder: {path}")
            path.mkdir(parents=True, exist_ok=True)
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py 19
    for storage_location_name, storage_location in config.storage_locations.items():
        storage_location.store_path.mkdir(parents=True, exist_ok=True)
        if (config.local_store_path / storage_location_name).exists():
            (config.local_store_path / storage_location_name).unlink()
        (config.local_store_path / storage_location_name).symlink_to(storage_location.store_path)
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py 21
    from ..config import _default_rclone_config
    if not config.rclone_config_path.exists():
        print(f"Creating rclone config file at: {config.rclone_config_path}")
        config.rclone_config_path.write_text(_default_rclone_config)
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/00_init_repoyard.pct.py 25
    print("Done!\n")
    print("You can modify the config at:", config_path)
    print("All repoyard data is stored in:", config.repoyard_data_path)