#! /bin/sh -e

# $Id: lr_check_prereq.in,v 1.6 2002/07/21 18:19:29 vanbaal Exp $

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

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/local"
datadir="${prefix}/share/lire"
exec_prefix="${prefix}"
libexecdir="${exec_prefix}/libexec/lire"
etcdir="${prefix}/etc/lire"
. $etcdir/defaults

case $format in
    xml|txt)
	required=""
	;;
    excel95)
	required="excel"
	;;
    pdf)
	required="xslt jade jadetex docbook-dtd docbook-dsssl"
	;;
    html|html_page|xhtml|xhtml_page)
	required="xslt docbook-dtd docbook-xsl tar"
	;;
    docbookx)
	required="xslt"
	;;
    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|html_page|xhtml|xhtml_page|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$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
	;;
    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"
        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
	;;
    zip)
	if test "x$LR_ZIP" = "x" || test ! -x "$LR_ZIP"
	then
	    echo >&2 "$tag err can't generate images: zip ('$LR_ZIP') is not available!"
	    exit 1
	fi
	;;
    esac
done

echo >&2 "$tag info stopped"

