#!/usr/bin/bash set -euo pipefail IFS=$'\n\t' SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" if [[ -d "${SCRIPT_DIR}/../../.git" ]] || [[ -f "${SCRIPT_DIR}/../../nodo/config/etc/mirrors.env" ]]; then REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" else REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" fi if [[ -f "${REPO_ROOT}/nodo/config/etc/mirrors.env" ]]; then # shellcheck source=/dev/null source "${REPO_ROOT}/nodo/config/etc/mirrors.env" elif [[ -f "${REPO_ROOT}/config/etc/mirrors.env" ]]; then # shellcheck source=/dev/null source "${REPO_ROOT}/config/etc/mirrors.env" elif [[ -f "${REPO_ROOT}/etc/mirrors.env" ]]; then # shellcheck source=/dev/null source "${REPO_ROOT}/etc/mirrors.env" fi MIRRORS_STAGING_DIR="${MIRRORS_STAGING_DIR:-$HOME/.local/share/mxos-mirrors/staging}" IPFS_USE_NOCOPY="${IPFS_USE_NOCOPY:-false}" log() { local level="$1" shift local timestamp timestamp="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" echo "{\"timestamp\":\"${timestamp}\",\"level\":\"${level}\",\"message\":\"$*\"}" } usage() { cat << USAGE Uso: $0 [OPCIONES] [DIRECTORIO_STAGING] Ingesta el directorio de staging en Kubo (IPFS) generando el grafo UnixFS DAG y retornando el CID raíz. Opciones: --nocopy Utiliza la extensión filestore (--nocopy) para evitar duplicación en disco -h, --help Muestra este mensaje de ayuda USAGE exit 1 } main() { local use_nocopy="${IPFS_USE_NOCOPY}" local staging_dir="${MIRRORS_STAGING_DIR}" while [[ $# -gt 0 ]]; do case "$1" in --nocopy) use_nocopy="true" shift ;; -h|--help) usage ;; *) staging_dir="$1" shift ;; esac done if [[ ! -d "${staging_dir}" ]]; then log "ERROR" "Directorio de staging no encontrado: ${staging_dir}" >&2 exit 1 fi # Limpiar artefactos innecesarios en la raíz de staging rm -rf "${staging_dir}/README.rst" \ "${staging_dir}/logo.svg" \ "${staging_dir}/guardianes.json" \ "${staging_dir:?}/bin" \ "${staging_dir}/config" \ "${staging_dir:?}/etc" \ "${staging_dir}/systemd" 2>/dev/null || true # Asegurar que las páginas web estén presentes en la raíz de staging for doc in README.html index.html; do if [[ -f "${REPO_ROOT}/${doc}" ]]; then cp -f "${REPO_ROOT}/${doc}" "${staging_dir}/${doc}" fi done # Asegurar que el directorio de herramientas del nodo esté sincronizado if [[ -d "${REPO_ROOT}/nodo" ]]; then mkdir -p "${staging_dir}/nodo" cp -rf "${REPO_ROOT}/nodo/"* "${staging_dir}/nodo/" fi # Asegurar que la documentación técnica Sphinx esté presente if [[ -d "${REPO_ROOT}/docs/_build/html" ]]; then mkdir -p "${staging_dir}/docs" cp -rf "${REPO_ROOT}/docs/_build/html/"* "${staging_dir}/docs/" fi # Asegurar que el tablero Vigía esté sincronizado en la raíz del catálogo if [[ -d "${REPO_ROOT}/vigia" ]]; then mkdir -p "${staging_dir}/vigia" cp -rf "${REPO_ROOT}/vigia/"* "${staging_dir}/vigia/" fi log "INFO" "Iniciando ingesta UnixFS de ${staging_dir} en Kubo..." local ipfs_args=(add -r --cid-version=1 --hash=sha2-256 -Q) if [[ "${use_nocopy}" == "true" ]]; then ipfs_args+=(--nocopy) fi local root_cid root_cid="$(ipfs "${ipfs_args[@]}" "${staging_dir}")" if [[ -z "${root_cid}" ]]; then log "ERROR" "Fallo en la generación del CID raíz." >&2 exit 1 fi log "INFO" "Ingesta completada exitosamente. CID raíz: ${root_cid}" # Asegurar pineo local del nuevo CID ipfs pin add "${root_cid}" >/dev/null 2>&1 || true # Salida limpia del CID por stdout echo "${root_cid}" } main "$@"