#!/bin/bash

# Check for target name
if [ $# -lt 1 ]; then
  echo
  echo "Usage: do_daily_build [docs] [4.1] [host [tooldir path]] [cov] [<target name>]"
  echo "Option: style - if style checking desired"
  echo "Option: schema - if schema generation desired"
  echo "Option: docs - if doxygen documentation build desired"
  echo "Option: 4.1 - if host gcc version 4.1 build"
  echo "Option: build - if host build only desired"
  echo "Option: host - if host build and test desired"
  echo "Option: tooldir path - if provided, copy host utilites to specified path"
  echo "Option: cov - if coverity target build desired"
  echo "Option: <target name> - if provided, load on, and test on target"
  exit 127
fi

# define XSD_DEST_DIR with desired default or from the environment if provided
: ${XSD_DEST_DIR:=/mbari/LRAUV/xsd}

DO_STYLE=0
DO_SCHEMA=0
DO_DOCS=0
DO_4_1=0
DO_BUILD=0
DO_HOST=0
DO_TOOLDIR=0
TOOLDIR_SET=0
DO_COV=0
DO_TARGET=0
TOOLDIR="~/workspace/lrauv-application/Tools"
TARGET=""

for arg in "$@"
do
    case "$arg" in
    style)  DO_STYLE=1
            ;;
    schema) DO_SCHEMA=1
            ;;
    docs)   DO_DOCS=1
            ;;
    4.1)    DO_4_1=1
            ;;
    build)  DO_BUILD=1
            ;;
    host)   DO_BUILD=1;DO_HOST=1
            ;;
    tooldir) DO_TOOLDIR=1
            ;;
    cov)    DO_COV=1
            ;;
    *)      if [ $DO_TOOLDIR == 1 ] && [ $TOOLDIR_SET == 0 ]
            then
              TOOLDIR="$arg"
              TOOLDIR_SET=1
            else
              DO_TARGET=1
              TARGET="$arg"
            fi
            ;;
    esac
done

#echo DO_STYLE=$DO_STYLE
#echo DO_DOCS=$DO_DOCS
#echo DO_4_1=$DO_4_1
#echo DO_HOST=$DO_HOST
#echo DO_TOOLDIR=$DO_TOOLDIR
#echo TOOLDIR=$TOOLDIR
#echo DO_TARGET=$DO_TARGET
#echo TARGET=$TARGET

# to use newer git version (2.1.3)
PATH=/usr/local/git/bin:$PATH:/usr/local/bin/

echo "Starting build"

cd ~/workspace/lrauv-application/ || exit 30

echo "PWD: `pwd`"
echo "GIT: `git --version`"

# reset target, via a hard reset
# let's not do this for now!
#stty -F /dev/ttyUSB0 litout 9600; echo AM1 > /dev/ttyUSB0

# clean before update, to ensure thorough clean
echo
echo "cleaning..."
make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ distclean > /dev/null

# Update from repository
echo
echo "updating from repository"
cd ~/workspace/lrauv-application/
echo "git pull origin master"
git pull origin master || exit 31
echo "git submodule update --recursive"
git submodule update --recursive || exit 32

