#! /bin/sh
#+
#
# ===============
# genaeres_rtf.sh
# ===============
#
# SYNOPSIS
# ========
#
# ::
#
#   genaeres_rtf.sh
#
# DESCRIPTION
# ===========
#
# aeres 2007-2012 pour inclusion dans document word
#
# EXAMPLES
# ========
#
# ::
#
#   genaeres_rtf.sh
#
# SEE ALSO
# ========
#
# genbib.sh
#
# genaeres.sh
#
# TODO
# ====
#
# log
#
# phybiocar theme
#
# look for missing values (aerestype and aeresteam)
#
# EVOLUTIONS
# ==========
#
# - fplod 20120629T154955Z cratos (Linux)
#
#   * reorganize to deal all aeres type
#
# - fplod 20120628T101424Z cratos (Linux)
#
#   * creation
#
#-
set -u
command=$(basename ${0})
#
system=$(uname)
case "${system}" in
   AIX|IRIX64)
      echo "${command} : www : no specific posix checking"
   ;;
   *)
      set -o posix
   ;;
esac
#
usage=" Usage : ${command}"
#
# test if bibtool available
tool=bibtool
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
   echo "${command} : eee : ${tool} unavailable" >&2
   exit 1
fi
unset tool
unset status
#
# default
# n.a.
#
hostname=$(hostname)
tmpdir=${PROJECT_LOG}/gtbiblio/aeres2007_2012rtf/
dirwww=${PROJECT_OD}/public_html/gtbiblio/aeres2007_2012rtf/
rm -rf ${tmpdir} 2> /dev/null
mkdir -p ${tmpdir}
rm -rf ${dirwww} 2> /dev/null
mkdir -p ${dirwww}
#
. define_teams_aeres2007_2012.sh
. define_phybiocar_theme.sh
. jabref_rtf.sh
. jabref_header.sh
#
# define bibliography reference file
biblioref_orig=${PROJECT_ID}/biblioref.bib
biblioreffull=${tmpdir}/all.bib
cp ${biblioref_orig} ${biblioreffull}
#
list_year="2007 2008 2009 2010 2011 2012"
#
# loop on teams and years to build bibliography reference file
list_fteamyear=""
iteam=1
nbteam=${#list_team_bibtool[@]}
while [ ${iteam} -le ${nbteam} ]
do
    echo "${LINENO} : iii: team = ${list_team_bibtool[${iteam}]}"
    fteam=${tmpdir}/${list_team_title[${iteam}]}.bib
    rm ${fteam} 2> /dev/null
    bibtool_command="bibtool -- 'select={aeresteam \"${list_team_bibtool[${iteam}]}\"}' ${biblioreffull} -o ${fteam}"
    eval ${bibtool_command}
    unset bibtool_command
    nbatteam=$(grep -c @ ${fteam})
    if [ ${nbatteam} -ge 2 ]
    then
        echo "iii : $((${nbatteam} - 1)) ref for aeresteam ${list_team_title[${iteam}]}"
        for iyear in ${list_year}
        do
           echo "${LINENO} : iii: year = ${iyear}"
           fteamyear=${tmpdir}/$(basename ${fteam} .bib)_${iyear}.bib
           rm  ${fteamyear} 2> /dev/null
           bibtool_command="bibtool -- 'select={year \"${iyear}\"}' ${fteam} -o ${fteamyear}"
           eval ${bibtool_command}
	   unset bibtool_command
           # test if empty
           nbatteamyear=$(grep -c @ ${fteamyear})
           if [ ${nbatteamyear} -ge 2 ]
           then
               echo "iii : $((${nbatteamyear} - 1)) ref for aeresteam ${list_team_title[${iteam}]} and year ${iyear}"
               list_fteamyear="${list_fteamyear} ${fteamyear}"
           else
               echo "www : no ref for aeresteam ${list_team_title[${iteam}]} and year ${iyear}"
           fi
           unset nbatteamyear
       done # end of year loop
       unset iyear
    else
       echo "www : no ref for aeresteam ${list_team_title[${iteam}]}"
    fi
    unset nbatteam
    unset nbatteamyear
    iteam=$(( ${iteam} + 1 ))
done # end of team loop
unset iteam
unset nbteam
unset list_year
unset fteam
#
echo "${LINENO}: iii: list_fteamyear = ${list_fteamyear}"
#
# define jabref header
# ++ usefull
jabref_header ${tmpdir}/header_jabref
#
# loop on aerestype
list_aerestype="ACL ACLN ASCL BRE C-INV C-ACTI C-ATCN C-COM C-AFF DO OR OS PV PAT PT AP"
for aerestype in ${list_aerestype}
do
   echo "${LINENO} : iii: aerestype = ${aerestype}"
   for fteamyear in ${list_fteamyear}
   do
      echo "${LINENO} : iii: fteamyear = ${fteamyear}"
      #++ add header
      fteamyeartype=${tmpdir}/$(basename ${fteamyear} .bib)_${aerestype}.bib
      rm ${fteamyeartype} 2> /dev/null
      bibtool_command="bibtool -- 'select={aerestype \"${aerestype}\"}' ${fteamyear} -o ${fteamyeartype}"
      eval ${bibtool_command}
      unset bibtool_command
      # test if empty
      nbatteamyeartype=$(grep -c @ ${fteamyeartype})
      if [ ${nbatteamyeartype} -ge 2 ]
      then
          echo "iii : $((${nbatteamyeartype} - 1)) ref type ${aerestype} in ${fteamyear}"
          ofteamyeartype=${dirwww}/$(basename ${fteamyeartype} .bib).rtf
          rm ${ofteamyeartype} 2> /dev/null
          jabref_rtf \
	  ${PROJECT}/../branches/bibliolocean/src/jabref.preferences.xml \
	  ${fteamyeartype} ${ofteamyeartype}
          # ++ test si ok
      else
          echo "www : no ref type ${aerestype} in ${fteamyear}"
      fi
      unset nbatteamyeartype
   done # end of loop on fteamyear
done # end of loop on aerestype
#
# clean
#++rm ${biblioref} 2> /dev/null
#++rm ${tmpdir}/header_jabref 2> /dev/null
#
# end
exit 0
