#!/bin/bash
# $Id: bibtexconv-odt 1342 2012-02-10 18:34:09Z dreibh $
#
# Copyright (C) 2010-2012 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: dreibh@iem.uni-due.de

if [ $# -ne 4 ] ; then
   echo >&2 "Usage: $0 [Template ODT] [Output ODT] [BibTeX file] [Export script]"
   exit 1
fi

TEMPLATE_ODT="$1"
OUTPUT_ODT="$2"
BIBTEX_FILE="$3"
EXPORT_SCRIPT="$4"
TEMP_ZIP=`mktemp -d`

RESULT=1

bibtexconv $BIBTEX_FILE <$EXPORT_SCRIPT >$TEMP_ZIP/content.xml && \
cp "$TEMPLATE_ODT" $TEMP_ZIP/template.odt && \
( cd $TEMP_ZIP && \
  zip template.odt --out output.odt -x content.xml && \
  zip output.odt content.xml ) && \
RESULT=0 && \
mv $TEMP_ZIP/output.odt "$OUTPUT_ODT"

rm -rf $TEMP_ZIP


exit $RESULT
