#! /bin/sh -e

# $Id: lr_check_prereq.in,v 1.2 2002/02/12 17:40:05 flacoste Exp $

#
# Copyright (C) 2001 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_check_prereq

tag="${SUPERSERVICE:-all} ${SERVICE:-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] format"
  exit 1
fi

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

format="$1"

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

case $format in
    xml|txt)
	required=""
	;;
    pdf)
	required="xslt jade jadetex docbook-dtd docbook-dsssl"
	;;
    html)
	required="xslt docbook-dtd docbook-xsl-html tar"
	;;
    docbookx)
	required="xslt"
	;;
    xhtml)
	required="xslt docbook-dtd docbook-xsl-xhtml tar"
	;;
    rtf)
	required="xslt jade docbook-dtd docbook-dsssl"
        ;;
    logml)
	required="xslt"
	;;
    *)
        echo >&2 "$tag err format $format is not supported"
        exit 1
        ;;
esac

if test -n "$images"
then
    case $format in
    pdf|html|xhtml|docbookx)
	required="$required gd tar"
	;;
    rtf)
	required="$required gd zip"
	;;
    *)
        echo >&2 "$tag err format $format doesn't support images"
        exit 1
	;;
    esac
fi

if test -z "$required"
then
    echo >&2 "$tag info no specific requirements"
    exit 0
fi
echo >&2 "$tag info required for $format: $required"

for r in $required
do
    
    case $r in
    xslt)
	if test "x$XSLT_PROCESSOR" = "xnone" 
	then
	    cat >&2 <<EOF
$tag err no XSLT processor available!
$tag err Please install the Gnome XSLT Library available at http://xmlsoft.org/XSLT/
$tag err Consult Lire User's Manual for more informations.
EOF
	    #' Fontification hack
	    exit 1
	fi
	;;
    jade)
	if test -z "$JADE"
	then
	    cat >&2 <<EOF
$tag err Jade or Openjade don't seem to be installed.
$tag err Please install jade or openjade
$tag err Consult the Lire User's Manual for more informations.
EOF
	    exit 1
	fi
	if test ! -x "$JADE"
	then
	    echo >&2 "$tag err program '$JADE' is  not executable!"
	    exit 1
	fi
	;;
    jadetex)
	if test -z "$PDFJADETEX"
	then
	    cat >&2 <<EOF
$tag err Jadetex doesn't seem to be installed.
$tag err Please install jadetex.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    exit 1
	fi
	if  test ! -x "$PDFJADETEX"
	then
	    echo >&2 "$tag err program '$PDFJADETEX' is not executable!"
	    exit 1
	fi
	;;
    gd)
	if /usr/bin/perl -MGD::Graph -e 'exit 0' 2>/dev/null
	then
	    :
	else
	    cat >&2 <<EOF
$tag err requested images cannot be generated: missing GD::Graph perl module.
$tag err Please install the GD::Graph perl modules from a local CPAN mirror.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    #' Fontification hack
	    exit 1
	fi
	;;
    docbook-dtd)
	if test -z "$DBK_XML_DTD"
	then
	    cat >&2 <<EOF
$tag err The DocBook XML V4.1.2 DTD doesn't seem to be installed.
$tag err Please install the DocBook XML V4.1.2 DTD.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    #' Fontification hack
	    exit 1
	fi
	if test ! -f "$DBK_XML_DTD"
	then
	    cat >&2 <<EOF
$tag err Please install the DocBook XML V4.1.2 DTD.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    #' Fontification hack
	    exit 1
	fi
	;;
    docbook-dsssl)
	if test -z "$DBK_PRINT_STYLESHEET"
	then
	    cat >&2 <<EOF
$tag err The DocBook DSSSL stylesheets don't seem to be installed.
$tag err Please install Norman Walsh's DocBook DSSSL Stylesheets.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    #' Fontification hack
	    exit 1
	fi
	if test ! -f "$DBK_PRINT_STYLESHEET"
	then
	    cat >&2 <<EOF
$tag err docbook.dsl file '$DBK_PRINT_STYLESHEET' does not exist!
$tag err Please install Norman Walsh's DocBook DSSSL Stylesheets.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    exit 1
	fi
	;;
    docbook-xsl-html)
	if test -z "$DBK_HTML_XSL"
	then
	    cat >&2 <<EOF
$tag err The DocBook XSL stylesheets don't seem to be installed.
$tag err Please install Norman Walsh's DocBook XSL Stylesheets.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    #' Fontification hack
	    exit 1
	fi
	if test ! -f "$DBK_HTML_XSL"
	then
	    cat >&2 <<EOF
$tag err docbook.xsl file '$DBK_HTML_XSL' does not exist!
$tag err Please install Norman Walsh's DocBook XSL Stylesheets.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    exit 1
	fi
	;;
    docbook-xsl-xhtml)
	if test -z "$DBK_XHTML_XSL"
	then
	    cat >&2 <<EOF
$tag err The DocBook XSL stylesheets don't seem to be installed.
$tag err Please install Norman Walsh's DocBook XSL Stylesheets.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    #' Fontification hack
	    exit 1
	fi
	if test ! -f "$DBK_XHTML_XSL"
	then
	    cat >&2 <<EOF
$tag err docbook.xsl file '$DBK_XHTML_XSL' does not exist!
$tag err Please install Norman Walsh's DocBook XSL Stylesheets.
$tag err Consult the Lire User's Manual for more informations.
EOF
	    exit 1
	fi
	;;
    tar)
	if test "x$TAR" = "x" || test ! -x "$TAR"
	then
	    echo >&2 "$tag err can't generate images: tar ('$TAR') is not available!"
	    exit 1
	fi
	;;
    zip)
	if test "x$ZIP" = "x" || test ! -x "$ZIP"
	then
	    echo >&2 "$tag err can't generate images: zip ('$ZIP') is not available!"
	    exit 1
	fi
	;;
    esac
done

echo >&2 "$tag info stopped"

