#!/usr/bin/env bash # # usync - Unified sync command for the Ultimate Notetaking System # # Syncs both nb notebooks and triggers Syncthing scans. # # Usage: # usync - Sync everything # usync nb - Sync only nb notebooks # usync st - Sync only Syncthing folders # usync status - Show sync status set -euo pipefail # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[OK]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } sync_nb() { log_info "Syncing nb notebooks..." if ! command -v nb &> /dev/null; then log_error "nb not found in PATH" return 1 fi # Get list of notebooks notebooks=$(nb notebooks --names 2>/dev/null || echo "") if [ -z "$notebooks" ]; then log_warn "No nb notebooks found" return 0 fi # Sync each notebook for notebook in $notebooks; do log_info " Syncing notebook: $notebook" if nb "$notebook:sync" 2>/dev/null; then log_success " $notebook synced" else log_warn " $notebook sync failed (no remote?)" fi done } sync_syncthing() { log_info "Triggering Syncthing scan..." if ! command -v syncthing &> /dev/null; then log_error "syncthing not found in PATH" return 1 fi # Check if Syncthing is running if ! syncthing cli show system &> /dev/null; then log_warn "Syncthing is not running" return 1 fi # Trigger scan on all folders if syncthing cli scan --all 2>/dev/null; then log_success "Syncthing scan triggered" else log_error "Failed to trigger Syncthing scan" return 1 fi } show_status() { echo "=== Sync Status ===" echo "" # nb status echo "--- nb Notebooks ---" if command -v nb &> /dev/null; then nb notebooks 2>/dev/null || echo " No notebooks" echo "" echo "Last sync times:" for notebook in $(nb notebooks --names 2>/dev/null); do nb_dir="${NB_DIR:-$HOME/.nb}" if [ -d "$nb_dir/$notebook/.git" ]; then last_commit=$(git -C "$nb_dir/$notebook" log -1 --format="%ar" 2>/dev/null || echo "never") echo " $notebook: $last_commit" fi done else echo " nb not installed" fi echo "" # Syncthing status echo "--- Syncthing ---" if command -v syncthing &> /dev/null && syncthing cli show system &> /dev/null; then syncthing cli show system 2>/dev/null | grep -E "(myID|startTime)" || true echo "" echo "Folders:" syncthing cli show config 2>/dev/null | jq -r '.folders[] | " \(.label // .id): \(.path)"' 2>/dev/null || echo " Unable to get folder info" else echo " Syncthing not running" fi } # Main case "${1:-all}" in nb) sync_nb ;; st|syncthing) sync_syncthing ;; status) show_status ;; all|"") sync_nb echo "" sync_syncthing echo "" log_success "All sync operations complete" ;; -h|--help|help) echo "usync - Unified sync command" echo "" echo "Usage:" echo " usync Sync everything (nb + Syncthing)" echo " usync nb Sync only nb notebooks" echo " usync st Trigger Syncthing scan" echo " usync status Show sync status" echo " usync help Show this help" ;; *) log_error "Unknown command: $1" echo "Run 'usync help' for usage" exit 1 ;; esac