#!/usr/bin/env bash
# Tangle an org file using Emacs batch mode
# Usage: tangle <org-file>

set -e

if [ $# -ne 1 ]; then
    echo "Usage: tangle <org-file>"
    echo "  Tangles all src blocks with :tangle directives to their targets"
    exit 1
fi

ORG_FILE="$1"

if [ ! -f "$ORG_FILE" ]; then
    echo "Error: File not found: $ORG_FILE"
    exit 1
fi

echo "Tangling: $ORG_FILE"

emacs --batch \
    --load org \
    --eval "(setq org-confirm-babel-evaluate nil)" \
    --eval "(org-babel-tangle-file \"$ORG_FILE\")"

echo "Done."
