#!/usr/bin/env bash
# $LastChangedDate: 2010-05-25 21:26:05 -0400 (Tue, 25 May 2010) $
# LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG
# Do some checks before compiling.
#-------------------------------------------------------------------------------

SRC_DIR=$1
[ -d $SRC_DIR ] || exit 1

# Catch: iter < end()
if ( grep -i "iter.* < .*end[(]" $SRC_DIR/*.* ); then
    echo "ERROR: iter < end() is wrong!" >/dev/stderr
    exit 1
fi

# A habitual mistake that causes a buffer overrun is passing sizeof()
# to InitArray() which expects an element count, not a byte count.
if ( grep "InitArray.*sizeof" $SRC_DIR/*.* ); then  # not grep -q
    echo "ERROR: sizeof() cannot be passed to InitArray()" >/dev/stderr
    exit 1
fi

# Catch writing #if DEBUG2.  #if DEBUG == 2 is correct.
if ( grep "if.*DEBUG2" $SRC_DIR/*.* ); then
    echo "ERROR: should be DEBUG==2" >/dev/stderr
    exit 1
fi

# Catch this mistake THREAD_CODE( Atomic::Add() )
# because when compiled single-thread it expands nothing.
if ( grep "\(^\|[^/]\)THREAD_CODE.*Atomic" $SRC_DIR/*.* ); then
    echo "ERROR: THREAD_CODE( Atomic ) expands nothing if ! COMPILE_THREADS" >/dev/stderr
    exit 1
fi

# Should be COMPILE_THREADS (plural).
if ( grep "if COMPILE_THREAD$" $SRC_DIR/*.* ) || ( grep "if COMPILE_THREAD " $SRC_DIR/*.* ); then
    echo "ERROR: Should be COMPILE_THREADS (plural)." >/dev/stderr
    exit 1
fi

# shptr shouldn't be a reference.
FILES=
for FILE in $SRC_DIR/*.*
do
    if ( echo $FILE | grep -q -v shptr ); then
        FILES="$FILES $FILE"
    fi
done
if ( grep "shptr<.*>&" $FILES ); then
    echo "ERROR: SharedPtr/shptr shouldn't be a reference" >/dev/stderr
    exit 1
fi

exit 0
