
set(files
	AnimatedGIFOutputDef
	AnnotationViewDef AnnotationViewRules
	AuxPrepBufrTable_B.txt AuxPrepBufrTable_D.txt
	AxisDef AxisRules
	BinningObjectDef BinningObjectRules
	CartesianViewDef CartesianViewRules
	CleanFileDef
	Coastlines
	CoastDef
	CoastRules
	ConfigRules
	ContDef
	ContRules
	DeviceDriverDef
	DeviceDriverRules
	DivrotDef
	DivrotRules
	DrawingPriorityDef
	DrawingPriorityRules
	EcfsDef
	EPSCAIROOutputDef
	EPSOutputDef
	FunctionList.txt
	GenAppDef
	GenAppRules
	GeoToGribDef
	GeoToGribRules
	GeoViewDef GeoViewRules
	GLOBEOutputDef
	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
	MagicsColours.txt
	MapViewDef
	MapViewRules
	MarsDef
	MarsRules
	ObjectList
	ObjectList.forTesting
	ObsFilterDef
	ObsFilterRules
	ObsPlottingDef
	ObsPlottingRules
	OverlayControl
	OverlayControlDef
	OverlayControlRules
	PageFrameDef
	PageFrameRules
	PDFOutputDef
	PDFQTOutputDef
	PlotPageDef
	PlotPageRules
	PlotSubPageDef
	PlotSubPageRules
	PlotSuperPageDef
	PlotSuperPageRules
	Plot_Window_for_Image
	PNGOutputDef
	PNGQTOutputDef
	PredefinedAreaList
	PSCAIROOutputDef
	PSOutputDef
	RelHumDef
	RelHumRules
	Services
	SimpleFormulaDef
	SimpleFormulaRules
	StationDef
	StationRules
	SubpageFrameDef
	SubpageFrameRules
	SVGOutputDef
        SVGQTOutputDef
	SymbDef SymbRules
	TableCommonDef TableReaderDef TableReaderRules
	TextDef TextRules
	UiServiceTable
	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
        WsCollectionDef
        WsColdFrontDef
        WsCommonDef
        WsCommonXyCurveDef
        WsCommonXyPointDef
        WsConvergenceLineDef
        WsConvergenceLineRules
        WsFrontDef
        WsFrontRules
        WsHighDef
        WsHighRules
        WsImageDef
        WsImageRules
        WsInstabilityLineDef
        WsInstabilityLineRules
        WsItczDef
        WsItczRules
        WsIttDiscontinuityDef
        WsIttDiscontinuityRules
        WsLowDef
        WsLowRules
        WsOccludedDef
        WsPlacemarkDef
        WsPlacemarkRules
        WsQuasiStationaryDef
        WsRidgeDef
        WsRidgeRules
        WsMarkerDef
        WsMarkerRules
        WsShapeDef
        WsShapeRules
        WsShearLineDef
        WsShearLineRules
        WsStormDef
        WsStormRules
        WsTextDef
        WsTextRules
        WsTropicalWaveDef
        WsTropicalWaveRules
        WsTroughDef
        WsTroughRules
        WsWarmFrontDef
        WsWmoSymbolDef
        WsWmoSymbolRules
        WsItems
	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(tables
    grib_def
)

# 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
    event
    macro
    ObjectListAllTarget
)


#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 appl_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' | sed 's/marsG2 /mars${appl_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)

if(HAVE_PPROC_MIR)
    generate_def_file(MarsMirDef     ecmwf.def _MIR MIR)
    generate_def_file(MarsMirRules   mars.chk  _MIR MIR)
endif()

if(HAVE_PPROC_EMOS)
    generate_def_file(MarsEmosDef     ecmwf.def _EMOS EMOS)
    generate_def_file(MarsEmosRules   mars.chk  _EMOS EMOS)
endif()

#if(ENABLE_MARS_CMD_LINE)
#    generate_def_file(MarsCmdLineDef     ecmwf.def _CMDLINE CmdLine)
#    generate_def_file(MarsCmdLineRules   mars.chk  _CMDLINE CmdLine)
#endif()

#generate_def_file(MarsClientDef   ecmwf.def _CLIENT CLIENT)
#generate_def_file(MarsClientRules mars.chk  _CLIENT CLIENT)

generate_def_file(MarsTiggeDef   tigge.def _TIGGE TIGGE)
generate_def_file(MarsTiggeRules mars.chk  _TIGGE TIGGE)

if (METVIEW_MARS_ODB)
    generate_def_file(MarsOdbDef   ecmwf.def _ODB ODB)
    generate_def_file(MarsOdbRules mars.chk  _ODB 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(ObjectListAllTarget 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} ObjectListAllTarget
    )

add_custom_target( all_defs ALL DEPENDS defs_created.txt )

#Copy the tables to the build directory
foreach( d ${tables})
    add_custom_target(copy_all_tables ALL
    COMMAND cmake -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/${d} ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/${d}
    )
endforeach()

#Install the tables
foreach( d ${tables})
    install( DIRECTORY ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/etc/${d}/
         DESTINATION share/metview/etc/${d}
         FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
         DIRECTORY_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
    )
endforeach()
