#! /usr/bin/env bash
#
# Bro postprocessor script to archive log files.
#
# archive-log [-c] <rotated-file-name> <base-name> <timestamp-when-opened> <timestamp-when-closed> <terminating> <writer>

. `dirname $0`/broctl-config.sh

delete=1
if [ "$1" == "-c" ]; then
    delete=0
    shift
fi

terminating=$5
writer=$6

century=`date +%Y | sed 's/..$//g'`

from=`echo $3 | sed 's/[_.]/-/g'`
from="$century$from"

to=`echo $4 | sed 's/[_.]/-/g'`
to="$century$to"

ext=`echo $1 | sed 's/^.*\.//'`
dest=`${makearchivename} $2.$ext $writer $from $to `

echo $dest | grep -q '^/'

if [ $? != 0 ]; then
    dest="${logdir}/$dest"
fi

dest_dir=`dirname $dest`

mkdir -p $dest_dir # Makes sure all parent directories exist.

# Record time of last rotation.
date +%y-%m-%d_%H.%M.%S >.rotated.$2 # Bro default format when rotating files.

# Run other postprocessors.
for pp in ${postprocdir}/*; do
    nice $pp $@
done

if [ -e $1 ]; then
    if [ "${compresslogs}" == "1" -a "$writer" == "ascii" ]; then
        nice gzip -9 <$1 >$dest.gz 2>/dev/null &
    else
        nice cp $1 $dest &
    fi
fi

wait

if [ "$?" == "0" ]; then
    if [ "$delete" == "1" ]; then
        rm -rf $1
    else
        # Only delete if too large (>100MB).
        find $1 -size +104857600c -delete
    fi
fi

if [ "$terminating" == "1" ]; then
    ${scriptsdir}/remove-link-for-log $2
fi

