#!/bin/sh
#
# Script to generate debug.h
# Output goes to stdout, this should be redirected by the caller

# Configuration
DEBUG_LEVELS=4
DEBUG_PARTS="MAIN ARGPARSE SYSFS CPUFREQ LOG"
DEBUG_HELPER_DEF=`grep -o "void .*()" debug.c | head -n 1`
DEBUG_HELPER=${DEBUG_HELPER_DEF##* }
HEXDUMP_HELPER_DEF=`grep -o "void .*(unsigned char \*data,int len)" debug.c`
HEXDUMP_HELPER=${HEXDUMP_HELPER_DEF#* }
HEXDUMP_HELPER=${HEXDUMP_HELPER%%(*}

# Generate the configuration part
cat << EOF
#ifndef HAVE_DEBUG_H
#define HAVE_DEBUG_H

// Configuration
EOF

for PART in ALL $DEBUG_PARTS
do
 VALUE=0
 if [ -r "debug.h" ]
 then
  VALUE=`grep "^#define SET_DEBUG_$PART " debug.h | head -n 1 | awk '{ print $NF }' | tr -cd "[0-9]"`
 fi
 if [ -n "$VALUE" ]
 then
  echo "#define SET_DEBUG_$PART $VALUE"
 else
  echo "#define SET_DEBUG_$PART 0"
 fi
done

for SETTING in SET_DEBUG_PRINT_LEVEL SET_DEBUG_PRINT_TIME SET_DEBUG_PRINT_LINE SET_DEBUG_PRINT_FUNCTION
do
 if [ -r "debug.h" ]
 then
  if grep -q "^#define $SETTING" debug.h
  then
   echo "#define $SETTING"
  else
   echo "//#define $SETTING"
  fi
 else
  echo "#define $SETTING"
 fi
done
echo "// End of configuration"
echo ""

# Configure prefix
echo "// Set prefix"
if [ -n "$DEBUG_PREFIX_CONDITION" ]
then
 echo "$DEBUG_PREFIX_CONDITION"
fi
echo "#define SET_DEBUG_PRINT_PREFIX \"$DEBUG_PREFIX\""
if [ -n "$DEBUG_PREFIX_CONDITION" ]
then
 echo "#endif"
fi
echo ""

# Generate the part to make ALL work
echo "// Make ALL work"
for PART in $DEBUG_PARTS
do
 echo "#if (SET_DEBUG_ALL>SET_DEBUG_$PART)"
 echo " #undef SET_DEBUG_$PART"
 echo " #define SET_DEBUG_$PART SET_DEBUG_ALL"
 echo "#endif"
 echo ""
done

# Check if we need stdio pr the helper
echo "// Check if we need stdio.h or the helpers"
for PART in $DEBUG_PARTS
do
 echo "#if (SET_DEBUG_$PART>0)"
 echo " #ifndef NEED_STDIO_H"
 echo "  #define NEED_STDIO_H"
 echo " #endif"
 echo " #ifdef SET_DEBUG_PRINT_TIME"
# echo "  #ifdef NEED_DEBUG_HELPER"
# echo "   #undef NEED_DEBUG_HELPER"
# echo "  #endif"
 echo "  #ifndef NEED_DEBUG_HELPER"
 echo "   #define NEED_DEBUG_HELPER"
 echo "  #endif"
 echo " #endif"
 echo " #ifndef NEED_HEXDUMP_HELPER"
 echo "  #define NEED_HEXDUMP_HELPER"
 echo " #endif"
 echo "#endif"
 echo ""
done

# Print the format definitions
# Define printing of prefixes
cat <<EOF
// Define the printing of the debug prefix
#ifdef SET_DEBUG_PRINT_PREFIX
 #define DEBUG_PRINT_PREFIX fprintf(stderr,"%s",SET_DEBUG_PRINT_PREFIX);
#else
 #define DEBUG_PRINT_PREFIX
#endif

EOF
# Generate the ammount of levels requested
echo "// Define the printing of debuglevels"
echo "#ifdef SET_DEBUG_PRINT_LEVEL"
for((COUNT=1;COUNT<=$DEBUG_LEVELS;COUNT++))
do
 echo " #define DEBUG_PRINT_LEVEL$COUNT fprintf(stderr,\"$COUNT: \");"
done
echo "#else"
for((COUNT=1;COUNT<=$DEBUG_LEVELS;COUNT++))
do
 echo " #define DEBUG_PRINT_LEVEL$COUNT"
done
echo "#endif"
echo ""

echo "// Debug timestamps definition"
echo "#ifdef SET_DEBUG_PRINT_TIME"
#echo "#define NEED_DEBUG_HELPER"
echo " #define DEBUG_PRINT_TIME $DEBUG_HELPER;"
echo "#else"
echo " #define DEBUG_PRINT_TIME"
echo "#endif"
echo ""

cat << EOF
// Define the printing of file names and line numbers
#ifdef SET_DEBUG_PRINT_LINE
 #define DEBUG_PRINT_LINE fprintf(stderr,"%s:%d: ",__FILE__,__LINE__);
#else
 #define DEBUG_PRINT_LINE
#endif

// Define the printing of function names
#ifdef SET_DEBUG_PRINT_FUNCTION
 #define DEBUG_PRINT_FUNCTION fprintf(stderr,"%s: ",__func__);
#else
 #define DEBUG_PRINT_FUNCTION
#endif

EOF

echo "// Define debugging format"
for((COUNT=1;COUNT<=$DEBUG_LEVELS;COUNT++))
do
 echo "#define DEBUG${COUNT}_FORMAT(...) \\"
 echo " DEBUG_PRINT_PREFIX \\"
 echo " DEBUG_PRINT_LEVEL${COUNT} \\"
 echo " DEBUG_PRINT_TIME \\"
 echo " DEBUG_PRINT_LINE \\"
 echo " DEBUG_PRINT_FUNCTION \\"
 echo " fprintf(stderr,__VA_ARGS__);"
 echo ""
done

# Generate the debug functions
echo "// Define the functions"
for PART in $DEBUG_PARTS
do
 for((COUNT=1;COUNT<=$DEBUG_LEVELS;COUNT++))
 do
  echo "#if (SET_DEBUG_$PART>$((COUNT-1)))"
  echo " #define DEBUG${COUNT}_$PART(...) DEBUG${COUNT}_FORMAT(__VA_ARGS__)"
  echo " #define HEXDUMP${COUNT}_$PART(data,datalen) $HEXDUMP_HELPER(data,datalen);"
  echo "#else"
  echo " #define DEBUG${COUNT}_$PART(...)"
  echo " #define HEXDUMP${COUNT}_$PART(data,datalen)"
  echo "#endif"
  echo ""
 done
done

# Include stdio when needed
echo "#ifdef NEED_STDIO_H"
echo " #include <stdio.h>"
echo "#endif"

# Define helper function
echo "#ifdef NEED_DEBUG_HELPER"
echo " $DEBUG_HELPER_DEF;"
echo "#endif"

# Define hexdump function
echo "#ifdef NEED_HEXDUMP_HELPER"
echo " $HEXDUMP_HELPER_DEF;"
echo "#endif"

# Finish the ifdef HAVE_DEBUG_H
echo ""
echo "#endif"