#!/bin/bash

LOGROTATE=../logrotate
M="-m ./mailer"
S=-"s state"
RLR="$LOGROTATE $M $S"

cleanup() {
    rm -f test*.log* anothertest*.log* state test-config.[1-9] scriptout mail-out

    [ -n "$1" ] && echo "Running test $1"
}

genconfig() {
    input=test-config.$1.in
    output=test-config.$1
    sed "s,&DIR&,$PWD,g" < $input > $output
}

createlog() {
    num=$1
    file=$2

    case $num in
	0)
	    what=zero
	    ;;
	1)
	    what=first
	    ;;
	2)
	    what=second
	    ;;
	3)
	    what=third
	    ;;
	4)
	    what=fourth
	    ;;
	5)
	    what=fifth
	    ;;
	*)
	    exit 1
	    ;;
    esac

    echo $what > $file
}

createlogs() {
    base=$1
    numlogs=$2

    rm -f ${base}*

    num=0
    while [ $num != $numlogs ]; do
	if [ $num = 0 ]; then
	    createlog 0 $base
	else
	    createlog $num ${base}.$num
	fi

	num=`expr $num + 1`
    done
}

checkoutput() {
    while read line; do
	set $line
	file=$1
	shift

	fileother=`echo $line | awk '{print $1}'`
	expected=`echo $line | cut -s -d\  -f2-`

	if [ $file != $fileother ]; then
	    echo "unexpected file $file'" >&2
	    return 1
	fi

	if [ ! -f $file ]; then
	    echo "file $file does not exist"
	fi

	contents=`cat $file`
	if [ "$contents" != "$expected" ]; then
	    echo "file $file does not contain expected results" >&2
	    echo contains: \'$contents\'
	    echo expected: \'$expected\'
	    return 1
	fi
    done
}

preptest() {
    base=$1
    confignum=$2
    numlogs=$3

    rm -f $base*
    rm -f state

    genconfig $confignum
    createlogs $base $numlogs
}

# we don't want any stuff left from previous runs
cleanup

# Without a log file, no rotations should occur
preptest test.log 1 2
$RLR test-config.1 

checkoutput test.log* <<EOF
test.log zero
test.log.1 first
EOF

# Put in place a state file that will force a rotation
cat > state <<EOF
logrotate state -- version 1
"$PWD/test.log" 2000-1-1
EOF

# Now force the rotation
$RLR test-config.1
checkoutput test.log* <<EOF
test.log
test.log.1 zero
test.log.2 first
EOF

# rerun it to make sure nothing happens
$RLR test-config.1 

checkoutput test.log* <<EOF
test.log
test.log.1 zero
test.log.2 first
EOF

cleanup 1

preptest test.log 2 3
$RLR test-config.2 --force

checkoutput test.log* <<EOF
test.log.1 zero
test.log.2 first
EOF

if [ -f test.log ]; then
    echo "erroneously created test.log"
fi

cleanup 2

preptest test.log 3 1
$RLR test-config.3 --force

checkoutput test.log* <<EOF
test.log
test.log.1 zero
scriptout foo
EOF

cleanup 3

preptest test.log 3 1
preptest test2.log 3 1
$RLR test-config.3 --force

checkoutput test*.log* <<EOF
test.log
test.log.1 zero
test2.log
test2.log.1 zero
scriptout foo foo
EOF

cleanup 4

preptest test.log 4 1
preptest test2.log 4 1
$RLR test-config.4 --force 

checkoutput test*.log* <<EOF
test.log
test.log.1 zero
test2.log
test2.log.1 zero
scriptout foo
EOF

cleanup 5

preptest test.log 5 1
preptest anothertest.log 5 1
$RLR test-config.5 --force 

checkoutput test*.log* <<EOF
test.log
test.log.1 zero
anothertest.log
anothertest.log.1 zero
scriptout foo
EOF

cleanup 6

preptest test.log 6 1
preptest anothertest.log 6 1
$RLR test-config.6 --force

checkoutput test*.log* <<EOF
test.log
test.log.0 zero
anothertest.log
anothertest.log.0 zero
scriptout foo
EOF

cleanup 7

preptest test.log 7 1
preptest anothertest.log 7 1
$RLR test-config.7 --force

checkoutput test*.log* <<EOF
test.log
test.log.6 zero
anothertest.log
anothertest.log.6 zero
scriptout foo
EOF

cleanup 8
