# - Look for GNU Bison++, the parser generator
# Based off a news post from Andy Cedilnik at Kitware
# Defines the following:
#  BISONPP_EXECUTABLE - path to the bison executable
#  BISONPP_FILE - parse a file with bison
#  BISONPP_PREFIX_OUTPUTS - Set to true to make BISON_FILE produce prefixed
#                         symbols in the generated output based on filename.
#                         So for ${filename}.y, you'll get ${filename}parse(), etc.
#                         instead of yyparse().
#  BISONPP_GENERATE_DEFINES - Set to true to make BISON_FILE output the matching
#                           .h file for a .c file. You want this if you're using
#                           flex.

#Modified by Markus for bison++

IF(NOT DEFINED BISONPP_PREFIX_OUTPUTS)
 SET(BISONPP_PREFIX_OUTPUTS FALSE)
ENDIF(NOT DEFINED BISONPP_PREFIX_OUTPUTS)

IF(NOT DEFINED BISONPP_GENERATE_DEFINES)
 SET(BISONPP_GENERATE_DEFINES FALSE)
ENDIF(NOT DEFINED BISONPP_GENERATE_DEFINES)

IF(NOT BISONPP_EXECUTABLE)
 MESSAGE(STATUS "Looking for bison++")
 FIND_PROGRAM(BISONPP_EXECUTABLE bison++)
 IF(BISONPP_EXECUTABLE)
   MESSAGE(STATUS "Looking for bison++ -- ${BISONPP_EXECUTABLE}")
 ENDIF(BISONPP_EXECUTABLE)
ENDIF(NOT BISONPP_EXECUTABLE)

IF(BISONPP_EXECUTABLE)
 MACRO(BISONPP_FILE FILENAME)
   GET_FILENAME_COMPONENT(PATH "${FILENAME}" PATH)
   IF("${PATH}" STREQUAL "")
     SET(PATH_OPT "")
   ELSE("${PATH}" STREQUAL "")
     SET(PATH_OPT "/${PATH}")
   ENDIF("${PATH}" STREQUAL "")
   GET_FILENAME_COMPONENT(HEAD "${FILENAME}" NAME_WE)
   IF(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}")
     FILE(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}")
   ENDIF(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}")
   IF(BISONPP_PREFIX_OUTPUTS)
     SET(PREFIX "${HEAD}")
   ELSE(BISONPP_PREFIX_OUTPUTS)
     SET(PREFIX "yy")
   ENDIF(BISONPP_PREFIX_OUTPUTS)
   SET(OUTFILE "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}/${HEAD}.tab.cpp")
   IF(BISONPP_GENERATE_DEFINES)
     SET(HEADER "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}/${HEAD}.tab.h")
     ADD_CUSTOM_COMMAND(
       OUTPUT "${OUTFILE}" "${HEADER}"
       COMMAND "${BISONPP_EXECUTABLE}"
       ARGS "--name-prefix=${PREFIX}"
       "--defines"
       "--output-file=${OUTFILE}"
       "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}"
       DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}")
     SET_SOURCE_FILES_PROPERTIES("${OUTFILE}" "${HEADER}" PROPERTIES GENERATED TRUE)
     SET_SOURCE_FILES_PROPERTIES("${HEADER}" PROPERTIES HEADER_FILE_ONLY TRUE)
   ELSE(BISONPP_GENERATE_DEFINES)
     ADD_CUSTOM_COMMAND(
       OUTPUT "${OUTFILE}"
       COMMAND "${BISONPP_EXECUTABLE}"
       ARGS "--name-prefix=${PREFIX}"
       "--output-file=${OUTFILE}"
       "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}"
       DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}")
     SET_SOURCE_FILES_PROPERTIES("${OUTFILE}" PROPERTIES GENERATED TRUE)
   ENDIF(BISONPP_GENERATE_DEFINES)
 ENDMACRO(BISONPP_FILE) 
ENDIF(BISONPP_EXECUTABLE)
