#!/bin/bash -x

HDATE=`date -u +"%a %b %_d, %R"`

SELF=/root/create-warnings-file.sh
MYTH_DIR=$1
OUTPUT_DIR=$2
MYTHTV_DIR=$MYTH_DIR/mythtv
MYTHPLUGINS_DIR=$MYTH_DIR/mythplugins

ccache -c

pushd $MYTH_DIR > /dev/null
  git pull > /dev/null
popd > /dev/null

pushd $MYTHTV_DIR > /dev/null
  rm -f mythtv-warnings.txt mythtv-warnings.html

  make distclean > /dev/null 2> /dev/null
  ./configure --enable-libfftw3 --enable-extra-warnings > /dev/null
  nice make -j3 -k > /dev/null 2>&1

  make distclean > /dev/null
  ./configure --enable-libfftw3 --enable-extra-warnings > /dev/null
  nice make -j1 -k > /dev/null 2> mythtv-warnings.txt

  cat mythtv-warnings.txt > mythtv-warnings-unfiltered.txt
  cat mythtv-warnings.txt | grep -v yasm | grep -v tspacket.h > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v replex/multiplex.c > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v svq1dec.c > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v v210enc.c > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v v210dec.c > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v hdhomerun_control.c > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "inlining failed" > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "/usr/include/qt4" > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "/usr/bin/ld" > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "In file included from" > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "                 from" > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "cc1plus: note: initialized from here" > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v XvShmCreateImage > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "libav" > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "libswscale" > tmp.txt ; mv tmp.txt mythtv-warnings.txt
  cat mythtv-warnings.txt | fgrep -v "libpostproc" > tmp.txt ; mv tmp.txt mythtv-warnings.txt

  cat mythtv-warnings.txt | fgrep -v fifowriter.cpp | fgrep -v avi.c | fgrep -v multiplex.c | fgrep -v mpeg2fix.cpp | fgrep -v replex.c | fgrep -v pes.c | fgrep -v ringbuffer.c > mythtv-warnings-other.txt
  cat mythtv-warnings.txt | egrep "fifowriter.cpp|avi.c|multiplex.c|mpeg2fix.cpp|replex.c|pes.c|ringbuffer.c" > mythtv-warnings-transcode.txt

  WARNING_COUNT=`cat mythtv-warnings.txt | grep warning | wc -l`
  WARNING_COUNT_TRANS=`cat mythtv-warnings-transcode.txt | grep warning | wc -l`
  WARNING_COUNT_OTHER=`cat mythtv-warnings-other.txt | grep warning | wc -l`

  ## cppcheck is now run externally...
  #cppcheck -j3 . > cppcheck-progress.txt 2> mythtv-cppcheck.txt
  touch mythtv-cppcheck.txt
  cat mythtv-cppcheck.txt | fgrep -v PSIPTable | fgrep -v VirtualChannelTable > tmp.txt ; mv tmp.txt mythtv-cppcheck.txt
  cat mythtv-cppcheck.txt | fgrep -v "Invalid number of character" > tmp.txt ; mv tmp.txt mythtv-cppcheck.txt

  CPPCHECK_WARNING_COUNT=`cat mythtv-cppcheck.txt | wc -l`

  EDATE=`date -u +%R`
  echo "<h1>MythTV Warnings " $HDATE - $EDATE UTC"</h1>" >  mythtv-warnings.html
  echo "<h2>GCC Count: $WARNING_COUNT </h2>"          >> mythtv-warnings.html
  echo "<h2>CPPCHECK Count: $CPPCHECK_WARNING_COUNT </h2>" >> mythtv-warnings.html

  echo "<h3>All GCC (except replex) Count: $WARNING_COUNT_OTHER </h3>" >> mythtv-warnings.html
  echo "<pre>"                                        >> mythtv-warnings.html
  cat mythtv-warnings-other.txt                       >> mythtv-warnings.html
  echo "</pre>"                                       >> mythtv-warnings.html

  echo "<h3>replex GCC Count: $WARNING_COUNT_TRANS </h3>">> mythtv-warnings.html
  echo "<pre>"                                        >> mythtv-warnings.html
  cat mythtv-warnings-transcode.txt                   >> mythtv-warnings.html
  echo "</pre>"                                       >> mythtv-warnings.html

  echo "<h3>All CPPCHECK Count: $CPPCHECK_WARNING_COUNT </h3>" >> mythtv-warnings.html
  echo "<pre>"                                        >> mythtv-warnings.html
  cat mythtv-cppcheck.txt                             >> mythtv-warnings.html
  echo "</pre>"                                       >> mythtv-warnings.html

popd > /dev/null

