#! /bin/sh -e

# $Id: lr_check_prereq.in,v 1.18 2004/03/26 20:48:20 flacoste Exp $

PROGRAM=lr_check_prereq

tag="${LR_SUPERSERVICE:-all} ${LR_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=""
	;;
    excel95)
	required="excel"
	;;
    pdf)
	case $LR_PDF_STYLE in
	    fo)
		required="xslt xmltex docbook-dtd docbook-xsl"
		;;
	    *)
		required="xslt jade jadetex docbook-dtd docbook-dsssl"
		;;
	esac
	;;
    html|html_page|xhtml|xhtml_page)
	required="xslt docbook-dtd docbook-xsl tar"
	;;
    docbookx)
	required="xslt"
	;;
    *)
        echo >&2 "$tag err format $format is not supported"
        exit 1
        ;;
esac

if test -n "$images"
then
    case $format in
    pdf|html|html_page|xhtml|xhtml_page|docbookx)
	required="$required tar"
	case "$LR_IMAGE_STYLE" in
		gd) required="$required gd" ;;
		hq) required="$required ploticus gs" ;;
		*) required="$required ploticus"
	esac
	;;
    *)
        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$LR_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 information.
EOF
	    #' Fontification hack
	    exit 1
	fi
	;;
    jade)
	if test -z "$LR_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 information.
EOF
	    exit 1
	fi
	if test ! -x "$LR_JADE"
	then
	    echo >&2 "$tag err program '$LR_JADE' is  not executable!"
	    exit 1
	fi
	;;
    jadetex)
	if test -z "$LR_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 information.
EOF
	    exit 1
	fi
	if  test ! -x "$LR_PDFJADETEX"
	then
	    echo >&2 "$tag err program '$LR_PDFJADETEX' is not executable!"
	    exit 1
	fi
	;;
    xmltex)
	if test -z "$LR_PDFXMLTEX"
	then
	    cat >&2 <<EOF
$tag err PassiveTeX doesn't seem to be installed.
$tag err Please install pdfxmltex.
$tag err Consult the Lire User's Manual for more information.
EOF
	    exit 1
	fi
	if  test ! -x "$LR_PDFXMLTEX"
	then
	    echo >&2 "$tag err program '$LR_PDFJADETEX' is not executable!"
	    exit 1
	fi
	;;
    ploticus)
	if test -x "$LR_PLOTICUS" && "$LR_PLOTICUS" /dev/null >/dev/null 2>&1
	then
		:
	else
	    cat >&2 <<EOF
$tag err requested images cannot be generated: missing ploticus.
$tag err Please install ploticus (http://ploticus.sourceforge.net/)
$tag err Consult the Lire User's Manual for more information.
EOF
	    #' Fontification hack
		exit 1;
	fi
	;;
    gs)
	if test -x "$LR_GHOSTSCRIPT" && "$LR_GHOSTSCRIPT" -v >/dev/null 2>&1
	then
		:
	else
	    cat >&2 <<EOF
$tag err requested images cannot be generated: missing ghostscript.
$tag err Please install ghostscript (http://www.cs.wisc.edu/~ghost/)
$tag err Consult the Lire User's Manual for more information.
EOF
	    #' Fontification hack
		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 information.
EOF
	    #' Fontification hack
	    exit 1
	fi
	;;
    excel)
	if /usr/bin/perl -MSpreadsheet::WriteExcel -e 'exit 0' 2>/dev/null
	then
	    :
	else
	    cat >&2 <<EOF
$tag err Excel95 output format isn't available: missing Spreadsheet::WriteExcel perl module.
$tag err Please install the Spreadsheet::WriteExcel perl modules from a 
$tag err local CPAN mirror. Consult the Lire User's Manual for more 
$tag err information.
EOF
	    #' Fontification hack
	    exit 1
	fi
	;;
    docbook-dtd)
	if test -z "$LR_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 information.
EOF
	    #' Fontification hack
	    exit 1
	fi
	if test ! -f "$LR_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 information.
EOF
	    #' Fontification hack
	    exit 1
	fi
	;;
    docbook-dsssl)
	if test -z "$LR_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 information.
EOF
	    #' Fontification hack
	    exit 1
	fi
	if test ! -f "$LR_DBK_PRINT_STYLESHEET"
	then
	    cat >&2 <<EOF
$tag err docbook.dsl file '$LR_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 information.
EOF
	    exit 1
	fi
	;;
    docbook-xsl)
        vars=''
	test -z "$LR_DBK_HTML_XSL" && vars="$vars LR_DBK_HTML_XSL"
        test -z "$LR_DBK_HTML_ONEPAGE_XSL" && vars="$vars LR_DBK_HTML_ONEPAGE_XSL"
        test -z "$LR_DBK_XHTML_XSL" && vars="$vars LR_DBK_XHTML_XSL"
        test -z "$LR_DBK_XHTML_ONEPAGE_XSL" && vars="$vars LR_DBK_XHTML_ONEPAGE_XSL"
        test -z "$LR_DBK_FO_XSL" && vars="$vars LR_DBK_FO_XSL"
        if test -n "$vars"
	then
	    cat >&2 <<EOF
$tag err Variables $vars are unset: The DocBook XSL stylesheets
$tag err don't seem to be completely installed.
$tag err Please install Norman Walsh's DocBook XSL Stylesheets.
$tag err Consult the Lire User's Manual for more information.
EOF
	    #' Fontification hack
	    exit 1
	fi
        for var in LR_DBK_HTML_XSL LR_DBK_HTML_ONEPAGE_XSL LR_DBK_XHTML_XSL LR_DBK_XHTML_ONEPAGE_XSL
        do
            eval f=\$$var
            if test ! -f "$f"
            then
                cat >&2 <<EOF
$tag err Docbook XSL file '$f', as set in '$var' does not exist!
$tar err The DocBook XSL stylesheets don't seem to be completely installed.
$tag err Please install Norman Walsh's DocBook XSL Stylesheets.
$tag err Consult the Lire User's Manual for more information.
EOF
                exit 1
            fi
        done

        ;;
    tar)
	if test "x$LR_TAR" = "x" || test ! -x "$LR_TAR"
	then
	    echo >&2 "$tag err can't generate images: tar ('$LR_TAR') is not available!"
	    exit 1
	fi
	;;
    esac
done

echo >&2 "$tag info stopped"

exit 0

POD=<<'EOPOD'

=pod

=head1 NAME

lr_check_prereq - check Lire prerequisites

=head1 SYNOPSIS

B<lr_check_prereq> B<[-i]> I<format>

=head1 OPTIONS

=over 4

=item B<-i>

Check wether we're able to generate images, to display charts.

=back

=head1 DESCRIPTION

B<lr_check_prereq> tests wether software, needed for Lire to generated output
in I<format>, is available on the system.  For most tests, the results from
B<configure>, which was run before Lire was build, are used.  When the needed
software is not found on the system, B<lr_check_prereq> exits with 1.  If all
requirements are met, exit status is zero.

Supported I<format>'s are: xml, txt, excel95, pdf, html, html_page, docbookx,
xhtml, xhtml_page and rtf.

This script is called by lr_log2report(1), so that it's able to abort early,
when asked for an output format, not supported by the system.

Output on stderr is in the format expected by lr_run(1).

=head1 SEE ALSO

lr_log2report(1), documentation in the Lire User Manual

=head1 VERSION

$Id: lr_check_prereq.in,v 1.18 2004/03/26 20:48:20 flacoste Exp $

=head1 COPYRIGHT

Copyright (C) 2000, 2001, 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.

=head1 AUTHOR

Francis J. Lacoste <flacoste@logreport.org>

=cut

EOPOD


