#!/bin/sh

# $Id: merge2combined,v 1.3 2009/01/26 09:06:11 vanbaal Exp $
# $Source: /cvsroot/logreport/service/www/script/merge2combined,v $

# merge2combined - uncompress log files and convert them to extended Common Log Format

# Copyright (C) 2008 Joost van Baal
#
# 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.  There is NO warranty.  A copy of the GNU GPL is available from
# http://www.gnu.org/licenses/.

if test "--help" = "$1"
then
    cat <<EOT
The script merge2combined is useful in situations where logging data from both
Apache and IIS servers is to be combined in one Web traffic report.

Assumed is all files in current directory matching *.zip are ZIP archives, each
containing exactly one log file in the W3C Extended Log Format, as used by
Microsoft's Internet Information Services, while all files matching .gz are
gzip-ed log files in the extended Common Log Format (aka combined format), as
commonly used by Apache. All log files are unzipped (or gunzipped), merged and
converted to one log file in the combined format; this one log file is printed
to stdout.

Environment variables LR_w3c_extended2dlf LR_combined2dlf and LR_dlf2combined
are honored.  For debugging, set LR_merge2combined_debug=true.

Of course, environment variables PATH and TMPDIR are honored.

See README.merging-log-files in the Lire source tarball for a complete usage
example.
EOT
    exit 0
fi

for s in w3c_extended2dlf combined2dlf dlf2combined
do
    eval l="\$LR_$s"
    if test -n "$l"
    then
        # user has set e.g. LR_w3c_extended2dlf, honor that one
        :
    elif command -v $s >/dev/null
    then
        eval LR_$s=$s
    elif test -x /usr/share/lire/lib/lire/convertors/$s
    then
        # Debian package installed
        eval LR_$s=/usr/share/lire/lib/lire/convertors/$s
    else
        cat <<EOT
Command $s not found.  Please run "export
PATH=/path/to/your/lire/convertors:$PATH" or run "export
LR_$s=/path/to/your/$s" before starting this
script.
EOT
        exit 1
    fi
done

: ${LR_merge2combined_debug:=false}

out=`mktemp -d`
"$LR_merge2combined_debug" || trap 'rm -rf $out' EXIT TERM

for f in *.zip
do
    unzip -p -a $f | $LR_w3c_extended2dlf > $out/${f%.zip}.dlf
done

zcat *.gz | $LR_combined2dlf > $out/mergedgz.dlf

sort --key=10 $out/*.dlf | $LR_dlf2combined

"$LR_merge2combined_debug" && echo >&2 "tmpfiles are kept in $out"

