#!/bin/bash
#set -x
set -e
#=======================================================================
# * Version: $Id: logrotate_apache.src,v 1.3 2015/09/27 21:32:52 nroche Exp $
# * Project: MediaTex
# * Module : scripts
# *
# * This script manage apache2 access log extraction
#
# MediaTex is an Electronic Records Management System
# Copyright (C) 2014 2015 Nicolas Roche
#
# 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 3 of the License, or
# 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.  If not, see <http://www.gnu.org/licenses/>.
#=======================================================================

BINDIR=/usr/local/bin

# backup apache access logs
for COLL in $($BINDIR/mediatex ls coll); do
    DATE=$(/bin/date +'%Y%m%d')
    HOST=$(/bin/hostname -f)
    NAME=apache_${DATE}_${HOST}

    /bin/grep "mdtx-$COLL" /var/log/apache2/access.log >/tmp/$NAME.log || \
	/bin/true
    SIGN1=$(/usr/bin/md5sum /tmp/$NAME.log | cut -d' ' -f 1)
    SIGN1=$SIGN1:$(ls /tmp/$NAME.log -l | cut -d' ' -f 5)

    /bin/gzip -f /tmp/$NAME.log
    SIGN2=$(/usr/bin/md5sum /tmp/$NAME.log.gz | cut -d' ' -f 1)
    SIGN2=$SIGN2:$(/bin/ls /tmp/$NAME.log.gz -l | cut -d' ' -f 5)

    /bin/cat >/tmp/$NAME.cat <<EOF
Top Category "~mediatex"
Category "access": "~mediatex"
Document "$NAME": "access"
 "date" = "$(/bin/date)"
 "extracted from" = "$HOST"
 $SIGN1
 $SIGN2

Archive $SIGN1
 "format" = "text"
Archive $SIGN2
 "format" = "gz"
EOF

    TARGETDIR="mediatex/audits"
    /bin/cat >/tmp/$NAME.ext <<EOF
(GZIP
$SIGN2
=>
$SIGN1 $TARGETDIR/$NAME.log
)
EOF

    $BINDIR/mediatex upload++ \
        file /tmp/$NAME.log.gz as mediatex/backups/ \
        catalog /tmp/$NAME.cat rules /tmp/$NAME.ext \
        to coll $COLL

    /bin/rm -f /tmp/$NAME.log.gz /tmp/$NAME.cat /tmp/$NAME.ext
done
