diff options
| author | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2026-06-06 12:02:41 -0400 |
|---|---|---|
| committer | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2026-06-06 12:02:41 -0400 |
| commit | eabf1f6d74dac497ce31e3e2f441cfa25e9f74f2 (patch) | |
| tree | 626d64c3574cfbc7cc38eae6d142ef22b21cf59b /bib-add | |
| parent | 8351a1da3f56cde9939b934bc5533a95aff1c95e (diff) | |
| download | bibutils-eabf1f6d74dac497ce31e3e2f441cfa25e9f74f2.tar.gz | |
Initial implementation (only a few years later!)
This is pure Claude. I'd written out the plan for
this suite of scripts eons ago, but never found the
time to actual do it. Remembered it this morning,
pointed Claude at the README, and had something
that appears to work in minutes.
caveat emptor: the design is mine, but the code is
purely LLM generated at this point.
Diffstat (limited to 'bib-add')
| -rwxr-xr-x | bib-add | 67 |
1 files changed, 67 insertions, 0 deletions
@@ -0,0 +1,67 @@ +#!/bin/sh +# bib-add - insert bibtex entries from stdin into a database file +# +# usage: bib-add [-f] db.bib < entry +# -f replace existing entries with the same key + +usage() { + printf 'usage: bib-add [-f] db.bib < entry\n' >&2 + exit 2 +} + +if [ -n "$BIBUTILS_LIB" ]; then + LIB=$BIBUTILS_LIB +elif [ -d "$(dirname "$0")/lib" ]; then + LIB=$(dirname "$0")/lib +else + LIB=/usr/local/share/bibutils +fi + +force=0 +while getopts f opt; do + case $opt in + f) force=1 ;; + *) usage ;; + esac +done +shift $((OPTIND - 1)) +[ $# -eq 1 ] || usage +db=$1 + +tmp=$(mktemp) && tmpkeys=$(mktemp) && tmpdb=$(mktemp) || exit 1 +trap 'rm -f "$tmp" "$tmpkeys" "$tmpdb"' EXIT INT TERM + +# canonicalize the incoming entries +awk -f "$LIB/bib-parse.awk" -f "$LIB/bib-canon.awk" -f "$LIB/bib-select.awk" \ + -v keys= -v invert=1 > "$tmp" + +if [ ! -s "$tmp" ]; then + printf 'bib-add: no entries on stdin\n' >&2 + exit 1 +fi + +awk -f "$LIB/bib-parse.awk" -f "$LIB/bib-lskeys.awk" "$tmp" > "$tmpkeys" + +if [ -f "$db" ]; then + dups=$(awk -f "$LIB/bib-parse.awk" -f "$LIB/bib-lskeys.awk" "$db" \ + | grep -Fxf "$tmpkeys") || dups= + if [ -n "$dups" ]; then + if [ "$force" -eq 1 ]; then + # rewrite the database without the entries being replaced + keys=$(printf '%s\n' "$dups" | paste -sd, -) + awk -f "$LIB/bib-parse.awk" -f "$LIB/bib-canon.awk" \ + -f "$LIB/bib-select.awk" -v keys="$keys" -v invert=1 \ + "$db" > "$tmpdb" || exit 1 + cp "$tmpdb" "$db" + else + printf 'bib-add: duplicate keys in %s:\n' "$db" >&2 + printf '%s\n' "$dups" >&2 + exit 1 + fi + fi +fi + +{ + [ -s "$db" ] && echo "" + cat "$tmp" +} >> "$db" |