#!/bin/sh
# Simple embed script: generates OCaml module from text files
# Usage: embed.sh -o output.ml file1.txt file2.txt ...

OUTPUT=""
FILES=""

while [ $# -gt 0 ]; do
    case "$1" in
        -o)
            OUTPUT="$2"
            shift 2
            ;;
        *)
            FILES="$FILES $1"
            shift
            ;;
    esac
done

if [ -z "$OUTPUT" ]; then
    echo "Usage: embed.sh -o output.ml file1.txt ..." >&2
    exit 1
fi

# Generate OCaml module
cat > "$OUTPUT" << 'HEADER'
(* Auto-generated by embed.sh *)

let files = [
HEADER

for f in $FILES; do
    if [ -f "$f" ]; then
        basename=$(basename "$f")
        content=$(cat "$f" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g')
        echo "  (\"$basename\", \"$content\");" >> "$OUTPUT"
    fi
done

cat >> "$OUTPUT" << 'FOOTER'
]

let get name =
  try Some (List.assoc name files)
  with Not_found -> None

let list () = List.map fst files
FOOTER

echo "Generated $OUTPUT with $(echo $FILES | wc -w) files"
