#!/bin/sh
#
# This script scans the whole source code for symbols of the form dprintf_xxx,
# generates the necessary macro definitions and puts them into the files
# include/stddebug.h and include/debug.h . This script must be started with 
# cwd = rootdir of the Wine-distribution.
#
# Michael Patra  <micky@marie.physik.tu-berlin.de>
#
DEBUG_H=include/debug.h
STDDEBUG_H=include/stddebug.h

mv $DEBUG_H $DEBUG_H.old
mv $STDDEBUG_H $STDDEBUG_H.old

# Build the list of debug identifiers
grep -h dprintf_ */*.c | sed 's/.*dprintf_\([A-Za-z0-9_]*\).*/\1/g' | \
	sort | uniq > temp.$$

# Build debug.h

sed '/^\/\* Do not remove this line or change anything below this line \*\//q'\
      <$DEBUG_H.old >$DEBUG_H
cat <<++EOF++ >> $DEBUG_H

#ifdef DEBUG_NONE_EXT
++EOF++

cat temp.$$ | tr a-z A-Z |
{
while read x
do
	echo "#undef DEBUG_$x" >> $DEBUG_H
done
}

cat <<++EOF++ >>$DEBUG_H
#endif

#ifdef DEBUG_ALL_EXT
++EOF++

cat temp.$$ | tr a-z A-Z |
{
while read x
do
	echo "#define DEBUG_$x" >> $DEBUG_H
done
}

cat <<++EOF++ >>$DEBUG_H
#endif

#ifdef DEBUG_RUNTIME
#ifdef DEBUG_DEFINE_VARIABLES
short debug_msg_enabled[]={
++EOF++

cat temp.$$ | tr a-z A-Z |
{
while read x
do
	cat <<++EOF++ >>$DEBUG_H
#ifdef DEBUG_$x
    1,
#else
    0,
#endif
++EOF++

done
}

cat <<++EOF++ >>$DEBUG_H
    0
};
#else
extern short debug_msg_enabled[];
#endif
#endif

++EOF++

i=0
cat temp.$$ |
{
while read x 
do
	y=`echo $x | tr a-z A-Z`
	cat <<++EOF++ >>$DEBUG_H
#ifdef DEBUG_RUNTIME
#define dprintf_$x if(!debug_msg_enabled[$i]) ; else fprintf
#define debugging_$x debug_msg_enabled[$i]
#else
#ifdef DEBUG_$y
#define dprintf_$x fprintf
#define debugging_$x 1
#else
#define dprintf_$x while(0) fprintf
#define debugging_$x 0
#endif
#endif

++EOF++
	let i=$i+1
done
}

cat <<++EOF++ >>$DEBUG_H

#ifdef DEBUG_RUNTIME
#ifdef DEBUG_DEFINE_VARIABLES
static char *debug_msg_name[] = {
++EOF++

cat temp.$$ | 
{
while read x
do
	echo "    \"$x\"," >> $DEBUG_H
done
}

cat <<++EOF++ >>$DEBUG_H
    ""
};
#endif
#endif
++EOF++

# Build stddebug.h

sed '/^\/\* Do not remove this line or change anything below this line \*\//q'\
      <$STDDEBUG_H.old >$STDDEBUG_H

cat <<++EOF++ >>$STDDEBUG_H

#ifdef DEBUG_NONE
++EOF++

cat temp.$$ | tr a-z A-Z |
{
while read x
do
	echo "#undef DEBUG_$x" >> $STDDEBUG_H
done
}

cat <<++EOF++ >>$STDDEBUG_H
#endif

#ifdef DEBUG_ALL
++EOF++

cat temp.$$ | tr a-z A-Z |
{
while read x
do
	echo "#define DEBUG_$x" >> $STDDEBUG_H
done
}
echo "#endif" >> $STDDEBUG_H

rm temp.$$ $DEBUG_H.old $STDDEBUG_H.old