#!/bin/bash
# Generate a keywords.txt for the library that is suitable for the Arduino IDE.
# Expects to run from the top directory of the library.

# Set the locale for sorting
export LC_ALL=C

cat << EndOfTextEndOfTextEndOfText
#########################################
# Syntax Coloring Map For IRremoteESP8266
#########################################

################################################
# WARNING: Do NOT edit this file directly.
#          It is generated by 'tools/mkkeywords'
#          e.g. tools/mkkeywords > keywords.txt
################################################

#######################################################
# The Arduino IDE requires the use of a tab separator
# between the name and identifier. Without this tab the
# keyword is not highlighted.
#
# Reference: https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#keywords
#######################################################

#######################################
# Datatypes & Classes (KEYWORD1)
#######################################

EndOfTextEndOfTextEndOfText

CLASSES=$(grep "^class " src/*.h | cut -d' ' -f2 | sort -u)
# Manually add typedefs as they are hard to parse out.
CLASSES="${CLASSES} ir_params_t match_result_t"
for i in ${CLASSES}; do
  echo -e "${i}\tKEYWORD1"
done | sort -du

cat << EndOfTextEndOfTextEndOfText

#######################################
# Methods and Functions (KEYWORD2)
#######################################

EndOfTextEndOfTextEndOfText

METHODS=$(egrep "^(uint|void|bool|String|static|match_result_t).*\(" src/*.cpp |
          cut -d' ' -f2- | sed 's/^.*:://' | cut -d'(' -f1 | sort -u |
          grep -v ICACHE_RAM_ATTR)
for i in ${METHODS}; do
  echo -e "${i}\tKEYWORD2"
done | sort -u

cat << EndOfTextEndOfTextEndOfText

#######################################
# Constants (LITERAL1)
#######################################

EndOfTextEndOfTextEndOfText
LITERALS=$(grep "^#define [A-Z]" src/*.cpp src/*.h |
           while read ignore define ignore; do
             echo ${define};
           done | sort -u |
           grep -v [\(\)] | grep -v ^_ | grep -v _\$ | grep -v VIRTUAL)
ENUMS=$(cat src/*.h | while read a b; do
          if [[ ${a} == "};" ]]; then
            ENUM=0;
          fi;
          if [[ ${ENUM} -eq 1 ]]; then
            echo $a | sed 's/,//g';
          fi;
          if [[ ${a} == "enum" ]]; then
            ENUM=1;
          fi;
        done)
for i in ${LITERALS} ${ENUMS}; do
  echo -e "${i}\tLITERAL1"
done | sort -u
