#! /bin/sh -e

# $Id: lr_xml2dbx.in,v 1.5 2002/04/13 22:55:16 vanbaal Exp $

#
# Copyright (C) 2000-2002 Stichting LogReport Foundation LogReport@LogReport.org
# 
#     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 2 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 (see COPYING); if not, check with
#     http://www.gnu.org/copyleft/gpl.html or write to the Free Software 
#     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
#

PROGRAM=lr_xml2dbx

# dereference sysconfdir's prefix dependency
prefix="/usr/local"
etcdir="${prefix}/etc/lire"
 
. $etcdir/profile_lean

tag="all all ${LR_ID:-UNSET} $PROGRAM"

echo >&2 "$tag info started with $*"

if test $# -lt 1
then
  echo >&2 "$tag err expecting argument. syntax: $PROGRAM [-i] report.xml"
  exit 1
fi

images=
while getopts :i a
do
    case $a in
    i)
        images="-i"
        ;;
    *) 
        echo >&2 "$tag err usage $PROGRAM [-i] report.xml"
        exit 1
        ;; 
    esac
done
shift `expr $OPTIND - 1 || true`

file="$1"

if test ! -r "$file"
then
  echo >&2 "$tag err file '$file' is not readable."
  exit 1
fi

lr_check_prereq $images docbookx || exit 1

# get global vars
prefix="/usr/local"
datadir="${prefix}/share/lire"
etcdir="${prefix}/etc/lire"
exec_prefix="${prefix}"
libexecdir="${exec_prefix}/libexec/lire"
PACKAGE="lire"
. $etcdir/defaults

DOCBOOKXSL="$datadir/xml/stylesheet/xsl/docbook.xsl"

if test ! -d "$TMPDIR"
then
    echo >&2 "$tag notice dir $TMPDIR does not exist, creating it"
    if mkdir "$TMPDIR"
    then
        :
    else
        echo >&2 "$tag err cannot create $TMPDIR, exiting!"
        exit 1
    fi
fi

tmpreportdir="$TMPDIR/$PROGRAM.$$"
echo >&2 "$tag debug creating temporary report dir $tmpreportdir"
if mkdir $tmpreportdir
then 
    :
else
    echo >&2 "$tag err failed to create $tmpreportdir"
    exit 1
fi

xmlfile="$TMPDIR/$PROGRAM.all.$$.xml"
if test -n "$images"
then
    lr_xml2png -o $tmpreportdir < $file > $xmlfile
else 
    cp $file $xmlfile
fi

# convert lire xml to dbx
dbxfile="$tmpreportdir/$PROGRAM.all.$$.dbx"
echo >&2 "$tag info converting $file to $dbxfile"
lr_xslt $DOCBOOKXSL $xmlfile "target-user='${LR_TARGET_USER:-sysadmin}'" "userlevel='${LR_USERLEVEL:-normal}'" > $dbxfile

# Give output
oldpwd="`pwd`"
cd $tmpreportdir

if test -n "$images"
then
    mkdir report
    reportdbx=$tmpreportdir/report/report.dbx
    mv $dbxfile $reportdbx
    if ls -- *.png > /dev/null 2>&1
    then
        # png's were created
        mv *.png report/
    else
        echo >&2 "$tag warning no images were created. i'll go ahead anyway."
    fi
    $LR_TAR cf - report
else
    cat $dbxfile
fi
cd $oldpwd

# clean temp
if test -n "$LR_KEEP"
then
    echo >&2 "$tag notice keeping temporary files in $tmpreportdir on your request. remove manually"
else
    rm -fr $tmpreportdir $xmlfile
fi

echo >&2 "$tag info stopped"

