export-notes/export-notes.sh
2025-05-15 14:48:26 +03:00

205 lines
5.2 KiB
Bash
Executable file

#!/bin/bash
# ----------------------------
# Configurable Section
# ----------------------------
#
#
# ----------------------------
# Default Values
# ----------------------------
SOURCE_BASE="$HOME"
NOTES_ROOT="notes/courses"
DEST_REL=""
SECTION_DIR=""
SHARE_REL=""
DEBUG=0
# ----------------------------
# Help Message
# ----------------------------
print_help() {
cat <<EOF
Usage: $0 [options]
Options:
--source-base PATH Base directory where notes root begins (default: \$HOME)
--notes-root PATH Path from base to notes root (default: notes/courses)
--section-dir PATH Relative path under notes root to export (default: current directory)
--dest-rel PATH Relative path under the destination root to export
--share-rel PATH Relative path (from section-dir) to a shared _share directory (e.g. ../_share)
If provided, its contents will be copied to the DEST/_share folder (as a sibling)
--debug Print resolved paths and stop before exporting
--help Show this help message
Behavior:
- .org files are exported only if newer than corresponding .html files.
- Shared assets from both --share-rel and section-local _share/ are merged into the destination.
- Symlinks are preserved; .bak, .log, and dotfiles are excluded from rsync copies.
Examples:
cd ~/notes/courses/business/music_money_makeover
$0 --debug
$0 \\
--source-base ~/notes \\
--notes-root notes/courses \\
--section-dir business/music_money_makeover \\
--share-rel ../_share
EOF
}
# ----------------------------
# Flag Parsing
# ----------------------------
while [[ "$#" -gt 0 ]]; do
case "$1" in
--source-base)
SOURCE_BASE="$2"
shift 2
;;
--notes-root)
NOTES_ROOT="$2"
shift 2
;;
--section-dir)
SECTION_DIR="$2"
shift 2
;;
--share-rel)
SHARE_REL="$2"
shift 2
;;
--debug)
DEBUG=1
shift
;;
--help)
print_help
exit 0
;;
*)
echo "❌ Unknown option: $1"
print_help
exit 1
;;
esac
done
# Auto-determine SECTION_DIR if not provided
if [ -z "$SECTION_DIR" ]; then
full_path="$(cd "$(pwd)" && pwd)"
base_path="$(cd "$SOURCE_BASE/$NOTES_ROOT" && pwd)"
SECTION_DIR="${full_path#"$base_path"/}"
fi
# ----------------------------
# Path Setup
# ----------------------------
SRC_DIR="$SOURCE_BASE/$NOTES_ROOT/$SECTION_DIR"
SHARE_DIR="$SRC_DIR/_share"
DATA_DIR="$SRC_DIR/_data"
# Detect OS and use appropriate destination base
if [[ "$OSTYPE" == "darwin"* ]]; then
DEST_ROOT="/usr/local/var/www"
else
DEST_ROOT="/var/www"
fi
DEST_DIR="$DEST_ROOT/$NOTES_ROOT/$SECTION_DIR"
# ----------------------------
# Debug Output
# ----------------------------
echo "📁 SOURCE_BASE: $SOURCE_BASE"
echo "📁 NOTES_ROOT: $NOTES_ROOT"
echo "📁 SECTION_DIR: $SECTION_DIR"
echo "📁 SRC_DIR: $SRC_DIR"
echo "📁 DEST_DIR: $DEST_DIR"
echo "📁 SHARE_DIR: $SHARE_DIR"
echo "📁 DATA_DIR: $DATA_DIR"
if [ "$DEBUG" -eq 1 ]; then
echo "🛑 Debug mode enabled. Exiting before export."
exit 0
fi
# ----------------------------
# Export .org to .html if changed
# ----------------------------
mkdir -p "$DEST_DIR"
find "$SRC_DIR" -name '*.org' ! -path "*/_share/*" | while read -r file; do
rel_path="${file#$SRC_DIR/}"
rel_dir="$(dirname "$rel_path")"
dest_subdir="$DEST_DIR/$rel_dir"
output_file="$dest_subdir/$(basename "${file%.org}.html")"
if [ ! -f "$output_file" ] || [ "$file" -nt "$output_file" ]; then
echo "Exporting $rel_path"
mkdir -p "$dest_subdir"
emacs "$file" --batch -l org --eval="(org-export-to-file 'html \"$output_file\")"
else
echo "Skipping $rel_path (no changes)"
fi
done
# ----------------------------
# Copy shared assets (_share), preserving symlinks but skipping junk files
# ----------------------------
#
# --- Shared assets from --share-rel (e.g. ../_share → sibling)
if [ -n "$SHARE_REL" ]; then
SHARE_SRC="$(cd "$SRC_DIR/$SHARE_REL" && pwd)"
DEST_SHARE="$(dirname "$DEST_DIR")/_share"
if [ -d "$SHARE_SRC" ]; then
echo "📦 Copying shared assets from $SHARE_SRC to $DEST_SHARE"
mkdir -p "$DEST_SHARE"
rsync -a --update --keep-dirlinks \
--exclude=".*" \
--exclude="*.bak" \
--exclude="*.log" \
"$SHARE_SRC/" "$DEST_SHARE/"
else
echo "⚠️ Warning: resolved --share-rel '$SHARE_REL' to '$SHARE_SRC', but it does not exist"
fi
fi
# --- Fallback to local _share (section-specific)
if [ -d "$SHARE_DIR" ]; then
mkdir -p "$DEST_DIR/_share"
rsync -a --update --keep-dirlinks \
--exclude=".*" \
--exclude="*.bak" \
--exclude="*.log" \
"$SHARE_DIR/" "$DEST_DIR/_share/"
fi
# ----------------------------
# Copy course-specific _data directory (if it exists)
# ----------------------------
if [ -d "$DATA_DIR" ]; then
mkdir -p "$DEST_DIR/_data"
rsync -a --update --keep-dirlinks \
--exclude=".*" \
--exclude="*.bak" \
--exclude="*.log" \
"$DATA_DIR/" "$DEST_DIR/_data/"
fi
echo "✅ Export complete for $SECTION_DIR$DEST_DIR"