diff options
Diffstat (limited to '.ai/scripts/flashcard-sync')
| -rwxr-xr-x | .ai/scripts/flashcard-sync | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/.ai/scripts/flashcard-sync b/.ai/scripts/flashcard-sync new file mode 100755 index 0000000..f5ba7fb --- /dev/null +++ b/.ai/scripts/flashcard-sync @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +# flashcard-sync: stats check + regenerate Anki apkg + place at ~/sync/phone/anki/ +# +# Wraps flashcard-stats.py + flashcard-to-anki.py (and optionally +# flashcard-diff-ids.py) for the canonical "rewrote the deck, now ship +# it" step in the flashcard-review workflow. +# +# Usage: +# flashcard-sync <source.org> +# flashcard-sync <source.org> --diff-against <previous-version.org> +# +# Exits non-zero when the stats check warns, when --diff-against shows +# any disappeared / appeared IDs, or when flashcard-to-anki.py fails. The +# Anki apkg is not written when any gate fails. + +set -euo pipefail + +usage() { + cat >&2 <<'EOF' +usage: flashcard-sync <source.org> [--diff-against <previous-version.org>] +EOF + exit 2 +} + +if [[ $# -lt 1 ]]; then + usage +fi + +SOURCE="$1" +shift + +DIFF_AGAINST="" +while [[ $# -gt 0 ]]; do + case "$1" in + --diff-against) + [[ $# -ge 2 ]] || usage + DIFF_AGAINST="$2" + shift 2 + ;; + -h|--help) + usage + ;; + *) + echo "unknown arg: $1" >&2 + usage + ;; + esac +done + +if [[ ! -f "$SOURCE" ]]; then + echo "error: $SOURCE not found" >&2 + exit 2 +fi + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +STATS="$SCRIPT_DIR/flashcard-stats.py" +DIFF_IDS="$SCRIPT_DIR/flashcard-diff-ids.py" +TO_ANKI="$SCRIPT_DIR/flashcard-to-anki.py" + +for helper in "$STATS" "$DIFF_IDS" "$TO_ANKI"; do + if [[ ! -f "$helper" ]]; then + echo "error: helper $helper not found" >&2 + exit 2 + fi +done + +echo "=== flashcard-sync: $SOURCE ===" +echo +echo "--- stats ---" +if ! python3 "$STATS" "$SOURCE"; then + echo + echo "stats check failed — fix warnings before sync, or call flashcard-to-anki.py directly to override" >&2 + exit 1 +fi +echo + +if [[ -n "$DIFF_AGAINST" ]]; then + if [[ ! -f "$DIFF_AGAINST" ]]; then + echo "error: $DIFF_AGAINST not found" >&2 + exit 2 + fi + echo "--- ID preservation ---" + if ! python3 "$DIFF_IDS" "$DIFF_AGAINST" "$SOURCE"; then + echo + echo "ID preservation check failed — SRS state may have been lost" >&2 + exit 1 + fi + echo +fi + +BASENAME="$(basename "$SOURCE" .org)" +OUTPUT="$HOME/sync/phone/anki/${BASENAME}.apkg" + +echo "--- regenerate apkg ---" +mkdir -p "$(dirname "$OUTPUT")" +"$TO_ANKI" "$SOURCE" --output "$OUTPUT" +echo +echo "deck synced to $OUTPUT" |
