#!/bin/bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

TOOLDIR="${DERBYDIR}/${BRANCH_DIR}/tools/testing/reporting/scripts"
. ${TOOLDIR}/env

SHOWDETAILS=""
PRESENTATION="Limited"
if [ "$1" = "details" ]
then
  # Show details
  SHOWDETAILS="ShowDetails"
  PRESENTATION="Full"
fi

# We have one catalog per platform
if [ ! -e ${TESTLOGDIR} ]
then
  echo "${TESTLOGDIR} does not exist"
  exit
fi

# Get the list of testsuite we are running:
TESTLISTFILE="${DERBYDIR}/testing/showtestlist"
SUITES=`gawk '{ print $1 }' ${TESTLISTFILE} | grep -v "^#"`

# Get the set of platforms
cd ${TESTLOGDIR}
PLATFORMS=`ls`

TESTID=`echo ${DERBYDIR} | gawk -F/ '{ print $NF }'`
  # Derby | TinderBox_Derby | XDerbyX | myDerbySandbox | jvm1.5 ....
TINDER=`echo "${TESTID}" | grep TinderBox` # Contains TinderBox
LARGEDATA=`echo "${TESTID}" | grep LargeData` # Contains LargeData

# Get the list of revisions
REVISIONSFILE="${DERBYDIR}/testing/revisionlist"
# head -60 to reduce to approx. last 3 months of daily tests (otherwise last 60 - ..TinderBox)
# Here we show all:
REVISIONS=`gawk '{ print $1 }' ${REVISIONSFILE} | grep -v "^#"`
PREVUPDATEREV=`gawk '{ print $1 }' ${REVISIONSFILE} | grep -v "^#" | head -2 | tail -1`

# CHANGE HERE TO USE A DIFFERENT FILE:
DTI="${DERBYDIR}/testing/${PRESENTATION}/index_all.html"
DTIPUB="${PUBLISHDIR}/${PRESENTATION}/index_all.html"


echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" > ${DTI}
chmod go+r ${DTI}
echo "<html> <head>" >> ${DTI}
echo "</head>" >> ${DTI}

echo "<body>" >> ${DTI}


if [ "$1" != "details" ]
then

  echo "<table style=\"text-align: left; width: 100%;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" >> ${DTI}
  echo "  <tbody>" >> ${DTI}
  echo "    <tr>" >> ${DTI}
  echo "      <td style=\"vertical-align: bottom;\"><font size=\"+1\"><b>" >> ${DTI}
  echo "        <a href=\"http://db.apache.org/derby\" target=\"_top\">Apache Derby" >> ${DTI}
  echo "        </a></b></font> is an open source, 100% Java SQL Database." >> ${DTI}
  echo "      </td>" >> ${DTI}
  # echo "      <td style=\"vertical-align: top; text-align: right;\"><i>Web space sponsored by</i>" >> ${DTI}
  # echo "        <a href="http://www.multinet.no/">" >> ${DTI}
  # echo "          <img alt=\"MultiNet AS\" style=\"border: 0px solid ; width: 150px; height: 27px;\"" >> ${DTI}
  # echo "          src=\"http://www.multinet.no/gfx/multilogo.jpg\">" >> ${DTI}
  # echo "        </a>" >> ${DTI}
  # echo "      </td>" >> ${DTI}
  echo "    </tr>" >> ${DTI}
  echo "  </tbody>" >> ${DTI}
  echo "</table>" >> ${DTI}

  echo "<HR>" >> ${DTI}
fi

TDB="TD style=\"vertical-align: bottom;\""
TDT="TD style=\"vertical-align: top;\""

DURHD="<$TDB></TD>"
if [ "${TINDER}" != "" ] || [ "${LARGEDATA}" != "" ]
then # Show duration %
  SHOWDURSUITE="Derbyall"
  if [ "${LARGEDATA}" != "" ]
  then
    SHOWDURSUITE="LargeData"
  fi
   DURHD="<$TDB><i> &nbsp;&nbsp;&nbsp;&nbsp;${SHOWDURSUITE}<br>&nbsp;&nbsp;&nbsp;&nbsp;duration<br>&nbsp;&nbsp;&nbsp;&nbsp;vs baseline </i></TD>"
fi
# if [ "${TESTID}" = "TinderBox_Derby" ]
# then # Show duration %
#    DURHD="<$TDB><i> &nbsp;&nbsp;&nbsp;&nbsp;Derbyall<br>&nbsp;&nbsp;&nbsp;&nbsp;duration<br>&nbsp;&nbsp;&nbsp;&nbsp;vs baseline </i></TD>"
# fi