pushd $MYTHTV_DIR > /dev/null
  make install > /dev/null
  make distclean > /dev/null
popd > /dev/null

HDATE=`date -u +"%a %b %_d, %R"`

pushd $MYTHPLUGINS_DIR > /dev/null
  make distclean
  ./configure > /dev/null
  nice make -j3 > /dev/null 2>&1

  make distclean
  ./configure > /dev/null

  SUBDIRS="mytharchive mythgame mythmusic mythweather mythzoneminder mythbrowser mythgallery mythnews mythvideo"
  WARNING_COUNT=0
  for I in $SUBDIRS ; do
    pushd $I > /dev/null
      touch $MYTHPLUGINS_DIR/$I-warnings.txt $MYTHPLUGINS_DIR/$I-cppcheck.txt
      qmake > /dev/null
      nice make -j3 > /dev/null 2> $MYTHPLUGINS_DIR/$I-warnings.txt
      cat $MYTHPLUGINS_DIR/$I-warnings.txt | fgrep -v "/usr/include/qt4"       > tmp.txt ; mv tmp.txt $MYTHPLUGINS_DIR/$I-warnings.txt
      cat $MYTHPLUGINS_DIR/$I-warnings.txt | fgrep -v "/usr/bin/ld"            > tmp.txt ; mv tmp.txt $MYTHPLUGINS_DIR/$I-warnings.txt
      cat $MYTHPLUGINS_DIR/$I-warnings.txt | fgrep -v "/usr/include/libvisual" > tmp.txt ; mv tmp.txt $MYTHPLUGINS_DIR/$I-warnings.txt
      cat $MYTHPLUGINS_DIR/$I-warnings.txt | fgrep -v "/goom/mathtools.h"      > tmp.txt ; mv tmp.txt $MYTHPLUGINS_DIR/$I-warnings.txt
      LOCAL_WARNING_COUNT=`cat $MYTHPLUGINS_DIR/$I-warnings.txt | grep warning | wc -l`
      WARNING_COUNT=`expr $WARNING_COUNT + $LOCAL_WARNING_COUNT`
    popd > /dev/null
  done

  EDATE=`date -u +%R`
  echo "<h1>MythPlugins Warnings $HDATE - $EDATE UTC</h1>"     >  mythplugins-warnings.html
  echo "<h2>GCC Count: $WARNING_COUNT </h2>"          >> mythplugins-warnings.html

  for I in $SUBDIRS ; do
    LOCAL_WARNING_COUNT=`cat $MYTHPLUGINS_DIR/$I-warnings.txt | grep warning | wc -l`
    echo "<h3>GCCs $I warnings Count: $LOCAL_WARNING_COUNT</h3>" >> mythplugins-warnings.html
    echo "<pre>"                                      >> mythplugins-warnings.html
    cat $I-warnings.txt                               >> mythplugins-warnings.html
    echo "</pre>"                                     >> mythplugins-warnings.html
  done
popd > /dev/null

pushd /tmp > /dev/null
  echo "<html>"                                              >  mythtv-warnings.html
    echo "<head>"                                            >> mythtv-warnings.html
    echo "<title>MythTV &amp; MythPlugins Warnings Report</title>" >> mythtv-warnings.html
    echo "</head>"                                           >> mythtv-warnings.html
    echo "<body>"                                            >> mythtv-warnings.html
    cat $MYTHTV_DIR/mythtv-warnings.html                     >> mythtv-warnings.html
    cat $MYTHPLUGINS_DIR/mythplugins-warnings.html           >> mythtv-warnings.html
    echo "<br/><p><a href=\"create-warnings-file.sh\">"      >> mythtv-warnings.html
    echo "Script used to create this report.</a></p><br/>"   >> mythtv-warnings.html
    echo "<br/><p><a href=\"mythtv-unfiltered.txt\">"        >> mythtv-warnings.html
    echo "Unfiltered mythtv warnings</a></p><br/>"           >> mythtv-warnings.html

    echo "</body>"                                           >> mythtv-warnings.html
  echo "</html>"                                             >> mythtv-warnings.html

  scp mythtv-warnings.html    mythtvdocs@www.cuymedia.net:/var/www/www.cuymedia.net/mythtv-warnings/index.html
  scp $MYTHTV_DIR/mythtv-warnings-unfiltered.txt mythtvdocs@www.cuymedia.net:/var/www/www.cuymedia.net/mythtv-warnings/mythtv-unfiltered.txt
  scp $SELF                   mythtvdocs@www.cuymedia.net:/var/www/www.cuymedia.net/mythtv-warnings/create-warnings-file.sh
popd > /dev/null

ccache -c > /dev/null
ccache -s

