#!/bin/sh
# PCP QA Test No. 442
# check 2^31 volume switching for pmlogextract
#
# Copyright (c) 2011 Ken McDonell.  All Rights Reserved.
# Copyright (c) 2012 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

rm -f $seq.full src/big2.* src/big3.*
./src/mkbig1 > $seq.full 2>&1
[ $? -eq 0 ] || _notrun `cat $seq.full`

# 10GB should keep us out of trouble
free=`_check_freespace 10240`
[ -z "$free" ] || _notrun $free

status=1	# failure is the default!
trap "rm -f $tmp.* src/big2.* src/big3.*; exit \$status" 0 1 2 3 15

for file in src/big1.*
do
    ln $file `echo $file | sed -e 's/big1/big2/'`
    :
done
ls -l src/big1* src/big2* >$seq.full 2>&1

_filter()
{
    sed \
	-e 's/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9][0-9][0-9]/TIMESTAMP/g'
}

# real QA test starts here
pmlogextract src/big1 src/big2 src/big3 2>&1 | _filter
ls -l src/big3* >>$seq.full 2>&1

# expect 3 volumes, and all < 2^31 bytes in size
#
limit=2147483648
nvol=`ls src/big3.? | wc -l | sed -e 's/ //g'`
if [ $nvol -ne 3 ]
then
    ls -l src/big3.?
    echo "Error: expecting 3 volumes, found $nvol"
    status=1
fi

echo "0" >$tmp.bc
for vol in src/big1.?
do
    size=`_filesize $vol`
    echo "+2*$size" >>$tmp.bc
done

for vol in src/big3.?
do
    size=`_filesize $vol`
    if [ -z "$size" ]
    then
	stat $vol
	echo "Error: cannot extract size for volume $vol"
	status=1
    elif [ "$size" -ge $limit ]
    then
	ls -l $vol
	echo "Error: size ($size) exceeds limit ($limit)"
	status=1
    fi
    echo "-$size" >>$tmp.bc
done

cat $tmp.bc >>$seq.full
echo
echo "sum(input data volume sizes) - sum(output data volume sizes)"
( tr '\012' ' ' <$tmp.bc ; echo ) | bc

echo
echo "Temporal Index Entries ..."
for arch in src/big1 src/big2 src/big3
do
    log=`echo $arch | sed -e 's/src\///g'`
    echo "$log: `pmdumplog -t $arch | grep '^[0-9]' | wc -l | sed -e 's/ //g'`"
done

# success, all done
status=0
exit
