### TODO:
### ObjectListAll
### Macro Editor resource files
### LOTS MORE!


set(files
	AnnotationViewDef AnnotationViewRules
	AuxPrepBufrTable_B.txt AuxPrepBufrTable_D.txt
	AxisDef AxisRules
	BinningObjectDef BinningObjectRules
	CartesianViewDef CartesianViewRules
	CleanFileDef
	Coastlines
	CoastDef
	CoastRules
	ConfigRules
	ContDef
	ContRules
	DatacoverageDef
	DatacoverageRules
	DeviceDriverDef
	DeviceDriverRules
	DivrotDef
	DivrotRules
	DrawingPriorityDef
	DrawingPriorityRules
	EcfsDef
	EPSOutputDef
	GenAppDef
	GenAppRules
	GeoToGribDef
	GeoToGribRules
	GeoViewDef GeoViewRules
	GraphDef
	GraphRules
	GribToGeoDef
	Input_Window
	KMLOutputDef
	MARS_none
	MARS_local
	MARS_remote
	MARS_webapi
	MAXISDef MAXISRules
    MBOXPLOTDef MBOXPLOTRules
	MCOASTDef MCOASTRules
	MCONTDef MCONTRules
	MGRAPHDef MGRAPHRules
	MLEGENDDef MLEGENDRules
	MIMPORTDef MIMPORTRules  
	MSYMBDef MSYMBRules
	MTAYLORDef MTAYLORRules
	MTEXTDef MTEXTRules
	MWINDDef MWINDRules
	MacroDef
	MacroParamDef
	MacroParamRules
	MacroRules
	MagicsColors.h
	MapViewDef
	MapViewRules
	MarsDef
	MarsRules
	ObjectList
	ObjectList.forTesting
	ObsFilterDef
	ObsFilterRules
	ObsPlottingDef
	ObsPlottingRules
	OverlayControl
	OverlayControlDef
	OverlayControlRules
	PageFrameDef
	PageFrameRules
	PDFOutputDef
	PlotPageDef
	PlotPageRules
	PlotSubPageDef
	PlotSubPageRules
	PlotSuperPageDef
	PlotSuperPageRules
	Plot_Window_for_Image
	PNGOutputDef
	PottFDef
	PottFRules
	PSOutputDef
	RelHumDef
	RelHumRules
	Services
	SimpleFormulaDef
	SimpleFormulaRules
	StationDef
	StationRules
	SubpageFrameDef
	SubpageFrameRules
	SVGOutputDef
	SymbDef SymbRules
	TableCommonDef TableReaderDef TableReaderRules
	TextDef TextRules
	UiSpec.Bookmarks
	UiSpec.Breadcrumbs
	UiSpec.DetailedFolderView
	UiSpec.DrawerView
	UiSpec.FolderDrawerPanel
 	UiSpec.FolderHistory
	UiSpec.FolderPanel
	UiSpec.FolderView
 	UiSpec.IconFolderView
 	UiSpec.IconHelpView 
	UiSpec.IconHolderView
 	UiSpec.TemplateDrawerView
	UiSpec.ViewDrawerView
	VelstrDef VelstrRules
	VoidDef
	WidgetDef
	WindDef
	WindRules
	dwd.def
	ecmwf.def
	italy.def
	mars.chk
	oldrd.chk
	obsgroups.def
	rt_by_obsgroup.chk
	reportypes.def
	tigge.def
    marsdb.webapi.cfg
	ms_mars.cfg
	ms_mars.cfg.INFO
	macro_deprecated_functions.txt
	macro_templates.txt
	uPlotTable
	licence_for_about_box.txt
	licence_for_macros.txt
)



# Set the list of things that really need to be built before we can start running
# Metview for the purpose of generating Macro dictionaries, etc.

set(metview_core_targets
    macro
)


set(extra_install_files macro_built_in_functions)

ADD_CUSTOM_COMMAND(
    OUTPUT  ${CMAKE_BINARY_DIR}/share/metview/etc/macro_built_in_functions.txt
    COMMAND ${FULL_STARTUP_SCRIPT_PATH} -b ${CMAKE_CURRENT_SOURCE_DIR}/../../../scripts/generate_list_of_macro_functions.mv ${CMAKE_BINARY_DIR}/share/metview/etc/macro_built_in_functions.txt
    DEPENDS ${FULL_STARTUP_SCRIPT_PATH} ${metview_core_targets}
)

#ADD_CUSTOM_TARGET(macro_built_in_functions ALL DEPENDS ${CMAKE_BINARY_DIR}/share/metview/etc/macro_built_in_functions.txt)

set (dict_file "${CMAKE_BINARY_DIR}/share/metview/etc/dict.txt")

ADD_CUSTOM_COMMAND(
    OUTPUT  ${dict_file}
    COMMAND ${FULL_STARTUP_SCRIPT_PATH} -b ${CMAKE_CURRENT_SOURCE_DIR}/../../../scripts/generate_dictionary.mv ${dict_file}
    DEPENDS ${FULL_STARTUP_SCRIPT_PATH} ${metview_core_targets}
)

#ADD_CUSTOM_TARGET(macro_built_in_functions ALL DEPENDS ${CMAKE_BINARY_DIR}/share/metview/etc/macro_built_in_functions.txt)


