#
#  satnogs-flowgraphs: SatNOGS GNU Radio flowgraphs
#
#  Copyright (C) 2020, 2026
#  Libre Space Foundation <http://libre.space>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>
#

set(hier_flowgraphs
    fsk_coherent_generator.grc
    psk_generator.grc
)

execute_process(
    COMMAND gnuradio-config-info --persistentdir
    OUTPUT_VARIABLE gnuradio_hier_path
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET
    RESULT_VARIABLE grc_info_out
)

# Add support for older versions of GNU Radio
if (NOT ${grc_info_out} EQUAL 0)
    set(gnuradio_hier_path "$ENV{HOME}/.grc_gnuradio")
endif()

foreach(grc_file ${hier_flowgraphs})
    get_filename_component(fname ${grc_file} NAME_WE)
    add_custom_command(OUTPUT "${gnuradio_hier_path}/satnogs_${fname}.py"
                              "${gnuradio_hier_path}/satnogs_${fname}.block.yml"
        COMMAND ${grcc} ${grc_file} -u
	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
	DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${grc_file}
    )

    add_custom_target(${fname} ALL 
        DEPENDS "${gnuradio_hier_path}/satnogs_${fname}.block.yml"
                "${gnuradio_hier_path}/satnogs_${fname}.py")

endforeach()
