#!/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 tmparch/big2.* tmparch/big3.*
./tmparch/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.* tmparch/big2.* tmparch/big3.*; exit \$status" 0 1 2 3 15

for file in tmparch/big1.*
do
    ln $file `echo $file | sed -e 's/big1/big2/'`
    :
done
ls -l tmparch/big1* tmparch/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 tmparch/big1 tmparch/big2 tmparch/big3 2>&1 | _filter
ls -l tmparch/big3* >>$seq.full 2>&1

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

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

for vol in tmparch/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 tmparch/big1 tmparch/big2 tmparch/big3
do
    log=`echo $arch | sed -e 's/tmparch\///g'`
    echo "$log: `pmdumplog -t $arch | grep '^[0-9]' | wc -l | sed -e 's/ //g'`"
done

# success, all done
status=0
exit