#$(dict): ../$(STARTUP_SCRIPT)   generate_dictionary.mv  ../bin/macro ../bin/macro regenerate_object_list ../share/metview/etc/ObjectListAll
#	../$(STARTUP_SCRIPT) -b generate_dictionary.mv $(dict)


ADD_CUSTOM_COMMAND(
    OUTPUT  ${CMAKE_BINARY_DIR}/share/metview/etc/macro_icon_function_help.xml
    COMMAND ${FULL_STARTUP_SCRIPT_PATH} -b ${CMAKE_CURRENT_SOURCE_DIR}/../../../scripts/generate_icon_fun_help.mv ${dict_file} ${CMAKE_BINARY_DIR}/share/metview/etc/macro_icon_function_help.xml
    DEPENDS ${FULL_STARTUP_SCRIPT_PATH} ${dict_file} ${metview_core_targets}
)

ADD_CUSTOM_TARGET(macro_built_in_functions ALL DEPENDS ${CMAKE_BINARY_DIR}/share/metview/etc/macro_built_in_functions.txt
                                                       ${CMAKE_BINARY_DIR}/share/metview/etc/dict.txt
                                                       ${CMAKE_BINARY_DIR}/share/metview/etc/macro_icon_function_help.xml
                                                       ${CMAKE_BINARY_DIR}/share/metview/etc/)

#$(icon_function_help): $(dict) ../$(STARTUP_SCRIPT)   generate_icon_fun_help.mv ../bin/macro regenerate_object_list ../share/metview/etc/ObjectListAll
#	../$(STARTUP_SCRIPT) -b generate_icon_fun_help.mv $(icon_function_help)


list(APPEND generated_files   macro_built_in_functions.txt dict.txt macro_icon_function_help.xml)



#if (METVIEW_MARS_ODB)
#    list(APPEND files MarsOdbDef MarsOdbRules)
#endif()


# a macro to generate derived Mars definition and rules files
macro(generate_def_file
      generated_file source_file verb_extension)

    add_custom_command(
        OUTPUT ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/${generated_file}
	    COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/${source_file} | sed 's/RETRIEVE[^D]/RETRIEVE${verb_extension} /g' | sed 's/READ /READ${verb_extension} /g' > ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/${generated_file}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${source_file})
    add_custom_target(${generated_file} DEPENDS ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/${generated_file})

    list(APPEND generated_files ${generated_file})
    list(APPEND dependencies  ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/${generated_file})

endmacro(generate_def_file)


generate_def_file(MarsG2Def      tigge.def _G2)
generate_def_file(MarsG2Rules    mars.chk  _G2)
generate_def_file(MarsTiggeDef   tigge.def _TIGGE)
generate_def_file(MarsTiggeRules mars.chk  _TIGGE)

if (METVIEW_MARS_ODB)
    generate_def_file(MarsOdbDef   ecmwf.def _ODB)
    generate_def_file(MarsOdbRules mars.chk  _ODB)
endif()


#if (METVIEW_ECREGRID)
#    generate_def_file(MarsEcRegridDef  ecmwf.def _ECREGRID)
#    generate_def_file(MarsEcRegridRules mars.chk _ECREGRID)
#endif()



# Set up ConfigDef to have the network proxy settings given to CMake
configure_file(ConfigDef.in ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/ConfigDef @ONLY)

list(APPEND generated_files ConfigDef)



# at configure time we can just copy most of the files across to the build directory
foreach( f ${files} )
    # copy to the build directory
    configure_file(${f} ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/ COPYONLY)
endforeach()



# add all these to the list of files to be installed
foreach( f ${files} ${generated_files})
    # ensure file is installed at install time
    install( FILES ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/${f}  
             DESTINATION share/metview/etc
             PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
endforeach()



# concatenate all object spec files into one
# remove all comments
# add "cat <<@ " as the first line, and "@" as the last
#message(STATUS "ModuleObjectSpecFiles: ${ModuleObjectSpecFiles}")

set(ObjectListAllComponents
       ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/Services
       ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/ObjectList
       ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/uPlotTable
       ${ModuleObjectSpecFiles})


add_custom_command(
    OUTPUT ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/ObjectListAll
	COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/${MARS_ACCESS} > ObjectListAll.temp \; echo 'cat <<\@'   >> ObjectListAll.temp \; cat ${ObjectListAllComponents} >> ObjectListAll.temp \; echo '\@'          >> ObjectListAll.temp \; grep -v "^#" ObjectListAll.temp > ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/ObjectListAll \; rm -f ObjectListAll.temp
#	COMMAND cat $(MARS_ACCESS) > ObjectListAll.temp \; echo "cat <<@ "   >> ObjectListAll.temp \; cat Services ObjectList uPlotTable >> ObjectListAll.temp \; cat `ls  ObjectSpec.* | grep -v \~`  >> ObjectListAll.temp \; echo "@"          >> ObjectListAll.temp \; grep -v '^#' ObjectListAll.temp > ObjectListAll
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ObjectList ${ObjectListAllComponents})
add_custom_target(ObjectListAll DEPENDS ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/ObjectListAll)


install( FILES ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/ObjectListAll
         DESTINATION share/metview/etc
         PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)


# add a target which will always be run. This will trigger the creation of the generated files if necessary
add_custom_command( 
    OUTPUT  defs_created.txt
    COMMAND touch defs_created.txt
    DEPENDS ${dependencies} ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/ObjectListAll
    )

add_custom_target( all_defs ALL DEPENDS defs_created.txt )