FOOTPRINTBASEREV=`head -1 ${DERBYDIR}/testing/builds/baseline.size | gawk -F\# '{ print $2 }'`
# TS=`cat ${UPDATELOGDIR}/${FOOTPRINTBASEREV}/UpdateTime`
TS=`head -2 ${DERBYDIR}/testing/builds/baseline.size | tail -1 | gawk -F\# '{ print $2 }'`

BGCOLOR="bgcolor=\"${COLOR0}\""
echo "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" >> ${DTI}
echo "<TR ${BGCOLOR} >" >> ${DTI}
echo "<$TDB><i> Revision </i></TD>" >> ${DTI}
echo "<$TDB><i> &nbsp;&nbsp;&nbsp;&nbsp;Date, Time </i></TD>" >> ${DTI}
echo "<$TDB><i> &nbsp;&nbsp;&nbsp;&nbsp;Changes </i></TD>" >> ${DTI}
echo "<$TDB><i> &nbsp;&nbsp;&nbsp;&nbsp;Size of derby.jar<br>&nbsp;&nbsp;&nbsp;&nbsp;</i>[Baseline: ${FOOTPRINTBASEREV}<br>&nbsp;&nbsp;&nbsp;&nbsp;${TS}]</TD>" >> ${DTI}
echo "<$TDB><i> &nbsp;&nbsp;&nbsp;&nbsp;Derbyall<br>&nbsp;&nbsp;&nbsp;&nbsp;failures </i></TD>" >> ${DTI}
echo "${DURHD}" >> ${DTI}
echo "<$TDB><i>&nbsp;&nbsp;&nbsp;&nbsp;Notes </i></TD>" >> ${DTI}
echo "</TR>" >> ${DTI}

