#!/bin/sh
# SecuBox Meta Cataloger
# Copyright (C) 2026 CyberMind.fr
#
# Aggregates MetaBlogizer sites, Streamlit apps, and services
# into a unified catalog with Virtual Books organization
. /lib/functions.sh
CONFIG="metacatalog"
VERSION="1.0.0"
# Paths
DATA_DIR="/srv/metacatalog"
ENTRIES_DIR="$DATA_DIR/entries"
CACHE_DIR="$DATA_DIR/cache"
INDEX_FILE="$DATA_DIR/index.json"
BOOKS_FILE="$DATA_DIR/books.json"
LANDING_PATH="/www/metacatalog"
# Logging
log_info() { echo "[INFO] $*"; logger -t metacatalog "$*"; }
log_warn() { echo "[WARN] $*" >&2; logger -t metacatalog -p warning "$*"; }
log_error() { echo "[ERROR] $*" >&2; logger -t metacatalog -p err "$*"; }
# ═══════════════════════════════════════════════════════════════
# HELPERS
# ═══════════════════════════════════════════════════════════════
ensure_dirs() {
mkdir -p "$DATA_DIR" "$ENTRIES_DIR" "$CACHE_DIR" "$LANDING_PATH/api"
}
uci_get() { uci -q get ${CONFIG}.$1; }
json_escape() {
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/ /\\t/g' | tr '\n' ' '
}
# Generate entry ID from domain
make_id() {
echo "$1" | sed 's/[^a-zA-Z0-9]/-/g' | tr '[:upper:]' '[:lower:]'
}
# Get current timestamp
now_iso() {
date -u +"%Y-%m-%dT%H:%M:%SZ"
}
# ═══════════════════════════════════════════════════════════════
# METABLOGIZER SCANNER
# ═══════════════════════════════════════════════════════════════
scan_metablogizer() {
local sites_root=$(uci_get source_metablogizer.path)
[ -z "$sites_root" ] && sites_root="/srv/metablogizer/sites"
[ ! -d "$sites_root" ] && return 0
log_info "Scanning MetaBlogizer sites in $sites_root"
local count=0
for site_dir in "$sites_root"/*/; do
[ -d "$site_dir" ] || continue
local site=$(basename "$site_dir")
local index_html="$site_dir/index.html"
[ -f "$index_html" ] || continue
# Get UCI config for this site
local domain=$(uci -q get metablogizer.site_$site.domain 2>/dev/null)
[ -z "$domain" ] && domain="$site.gk2.secubox.in"
local port=$(uci -q get metablogizer.site_$site.port 2>/dev/null)
[ -z "$port" ] && port="80"
# Extract metadata from HTML (BusyBox-compatible)
local title=$(sed -n 's/.*
\([^<]*\)<\/title>.*/\1/p' "$index_html" 2>/dev/null | head -1)
[ -z "$title" ] && title="$site"
local description=$(sed -n 's/.*meta[^>]*description[^>]*content="\([^"]*\)".*/\1/p' "$index_html" 2>/dev/null | head -1)
# Detect features
local has_canvas="false"
grep -q '