#!/usr/local/bin/bash
#
#
#======================================================================
#
#  Copyright (c) 2000 Qualcomm Incorporated.  All rights reserved.
#  The file License.txt specifies the terms for use, modification,
#  and redistribution.
#
#
# Revisions:
#
#    08/20/00 [rcg]
#            - Added vlogit and BLATHER.
#
#    07/20/00 [rcg]
#            - File added.
#
#======================================================================
#
#
# File: check-fmt
#
# Purpose:
#     This script checks for mismatched formats and parameters in
#     pop_msg, pop_log, logit, vlogit, BLATHERx, and DEBUG_LOGx calls.
*     Possible mismatches are ploced in the file FMT_WARNINGS.
#
#======================================================================


#------
#   Build the sed editing script.
#------
    cat >temp-sed-edit <<\EOF
s/pop_msg *( *p, *POP_[A-Z]*, *HERE,/printf ( /g
s/pop_log *( *p, *POP_[A-Z]*, *HERE,/printf ( /g
s/logit *( *[a-zA-Z]*, *[A-Z_]*, *HERE,/printf (/g
s/vlogit *( *[a-zA-Z]*, *[A-Z_]*, *HERE,/printf (/g
s/DEBUG_LOG[0-9]* *( *p,/printf (/g
s/BLATHER[0-9]* *( *p,/printf (/g
EOF


#------
#   Get list of all affected source files.
#------
FILE_LIST=`egrep -l "pop_msg|pop_log|logit|vlogit|DEBUG_LOG|BLATHER" */*.c`


#------
#   Loop through all source files, copying to *.preserve,
#   and replacing the original with an edited version.
#------
for file in $FILE_LIST
do
    echo "editing $file..."
    TEMP_NAME="${file}.preserve"
    if test -f $TEMP_NAME
    then
        echo "  >>> file $TEMP_NAME exists! <<<"
        exit 1
    fi
    mv $file $TEMP_NAME

    sed -f temp-sed-edit $TEMP_NAME > $file
done


#------
#   Run configure script, specifying warnings.
#   Additional compilation options can be passed
#   in to us, to allow checking additional code
#   paths.
#------
echo -n "Running ./configure..."
./configure --enable-warnings $@ >/dev/null
echo ".  Done."


#------
#   Run make, saving errors/warnings to FMT-WARNINGS.
#
#   There will be a lot of useless warnings as well; we
#   could grep for "format", but it's not a bad idea to
#   glance at the other warnings as well.  One can always
#   use 'fgrep warnings FMT-WARNINGS' instead of
#   'more FMT-WARNINGS' anyway.
#------
echo -n "Compiling..."
make >/dev/null 2>FMT-WARNINGS
if test -s FMT-WARNINGS
then
    echo ".  Done.  Warnings are in FMT-WARNINGS"
else
    rm -f FMT-WARNINGS
    echo ".  Done.  No warnings generated."
fi


#------
#   Put everything back the way we found it.
#------
rm -f temp-sed-edit

for file in $FILE_LIST
do
    TEMP_NAME="${file}.preserve"
    if test -s $TEMP_NAME
    then
        mv -f $TEMP_NAME $file
    fi
done