# Clean Logs directory
rm -rf ~/workspace/lrauv-application/Logs/*

if [ "$DO_STYLE" = "1" ]; then
  # Run cxxChecker to check style
  echo
  echo "Checking style"
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ cxxcheck
fi

if [ "$DO_SCHEMA" = "1" ]; then
  # generate schemas
  echo
  echo "Generating schemas"
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ schema > /dev/null
fi

if [ "$DO_DOCS" = "1" ]; then
  # Make Documentation
  echo
  echo "Building documentation"
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ doxygen > /dev/null
fi

if [ "$DO_4_1" = "1" ]; then
  # Build for g++ 4.1 compiler.
  echo
  echo "building host for g++ 4.1"
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ distclean > /dev/null
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ 4.1 > /dev/null
fi

if [ "$DO_BUILD" = "1" -o "$DO_HOST" = "1" ]; then
  # Build for latest compiler, build test
  echo
  echo "building host for latest g++"
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ distclean > /dev/null
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ all > /dev/null
  echo
fi

if [ "$DO_HOST" = "1" ]; then
  echo "running tests"
  cd ~/workspace/lrauv-application/

  # Run unit tests
  if [ -f bin/LRAUVTest ]; then
    bin/LRAUVTest > /dev/null
  else
    echo bin/LRAUVTest does not exist
  fi

  # Run functional tests
  if [ -f Tools/regression/runTest.py ]; then
    python Tools/regression/runTest.py
  else
    echo Tools/regression/runTest.py does not exist
  fi

  # Copy useful host files to TOOLDIR
  echo
  echo "copying SimDaemon to $TOOLDIR/SimDaemon"
  cp ~/workspace/lrauv-application/bin/SimDaemon $TOOLDIR/SimDaemon
  echo
  echo "copying unserialize to $TOOLDIR/unserialize"
  cp ~/workspace/lrauv-application/bin/unserialize $TOOLDIR/unserialize
fi

if [ "$DO_COV" = "1"  ]; then
  # Do target build under auspices of Coverity
  #PATH=$PATH:/usr/local/coverity/cov-sa-linux-5.3.0/bin
  PATH=$PATH:/home/tethys/coverity/cov-sa-linux-5.3.0/bin
  echo
  echo "cleaning for Coverity build"
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ distclean > /dev/null
  rm -rf ~/coverity_int_dir/*
  echo
  echo "Building for target with Coverity"
  cov-build --dir ~/coverity_int_dir/ make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ lpc3250test > /dev/null
  cov-analyze --enable ARRAY_VS_SINGLETON --enable BAD_COMPARE --enable BAD_FREE \
              --enable CHAR_IO --enable DEADCODE --enable USE_AFTER_FREE \
              --enable EVALUATION_ORDER --enable FORWARD_NULL \
              --enable INFINITE_LOOP --enable MISSING_BREAK \
              --enable MISSING_RETURN --enable NEGATIVE_RETURNS \
              --enable NO_EFFECT --enable NULL_RETURNS \
              --enable OVERRUN_DYNAMIC --enable OVERRUN_STATIC \
              --enable RESOURCE_LEAK --enable RETURN_LOCAL \
              --enable REVERSE_NEGATIVE --enable REVERSE_INULL \
              --enable SIGN_EXTENSION --enable SIZECHECK --enable UNINIT \
              --enable UNREACHABLE --enable VARARGS --enable UNUSED_VALUE \
              --enable CHECKED_RETURN \
              -en PARSE_ERROR -en MISSING_LOCK -en INTEGER_OVERFLOW \
              -en READLINK -en BAD_ALLOC_ARITHMETIC -en BAD_ALLOC_STRLEN \
              -en DELETE_VOID -en STREAM_FORMAT_STATE -en ASSERT_SIDE_EFFECT \
              -en UNIMPL_FUNCTIONS -en SECURE_CODING -en STACK_USE \
              -en USER_POINTER -en ATOMICITY \
              --dir ~/coverity_int_dir/
  cov-commit-defects --host tellum.shore.mbari.org --port 5467 --user tethys --stream Tethys --password lrauv2008 --dir ~/coverity_int_dir/
elif [ "$DO_TARGET" = "1" ]; then
  echo
  echo "cleaning for target build"
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ distclean > /dev/null
  make XSD_DEST_DIR=${XSD_DEST_DIR} -C ~/workspace/lrauv-application/Build/ lpc3250test > /dev/null
fi

if [ "$DO_TARGET" = "1" ]; then
  # Copy files to the target
  echo
  echo "copying files to target $TARGET"
  cd ~/workspace/lrauv-application/;scp -Cpr bin Config Missions Modules Resources root@$TARGET:/mnt/mmc/LRAUV/

  #Run tests on the target
  echo
  echo "Running LRAUVTest unit tests on target $TARGET"
  ssh root@$TARGET "cd /mnt/mmc/LRAUV;bin/LRAUVTest > /dev/null"
fi
