uberspaceify = repo.libs.uberspace.Uberspaceify(node)

repo_config_files = {}
for repo, repo_config in node.metadata.get("clerie-backup/repos").items():
    repo_config_files[f'/home/{node.username}/.config/clerie-backup/{repo}/repo_password'] = {
        "content": repo_config["repo_password"],
        "mode": "0600",
    }
    repo_config_files[f'/home/{node.username}/.config/clerie-backup/{repo}/repo_url'] = {
        "content": repo_config["repo_url"],
    }
    repo_config_files[f'/home/{node.username}/.config/clerie-backup/{repo}/auth_username'] = {
        "content": repo_config["auth_username"],
    }
    repo_config_files[f'/home/{node.username}/.config/clerie-backup/{repo}/auth_password'] = {
        "content": repo_config["auth_password"],
        "mode": "0600",
    }
    repo_config_files[f'/home/{node.username}/.config/clerie-backup/{repo}/files'] = {
        "content": "\n".join(repo_config.get("files", [])),
    }
    repo_config_files[f'/home/{node.username}/.config/clerie-backup/{repo}/excludes'] = {
        "content": "\n".join(repo_config.get("excludes", [])),
    }


files = uberspaceify.files({
    f'/home/{node.username}/.bwdownloads/clerie-backup.sh': {
        "content_type": "download",
        "source": "https://git.clerie.de/clerie/nixfiles/raw/commit/d17c2855ac0138c5009350d2539954c701df4eec/pkgs/clerie-backup/clerie-backup.sh",
        "content_hash": "c0b68e6a4979c5ddcaaec10053bd8a1223c0b808",
    },
    f'/home/{node.username}/.bwdownloads/restic.bz2': {
        "content_type": "download",
        "source": "https://github.com/restic/restic/releases/download/v0.17.3/restic_0.17.3_linux_amd64.bz2",
        "content_hash": "cd4336cc86ec4e86decff5c995f08908844120c4",
        "triggers": [
            "action:install_restic",
        ],
    },
    **repo_config_files,
})

actions = {
    "clerie_backup_exists": {
        "command": 'true',
        "unless": f'[[ -f "/home/{node.username}/bin/clerie-backup" && -x "/home/{node.username}/bin/clerie-backup" ]]',
        "triggers": [
            "action:install_clerie_backup",
        ],
    },
    "install_clerie_backup": {
        "command": (
            f'sed '
            f'-e "s#/etc/clerie-backup#/home/{node.username}/.config/clerie-backup#g" '
            f'-e "s#/var/cache/restic#/home/{node.username}/.cache/restic#g" '
            f'"/home/{node.username}/.bwdownloads/clerie-backup.sh" '
            f'> /home/{node.username}/bin/clerie-backup '
            f'&& chmod +x /home/{node.username}/bin/clerie-backup'
        ),
        "triggered": True,
    },
    "restic_exists": {
        "command": 'true',
        "unless": f'[[ -f "/home/{node.username}/bin/restic" && -x "/home/{node.username}/bin/restic" ]]',
        "triggers": [
            "action:install_restic",
        ],
    },
    "install_restic": {
        "command": f'bzip2 -dkc /home/{node.username}/.bwdownloads/restic.bz2 > /home/{node.username}/bin/restic && chmod +x /home/{node.username}/bin/restic',
        "triggered": True,
    },
}