#!/bin/bash

. buildsys/functions

ProcessArgs $*
Assert LoadConfigs
Assert SetupResFile

cd ../../..

SetState "Configuring"

if [ "$DEP" != "none" ]
then
   OSPL_HOME=$DEPWORKDIR
   export OSPL_HOME
fi

SPLICE_PLATFORM=$SETUP_TYPE
export SPLICE_PLATFORM
. ./configure -full
if [ "$CONFIGURATION" != "OK" ]
then
    echo "ERROR: configuration - dcps_build"
    exit 1
fi

SetState "Building"

echo "BUILD=RUNNING" >> $RESFILE
ArchiveLogs

if [ "$COVERITY" = "yes" ]
then
    #VER will have tags/ or heads/ on the front
    BRANCH_NAME=`echo $VER | cut -f 2 -d '/'`
    echo "++++++++COVERITY RUN"
    . $OSPL_OUTER_HOME/build/scripts/overnight/coverity_common
    #Regen the model file as they are not compatible between versions
    #and this only takes seconds
    if [ -f "$MODEL_DB_FILE" ]
    then
        echo "++++++++COVERITY removing $MODEL_DB_FILE"
        rm -f $MODEL_DB_FILE
    fi
    echo "++++++++COVERITY generating $MODEL_DB_FILE from $MODEL_FILE"
    echo "++++++++COMMAND [cov-make-library --concurrency -of $MODEL_DB_FILE $MODEL_FILE]"
    cov-make-library --concurrency -of $MODEL_DB_FILE $MODEL_FILE
    if [ $? != 0 ]
    then
        exit 2
    fi
    #Configure for each target compiler we use, this really only
    #needs to be done once, only warns about each time after that
    for i in $COMPILER_CONFIG
    do
        echo "++++++++COVERITY configuring $i"
        echo "++++++++COMMAND [cov-configure $i]"
        cov-configure $i
    done
    BUILD_CMD="cov-build --dir $CACHE_DIR sh -c"
    echo "++++++++COVERITY building CACHE_DIR - $CACHE_DIR"
    echo "++++++++COMMAND [$BUILD_CMD make VERBOSE=yes]"
    #Clean the cache dir as the unique directory path makes coverity
    #count all the source as new again
    if [ -d "$CACHE_DIR" ]
    then
        rm -rf $CACHE_DIR
    fi
fi

$BUILD_CMD make VERBOSE=yes
if [ $? != 0 ]
then
    exit 2
fi

if [ "$COVERITY" = "yes" ]
then
    STREAM="CandC++_${BRANCH_NAME}_${HOSTNAME}_${SPLICE_PLATFORM}"
    echo "++++++++COVERITY analyze CandC++/Java"
    echo "++++++++COVERITY stream setup is $STREAM"
    echo "++++++++COMMAND [cov-analyze --dir $CACHE_DIR --user-model-file $MODEL_DB_FILE $COV_ANALYSIS_OPTIONS $COV_ANALYSIS_EXTRA_OPTIONS]"
    cov-analyze --dir $CACHE_DIR --user-model-file $MODEL_DB_FILE $COV_ANALYSIS_OPTIONS $COV_ANALYSIS_EXTRA_OPTIONS
    if [ $? != 0 ]
    then
        exit 2
    fi
    echo "++++++++COVERITY commiting defects to $STREAM"
    echo "++++++++COMMAND [cov-commit-defects --dir $CACHE_DIR --host $COV_SERVER --port $COV_PORT --user $COV_USER --password $COV_PASSWORD --stream $STREAM]"
    cov-commit-defects --dir $CACHE_DIR --host $COV_SERVER --port $COV_PORT --user $COV_USER --password $COV_PASSWORD --stream $STREAM
    if [ $? != 0 ]
    then
        exit 2
    fi
fi

# Create all features file as a reference for the features enabled found on the scoreboard.
find $OSPL_HOME -name 'setup_*' | xargs grep 'INCLUDE_' | awk '{ print $2 }' | grep 'INCLUDE_' | sort | uniq >> $OSPL_HOME/all_features.txt
find $OSPL_OUTER_HOME -name 'setup_*' | xargs grep 'INCLUDE_' | awk '{ print $2 }' | grep 'INCLUDE_' | sort | uniq >> $OSPL_HOME/all_features.txt

exit 0
