#!/bin/bash
#
# gabriel 2010/06/03

export PATH="/bin:/usr/bin:/usr/lib/biblehal"


year=$1; shift

if [ ! -e /var/cache/biblehal/download/hal-extract-auto-${year}.bib ]
then
   echo "Usage biblehal-explode year"
   echo " File /var/cache/biblehal/download/hal-extract-auto-${year}.bib must exist"
   exit 1
fi

mkdir -p /var/cache/biblehal/explode

index=0
while read -r line ; do
   echo ${line} | egrep '^%% .*-[[:digit:]]+, version' > /dev/null
   if [[ $? -eq 0 ]]
   then
      index=$(echo ${line} | awk '{print $2}' | cut -f 1 -d ',')
      [ -e /var/cache/biblehal/explode/hal-${index}.bib ] && rm /var/cache/biblehal/explode/hal-${index}.bib
   fi
   echo -E ${line} >> /var/cache/biblehal/explode/hal-${index}.bib
done < /var/cache/biblehal/download/hal-extract-auto-${year}.bib

rm /var/cache/biblehal/explode/hal-0.bib
