#!/bin/bash
# $Id: bibtexconv-odt 868 2011-01-02 13:47:32Z dreibh $
#
# Copyright (C) 2010-2011 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`

RESULT=1

bibtexconv $BIBTEX_FILE <$EXPORT_SCRIPT >content.xml && \
cp "$TEMPLATE_ODT" "$TEMP_ZIP"
zip "$TEMP_ZIP" --out "$OUTPUT_ODT" -x content.xml && \
zip "$OUTPUT_ODT" content.xml && \
RESULT=0

rm -f content.xml "$TEMP_ZIP"

exit $RESULT
