#!/bin/sh
#
# hbibtex
#
# converts hexadecimal notation of TeX's .aux file to Extended Unix Code.
# runs bibtex on this .aux file to create .bbl file.
# use this script to get .bbl file, when using .bib file containing
#  8bit characters as label string for bibitem.
#

if test "$1" = "-v" ; then
  verbose="$1" ;
  shift;
else
  verbose="" ;
fi

if test "$1/" = "/" ; then
  echo "Usage: $0 [-v] filename";
  exit 1;
fi

auxfile=$1
tmpaux=$1.tmp

if test -f ${auxfile}.aux ; then
  true;
else
  echo "file ${auxfile}.aux doesn't exist.";
  exit 1;
fi

awk '\
BEGIN { \
  FS="\^"; \
  OFS=""; \
  hex="123456789abcdef"; \
} \
{ printf("%s", $1); \
  for ( i=2 ; i<=NF ; i++ ) { \
    if ( $(i-1) == "" ) \
      printf("%c%s",index(hex,substr($i,1,1))*16+index(hex,substr($i,2,1)), \
	     substr($i,3)); \
    else printf("%s",$i); \
  } \
  printf("\n"); \
}' $1.aux > ${tmpaux}

mv -f ${tmpaux} ${auxfile}.aux
bibtex ${verbose} ${auxfile}
