#!/bin/sh
# PCP QA Test No. 1132
# Exercise pcp2xlsx.
#
# Copyright (c) 2017-2018 Red Hat.
#

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

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"
$python -c "import openpyxl" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python openpyxl module not installed"

which unzip >/dev/null 2>&1 || _notrun "unzip not installed"
which pcp2xlsx >/dev/null 2>&1 || _notrun "pcp2xlsx not installed"

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal
$sudo rm -rf $tmp $tmp.* $seq.full
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_unzip_filter()
{
    sed \
       -e "s|Archive:.*||g" \
       -e "s|[ ]*$||g" \
    #end
}

_xlsx_filter()
{
    sed \
       -e 's|>.*</dcterms:created>|>|g' \
       -e 's|>.*</dcterms:modified>|>|g' \
    < $1 > $1.f
    mv $1.f $1
}

_path_filter()
{
    sed \
       -e 's|<c r="B2" s="1" t="inlineStr"><is><t>.*archives/rep</t>|<c r="B2" s="1" t="inlineStr"><is><t>archives/rep</t>|g' \
    < $1 > $1.f
    mv $1.f $1
}

A="$here/archives/rep"

# real QA test starts here
echo "--- running pcp2xlsx"
pcp2xlsx -a $A -H -I -z -b MB -P 2 -F $tmp.outfile ""
mkdir -p $tmp.dir/ref $tmp.dir/run
cd $tmp.dir/ref
echo "--- unzipping reference output"
unzip $here/sheet/pcp2xlsx.qa.1132.out | _unzip_filter
_xlsx_filter docProps/core.xml
_path_filter xl/worksheets/sheet1.xml
cd $tmp.dir/run
echo "--- unzipping generated output"
unzip $tmp.outfile | _unzip_filter
_xlsx_filter docProps/core.xml
_path_filter xl/worksheets/sheet1.xml
cd $tmp.dir
echo "--- diffing outputs - no output expected"
# map >< to >\n< so we can see differences better
# and force %.3f output for floats to avoid precision differences
# 
for file in ref/xl/worksheets/sheet1.xml run/xl/worksheets/sheet1.xml \
	    ref/xl/styles.xml run/xl/styles.xml \
	    ref/xl/sharedStrings.xml run/xl/sharedStrings.xml
do
    sed <$file  \
	-e '/^<styleSheet/d' \
	-e '/^<sst/d' \
	-e 's/></>\
</g' \
    | $PCP_AWK_PROG -F '>' '
BEGIN	{ OFS = "<" }
$1 == "<v" && $2 ~ /^[0-9][0-9]*\.[0-9]/	{ $2 = sprintf("%.3f>",gsub(/>/, "", $2)) }
	{ print }' >$tmp.tmp
    mv $tmp.tmp $file
done

find ref run -type f \
| xargs wc >>$here/$seq.full
diff -purN ref run
echo "--- end of diff"
cd $here
rm -f $tmp.outfile

# success, all done
status=0
exit
