# vim: syntax=sh
# $Id: defaults.in,v 1.134 2006/07/23 13:16:27 vanbaal Exp $

#
# Copyright (C) 2000-2003 Stichting LogReport Foundation LogReport@LogReport.org
#
#     This file is part of Lire.
# 
#     Lire 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. 
#

# Replacement for test -nt which isn't supported
# by Solaris' test (like if this was surprising...)
lr_test_nt () {
    file1=$1
    file2=$2
    /usr/bin/perl -e 'exit( (stat($ARGV[0]))[9] > (stat($ARGV[1]))[9] ? 0 : 1 )' $file1 $file2
}

########################################################################
# Runtime check for DocBook DTD
#
# Workaround for RH platforms where the path to DBK always change.
# Anyway, this is really the proper way to locate XML DTDs.
########################################################################
runtime_docbook_dtd_check () {
    if test -z "$lr_dbk_xml_dtd" -o ! -f "$lr_dbk_xml_dtd"
    then
        if test -x "$xmlcatalog_path" -a -f /etc/xml/catalog
        then
            DTD=`$xmlcatalog_path /etc/xml/catalog "-//OASIS//DTD DocBook XML V4.1.2//EN" || true`
            if echo "$DTD" |grep '^file://' > /dev/null 2>/dev/null
	    then
                lr_dbk_xml_dtd=`echo "$DTD" | sed -e 's|^file://||'`
            fi
        fi
    fi
    LR_DBK_XML_DTD="$lr_dbk_xml_dtd"
}

setup_archive_vars () {
    # LR_FAILEDDIR is used by lr_spool only
    LR_FAILEDDIR="$lr_archive_dir/email/raw/failed"
    export LR_FAILEDDIR
}

root_check () {
    if test -x /usr/xpg4/bin/id
    then
        # solaris' /usr/bin/id doesn't grok -u
        # don't you just love it...
        runid=/usr/xpg4/bin/id
    else
        runid=id
    fi

    if test -z "$lr_test_uid"
    then
        if test "`$runid -u`" -eq 0 -o "`$runid -ru`" -eq 0
        then
            cat >&2 <<EOF
Lire isn't made to run as root.
Please run it as an unprivileged user.

If you need root access to read the log file, you can 
pipe the log file to $PROGRAM :

su -c 'cat /path/to/logfile' root | $PROGRAM ...

Alternatively, if you can turn off this check by setting
lr_test_uid to off in the 'Preferences...' menu of lire(1).
EOF
            exit 1
    fi
fi
}

setup_tmpdir () {
    if test -z "$TMPDIR" -o ! -d "$TMPDIR" -o ! -w "$TMPDIR"
    then
        TMPDIR="/tmp"
    fi
    export TMPDIR
}

setup_home_dir () {
    if test ! -d $HOME/.lire
    then
        lire_log "info creating $HOME/.lire directory"
        mkdir $HOME/.lire
    fi
    
    for d in config converters plugins reports filters schemas templates
    do
      if test ! -d $HOME/.lire/$d
      then
          lire_log "info creating $HOME/.lire/$d directory"
          mkdir $HOME/.lire/$d
      fi
    done
}

export_vars () {
    export LR_SERVICE
    export LR_SUPERSERVICE
    export LR_ID
}

lr_mark_for_cleanup () {
    _LR_TEMP_FILES="$_LR_TEMP_FILES $@"
}

lr_cleanup () {
    if test -z "$lr_keep"
    then
        test -z "$_LR_TEMP_FILES" || rm -fr $_LR_TEMP_FILES
    else
        for f in $_LR_TEMP_FILES
        do
          lire_log "debug" "lr_keep is set, not deleting '$f'"
        done
    fi
}

set_cleanup_func () {
    trap lr_cleanup EXIT TERM INT HUP
}

# Main
prefix=/usr
exec_prefix=${prefix}
. /etc/lire/profile_lean
. /usr/lib/lire/lr_functions

setup_home_dir
eval `lr_environment`
root_check
setup_tmpdir
runtime_docbook_dtd_check
setup_archive_vars
set_cleanup_func
export_vars

# Local Variables:
# mode: sh
# End:
