#!/bin/sh
#
# This script creates a report for the test run in the /results directory
#

#
# Adjust Directory
#
cd ${0%/*}

#
# Timer sub
#
timer(){
START=`date '+%s'`
while [ ! -z `ps -eo pid | grep $1 | head -1` ]
do
    NOW=`date '+%s'`
    DIFF=$(( $NOW - $START ))
    H=$(( $DIFF / 3600 ))
    if [ $H -lt 10 ]; then H="0$H"; fi
    M=$(( ($DIFF / 60) % 60 ))
    if [ $M -lt 10 ]; then M="0$M"; fi
    S=$(( $DIFF % 60 ))
    if [ $S -lt 10 ]; then S="0$S"; fi
    echo -e "\rElapsed time:   $H:$M:$S\c"
    sleep 1
done
}

#
# Parameters
#
MEM=`cat /proc/meminfo | head -1 | awk '{print $2}'`
MEM=$((MEM / 1024))
if [ ! $MEM -gt 0 ]; then MEM=256; fi
if [ $MEM -gt 1024 ]; then MEM=1024; fi
GENERATE="../tools/TraceTools/bin/stargen -J-Xmx${MEM}m"
TTIMEMARK=${TTIMEMARK:-`date +%Y-%m-%d_%H-%M-%S`}
REPORT_DIR=${REPORT_DIR:-`ls -1vd /var/opt/lsb/test/olver-core/* | tail -1`}
GENOPT="-crpfm false -d $REPORT_DIR/report -x $REPORT_DIR/res.xml -mfpsc 1000 -bdbs xml:../tools/BugDB/BugDB.xml"

#
# Define appropriate XML parser
#
../tools/reportgen/select_xml_parser.pl $OLVER_XML_PARSER > ../tools/share/perl/SAX.ini

if test ! -w $REPORT_DIR; then
    echo "Warning: '$REPORT_DIR' is not writable"
fi

#
# Generate UniTesK report
#
echo ""
echo "Generate UniTesK report"
echo "----------------------------"
java -version 2>/dev/null
if [ $? -eq 0 -a -z "$REPORT_NO_JAVA" ]; then
    # fall back to perl parser if gcj is the only JVM available (bug 3091)
    java -version | grep -q gcj
    if [ $? -ne 0 -a -z "$REPORT_NO_JAVA" ]; then
        ls $REPORT_DIR/*.ut* >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            $GENERATE $GENOPT $REPORT_DIR/*.ut*
        fi
    fi
fi

test -f $REPORT_DIR/res.xml && grep '</Report>' $REPORT_DIR/res.xml >/dev/null
if [ $? -ne 0 ]; then
    echo "Java report generator failed"
    echo "Trying to use perl instead"
    echo -n "Using XML::SAX parser "
    tail -1 ../tools/share/perl/SAX.ini | awk '{print $3}'
    echo ""

    ls $REPORT_DIR/*.ut* >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        ../tools/reportgen/report.pl $GENOPT $REPORT_DIR/*.ut*
    fi
fi

if test ! -f $REPORT_DIR/res.xml; then
    echo "Error: res.xml is not found"
    exit 1
fi

grep '</Report>' $REPORT_DIR/res.xml >/dev/null
if [ $? -ne 0 ]; then
    echo "Error: res.xml is invalid"
    exit 1
fi

#
# Adjust Directory
#
cd ../tools/reportgen

#
# Generate REQ coverage report
#
if test -x /usr/bin/perl; then
    echo
    echo "Generate REQ coverage report"
    echo "----------------------------"
    { timer $$ & timerid=$! ;}
    ./reqcoverage.pl -o $REPORT_DIR/result.htm $REPORT_DIR/res.xml ./ids.xml
    { kill -s 9 $timerid & sleep 1 ;} >/dev/null 2>&1
else
    echo "Coverage report skipped because '/usr/bin/perl' is absent"
fi
echo

#
# Generate TET report
#
if test -x /usr/bin/perl; then
    echo
    echo "Generate TET-like report"
    echo "----------------------------"
    { timer $$ & timerid=$! ;}
    mkdir -p $REPORT_DIR/tet
    ./tetreport.pl $REPORT_DIR/res.xml > $REPORT_DIR/tet/tetreport.log
    ./tjreport -d $REPORT_DIR/tet/tetreport.log > $REPORT_DIR/tet/nice_tet.log
    { kill -s 9 $timerid & sleep 1 ;} >/dev/null 2>&1
else
    echo "TET-like report skipped because '/usr/bin/perl' is absent"
fi
echo

#
# Generate Summary Report
#
if test -x /usr/bin/perl; then
    echo
    echo "Generate OLVER summary report"
    echo "----------------------------"
    { timer $$ & timerid=$! ;}
    ./sumreport.pl -c $REPORT_DIR/result.htm -r $REPORT_DIR/run.log -a $REPORT_DIR/agent.log.gz -l ./LSBCoreFunctions -o $REPORT_DIR/summary.htm $REPORT_DIR/res.xml
    echo
    echo
    { kill -s 9 $timerid & sleep 1 ;} >/dev/null 2>&1
    echo "Open $REPORT_DIR/summary.htm to see reports"
else
    echo
    echo "OLVER summary report skipped because '/usr/bin/perl' is absent"
fi
