#!/usr/bin/env bash
#
# Update BibTeX entries for IETF documents (RFCs and Internet Drafts)
# Copyright (C) 2014-2025 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com
#

REFERENCES=~/src/papers/Referenzarchiv.bib
DOCUMENTS="$(grep -E "^@[a-zA-Z]*{[ \t].*,.*$" "${REFERENCES}" | sed -e "s/,//g" | awk '{ print $2; }' | grep -E "^RFC|^draft")"
UPDATES_FILE="updated.bib"
RESULTS_FILE="results.bib"
PARALLEL=16

BIBTEXCONV_OPTIONS="--check-urls"
IETF2BIBTEX_OPTIONS="--quiet"

if [ "$1" == "--test" ] ; then
   BIBTEXCONV_OPTIONS=""    # Do no check URLs
   IETF2BIBTEX_OPTIONS=""   # Verbose mode
   echo "!!! Test Mode !!!"
fi

echo "Step 1: Fetching metadata ..."
echo "${DOCUMENTS}" | (
   while read -r name ; do
      rm -f "${name}".bib
      if [[ "${name}" =~ ^(RFC[0-9]+$)|(draft-[a-z].*$) ]] ; then
         # echo >&2 "Processing ${name} ..."
         echo "./ietf2bibtex ${IETF2BIBTEX_OPTIONS} \"${name}\" >${name}.bib"
      else
         echo >&2 "Skipping ${name}"
      fi
   done
) | parallel -j${PARALLEL}
wait


echo "Step 2: Fetching documents ..."
echo "${DOCUMENTS}" | (
   while read -r name ; do
      if [ -e "${name}".bib ] ; then
         if [ "$(stat --format="%s" "${name}".bib)" != "0" ] ; then
            echo "./bibtexconv ${name}.bib --export-to-bibtex ${name}.bib2 ${BIBTEXCONV_OPTIONS} --only-check-new-urls --non-interactive || echo \"ERROR: ${name}.bib seems to be invalid!\""
         else
            echo >&2 "NOTE: Removing empty file ${name}.bib"
            rm -f "${name}".bib
         fi
      fi
   done
) | parallel -j${PARALLEL}
wait


echo "Step 3: Collecting results ..."
(
   cat "${REFERENCES}"
   echo "${DOCUMENTS}" | (
      while read -r name ; do
         if [ -e "${name}".bib2 ] ; then
            cat "${name}".bib2
         fi
         rm -f "${name}".bib "${name}".bib2
      done
   )
)  >"${UPDATES_FILE}"
./bibtexconv "${UPDATES_FILE}" --export-to-bibtex "${RESULTS_FILE}" --non-interactive
echo "Done."


echo "Step 4: Showing differences (${REFERENCES} ${RESULTS_FILE}) ..."
ls -l "${REFERENCES}" "${RESULTS_FILE}"
diff "${REFERENCES}" "${RESULTS_FILE}" | grep -v url.checked | grep -v -E '^[[:alnum:]]+' | grep -v "^---$"| grep -v "[ \t]*url[.a-z0-9]* ="
diff --color ${REFERENCES} "${RESULTS_FILE}" |grep author|grep -B1 '>'
