#!/bin/sh
#
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
#
#
# Copyright (c) 2007 Alexander Levin
#
#
# This script produces the file "menu_call_stats_list.c" containing
# pointers to the call statistics for all menu items in the core RockBox.
# Files with menu items that should get into this list must be compiled
# so that all the temporary files are kept (in particular, the result of
# the preprocessing). Usually this is done by including '-save-temps' into
# the list of the compiler options. In that case, the preprocessed files
# have the extension '.i' and are stored in the same directory as the
# original '.c' files.

BASE_NAME = "$OBJDIR/menu_call_stats_list"
TMP_NAME = "$BASE_NAME.tmp"
OUT_NAME = "$BASE_NAME.c"
STRUCT = "struct s_call_stats"
VARNAME = "menu_call_stats_list"

rm -f $OUT_NAME

# Process all *.i files (results of the preprocessing), find strings where
# stat structs are defined, isolate the definitions, and put them all to a
# temporary file.
find $APPSDIR -name "*.i" -exec grep "$STRUCT item_call" {} \; | sed -e "s/.*\($STRUCT item[^ ]*\) .*/\1/" | sort > $TMP_NAME

#
# Now produce the .c file with the array containing addresses of all the structs.
#
echo "/* This file was generated automatically. Please do not edit. */" > $OUT_NAME
echo "" >> $OUT_NAME
echo "#include \"menu.h\"" >> $OUT_NAME
echo "" >> $OUT_NAME
cat $TMP_NAME | sed -e "s/\(.*\)/extern \1;/" >> $OUT_NAME
echo "" >> $OUT_NAME
echo "$STRUCT * const $VARNAME[] = {" >> $OUT_NAME
cat $TMP_NAME | sed -e "s/.* \(.*\)/    \&\1,/" >> $OUT_NAME
echo "};" >> $OUT_NAME
echo "" >> $OUT_NAME
echo "const int $VARNAME_length = sizeof($VARNAME)/sizeof($VARNAME[0]);" >> $OUT_NAME

# Delete the temporary file
rm $TMP_NAME