SANDBOX=`basename ${DERBYDIR}`
no=1
for REVISION in ${REVISIONS}
do
  echo -n "${REVISION} "
  TS=`cat ${UPDATELOGDIR}/${REVISION}/UpdateTime`
  DT=`echo ${TS} | gawk '{ print $1 }'`
  TT=`echo ${TS} | gawk '{ print $2 }'`
  TZ=`echo ${TS} | gawk '{ print $3 }'`
  TS="${DT} ${TT} ${TZ}"
  CHGLINK="&nbsp;&nbsp;&nbsp;&nbsp;"
  if [ -e ${DERBYDIR}/testing/UpdateInfo/${REVISION}.txt ]
  then
    CHGLINK="&nbsp;&nbsp;&nbsp;&nbsp;<A HREF=\"../../UpdateInfo/${REVISION}.txt\">Chgs</A> "
  fi
  FOOTPRINT="&nbsp;&nbsp;&nbsp;&nbsp;"
  if [ -e ${DERBYDIR}/testing/builds/${REVISION}.size ]
  then
    SIZE=`grep derby.jar ${DERBYDIR}/testing/builds/${REVISION}.size | gawk '{ print $2" "$3 }'`
    FOOTPRINT="&nbsp;&nbsp;&nbsp;&nbsp;<A href=\"../../builds/${REVISION}.size\">${SIZE}</A>"
  fi

  DERBALLFAILS=""
  DERBALLPRCNT=""
  # TINDERBOX=`echo ${DERBYDIR} | grep TinderBox_`
  # if [ "${TESTID}" = "TinderBox_Derby" ]
  # then
    DERBALLFAILS="["
    for PLATFORM in ${PLATFORMS} # TinderBox uses ONE platform..
    do
      if [ -e ${PLATFORM}/externallyVisible ] || [ "${SHOWDETAILS}" = "ShowDetails" ]
      then
        if [ -e ${PLATFORM}/${REVISION}.csv ]
        then
          #  2: Number, 3: OK, 4: Failed, 5: Skipped, 6: time
          RES=`grep "^derbyall " ${PLATFORM}/${REVISION}.csv`
          if [ "${LARGEDATA}" != "" ]
          then
            RES=`grep "^largeData " ${PLATFORM}/${REVISION}.csv`
          fi
          FAILED=`echo ${RES} | gawk '{ print $4 }'`
          if [ "${FAILED}" == "" ]
          then
            FAILED="?"
          fi
          DERBALLFAILS="${DERBALLFAILS} ${FAILED} "
          if [ "${TINDER}" != "" ] || [ "${LARGEDATA}" != "" ]
          then
            TIME=`grep "^derbyall " ${PLATFORM}/${REVISION}.csv | gawk '{ print $6 }'`
            if [ "${LARGEDATA}" != "" ]
            then
              TIME=`grep "^largeData " ${PLATFORM}/${REVISION}.csv | gawk '{ print $6 }'`
            fi
            SECONDS=`${TOOLDIR}/toSeconds ${TIME}`
            BASESECONDS=`grep "^derbyall " ${PLATFORM}/baseline.csv | gawk '{ print $3 }'`
            if [ "${LARGEDATA}" != "" ]
            then
              BASESECONDS=`grep "^largeData " ${PLATFORM}/baseline.csv | gawk '{ print $3 }'`
            fi            
            PERCENT=`${TOOLDIR}/calcPercent ${SECONDS} ${BASESECONDS}`
            DERBALLPRCNT="&nbsp;&nbsp;&nbsp;${PERCENT}% "
          fi
        else
          DERBALLFAILS="${DERBALLFAILS} - "
        fi
      fi # visible, showdetails
    done # PLATFORMS
    DERBALLFAILS="${DERBALLFAILS}]"
  # fi # TINDERBOX

  BLDERRLINK=""
  if [ -e ${DERBYDIR}/testing/UpdateInfo/${REVISION}-buildDetails.txt ]
  then
    BLDERRLINK="&nbsp;&nbsp;&nbsp;&nbsp;<A HREF=\"../../UpdateInfo/${REVISION}-buildDetails.txt\"><font color=\"red\"><b> Build Errors!</b></font></A><br>"
  fi

  FAILURELINK=""
  if [ -e ${DERBYDIR}/FailReports/${REVISION}.html ]
  then
    FAILURELINK="&nbsp;&nbsp;&nbsp;&nbsp;<A HREF=\"../../FailReports/${REVISION}.html\">${DERBALLFAILS} </A>"
  elif [ -e ${DERBYDIR}/FailReports/${REVISION}M.html ]
  then # When we have Mods. e.g. JDK 1.6  testing with local mods to compatibility script....
    FAILURELINK="&nbsp;&nbsp;&nbsp;&nbsp;<A HREF=\"../../FailReports/${REVISION}M.html\">${DERBALLFAILS} </A>"
  fi

  NOTE=""
  if [ -e ${DERBYDIR}/testing/Notes/${REVISION}.txt ]
  then
    TXT=`cat ${DERBYDIR}/testing/Notes/${REVISION}.txt`
    NOTE="${TXT}"
  fi

  echo "<$TDT><a href=\"testSummary-"${REVISION}".html\">"${REVISION}"</a></TD>" >> ${DTI}
  echo "<$TDT>${TS}</TD>" >> ${DTI}
  echo "<$TDT>${CHGLINK}</TD>" >> ${DTI}
  echo "<$TDT>${FOOTPRINT}</TD>" >> ${DTI}
  echo "<$TDT>${BLDERRLINK}${FAILURELINK}</TD>" >> ${DTI}
  echo "<$TDT>${DERBALLPRCNT}</TD>" >> ${DTI}
  echo "<$TDT>${NOTE}</TD>" >> ${DTI}
  no=`expr $no + 1`
  echo "<TR>" >> ${DTI}
done # REVISIONS
echo "</TABLE>" >> ${DTI}
echo " [${no}]"


echo "<HR>" >> ${DTI}
echo "<div style=\"text-align: right;\">"  >> ${DTI}
echo "<font size=\"-1\"><i>"  >> ${DTI}
date +%Y-%m-%d" "%H:%M:%S" "%Z >> ${DTI}
echo "</i></font>" >> ${DTI}
echo "</div>" >> ${DTI}

# if [ "${SHOWDETAILS}" != "ShowDetails" ]
# then
#   echo "<i>Web space sponsored by</i> " >> ${DTI}
#   echo "<a href="http://www.multinet.no/">" >> ${DTI}
#   echo "<img align=\"top\" alt=\"MultiNet AS\" style=\"border: 0px solid; width: 200px; height: 36px;\" src=\"http://www.multinet.no/gfx/multilogo.jpg\"></a>" >> ${DTI}
# fi
echo "</body>" >> ${DTI}
echo "</html>"  >> ${DTI}
if [ "${SHOWDETAILS}" != "ShowDetails" ]
then
  SRC="${DTI}"
  DST="${DTIPUB}"
  echo "**** OBS! ${SCPUT} ${SRC} ${PUBLISHUSER}@${PUBLISHSITE}:${DST}"
  ### ${SCPUT} ${SRC} ${PUBLISHUSER}@${PUBLISHSITE}:${DST}
fi

