#!/usr/bin/env bash # Sync the OpenNovelWriter agent pack from this repo into an Obsidian vault. # # sync-to-vault.sh # dry run: report what differs # sync-to-vault.sh --apply # write, backing up what it replaces # # Overwrites ONLY: .opencode/agent/*, .opencode/command/*, .opencode/languages.yaml, # docs/SCHEMA.md, start.sh # Never touches: DATA/, AGENTS.md, 01/, Notizen/, Welt/, .obsidian/, the vault's own # .opencode/*.json (created only if missing) set -euo pipefail PACK="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" VAULT="${1:-}" APPLY="${2:-}" if [ -z "$VAULT" ] || [ ! -d "$VAULT" ]; then echo "usage: $(basename "$0") [--apply]" >&2 exit 2 fi if [ "$APPLY" != "" ] && [ "$APPLY" != "--apply" ]; then echo "unknown option: $APPLY (use --apply to write)" >&2 exit 2 fi STAMP="$(date +%Y%m%d-%H%M%S)" BACKUP="$VAULT/.pack-backup/$STAMP" MANAGED=( ".opencode/languages.yaml" "docs/SCHEMA.md" "start.sh" ".opencode/opencode.json" # created only if missing ) for d in ".opencode/agent" ".opencode/command"; do for f in "$PACK/$d"/*; do [ -e "$f" ] && MANAGED+=("$d/$(basename "$f")") done done md5of() { [ -f "$1" ] && md5 -q "$1" || echo "-"; } mode="DRY RUN (nothing will be written — add --apply)" [ "$APPLY" = "--apply" ] && mode="APPLY" echo "vault : $VAULT" echo "pack : $PACK" echo "mode : $mode" echo same=0; changed=0; missing=0; skipped=0 for rel in "${MANAGED[@]}"; do src="$PACK/$rel"; dst="$VAULT/$rel" # never clobber an existing instance config if [ "$rel" = ".opencode/opencode.json" ] && [ -f "$dst" ]; then echo " keep $rel (vault's own config preserved)" skipped=$((skipped+1)); continue fi if [ ! -f "$dst" ]; then state="new"; missing=$((missing+1)) elif [ "$(md5of "$src")" = "$(md5of "$dst")" ]; then state="same"; same=$((same+1)) else state="UPDATE"; changed=$((changed+1)) fi printf " %-7s %s\n" "$state" "$rel" if [ "$APPLY" = "--apply" ] && [ "$state" != "same" ]; then if [ -f "$dst" ]; then mkdir -p "$(dirname "$BACKUP/$rel")" cp -p "$dst" "$BACKUP/$rel" fi mkdir -p "$(dirname "$dst")" cp -p "$src" "$dst" fi done echo echo "identical: $same to update: $changed missing: $missing kept local: $skipped" if [ "$APPLY" = "--apply" ]; then [ -d "$BACKUP" ] && echo "replaced files backed up to: $BACKUP" echo "verify with: opencode agent list (in $VAULT)" else echo "run again with --apply to write these changes" fi