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

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


num_before=$(biblehal-count)

mkdir -p /var/cache/biblehal/upload

for bibfqname in /var/cache/biblehal/explode/hal-*.bib
do
   bibfile=$(basename $bibfqname)
   if [ -e /var/cache/biblehal/upload/$bibfile ]
   then
      if cmp $bibfqname /var/cache/biblehal/upload/$bibfile
      then
         echo "Already have $bibfqname"
         continue
      fi
      echo "Delete ref $bibfile on server refbase"
      biblehal-clean $bibfile
      num_before=$(( $num_before - 1 ))
   fi

   echo "Cache file $bibfqname"
   cp $bibfqname /var/cache/biblehal/upload/
   
   echo "Push file $bibfqname - Counter at $num_before"
   biblehal-push $bibfqname
   num_after=$(biblehal-count)
   if [[ $(( $num_after - $num_before )) == 1 ]]
   then
      num_before=$num_after
   else
      echo "Error on file $bibfqname (number of ref before $num_before / after $num_after)"
      echo "Cleaning bib $bibfile on server refbase"
      biblehal-clean $bibfile
      num_before=$(biblehal-count)
      continue
   fi
done
