
set(icons
    ALIGN_BOTTOM.xpm
    ALIGN_LEFT.xpm
    ALIGN_RIGHT.xpm
    ALIGN_TOP.xpm
    ANNOTATIONVIEW.xpm
    BINARY.xpm
    BINNING.xpm
    BUFR.xpm
    CARTESIANVIEW.xpm
    CLEANFILE.xpm
    COMPUTE.xpm
    DATACOVERAGE.xpm
    DISTR_HCENTER.xpm
    DISTR_VCENTER.xpm
    DRAWING_PRIORITY.xpm
    ECFS.xpm
    FLEXTRA_PREPARE.xpm FLEXTRA_RUN.xpm FLEXTRA_FILE.xpm FLEXTRA_VISUALISER.xpm
    FOLDER.xpm
    GDDRIVER.xpm
    GEOPOINTS.xpm
    GEOTIFF.xpm
    GEOVIEW.xpm
    GEO_TO_GRIB.xpm
    GIF.xpm
    GRIB.xpm
    GRIB_TO_GEO.xpm
    help_area.xpm
    help_line.xpm
    help_map.xpm
    help_point.xpm
    help_station.xpm
    INPUTVISUALISER.xpm
    JPEG.xpm
    LLMATRIX.xpm
    lock_icon.xpm
    MACRO.xpm
    MACROPARAM.xpm
    MAPVIEW.xpm
    MBOXPLOT.xpm
    MCOAST.xpm
    MCONT.xpm
    MGRAPH.xpm
    MLEGEND.xpm
    MIMPORT.xpm
    MOBS.xpm
    MTAYLOR.xpm
    MSYMB.xpm
    MTEXT.xpm
    MWIND.xpm
    NAMELIST.xpm
    NETCDF.xpm NETCDF_RTTOV_INPUT.xpm
    NETCDFPLUS.xpm
    NEWS.xpm
    NOTE.xpm
    OBSFILTER.xpm
    ODB_DB.xpm ODB_FILTER.xpm ODB_VISUALISER.xpm
    BUFRPICKER.xpm
    OVERLAY_CONTROL.xpm
    PAXIS.xpm
    MAXIS.xpm
    PCOAST.xpm
    PCONT.xpm
    PDF.xpm
    PGRAPH.xpm
    PLOTPAGE.xpm
    PLOTSUBPAGE.xpm
    PNG.xpm
    POTTF.xpm
    DIVROT.xpm
    PSOUTPUT.xpm
    PSFILE.xpm
    PSYMB.xpm
    PTEXT.xpm
    PWIND.xpm
    READ.xpm
    READ_TIGGE.xpm
    RELHUM.xpm
    RETRIEVE.xpm
    RETRIEVE_TIGGE.xpm
    RTTOV_OUTPUT_FILE.xpm RTTOV_RUN.xpm RTTOV_VISUALISER.xpm
    SCM_INPUT_DATA.xpm  SCM_OUTPUT_DATA.xpm SCM_RUN.xpm SCM_VIS.xpm
    SERVICE.xpm
    SHELL.xpm
    SIMPLE_FORMULA.xpm
    SQL.xpm
    STATIONS.xpm
    SUBPAGEDETAILS.xpm
    SUPERPAGE.xpm
    SVG.xpm
    SVGOUTPUT.xpm
    SYSTEM.xpm
    TABLE.xpm
    TABLEREADER.xpm
    TABLEVISUALISER.xpm
    TAR.xpm
    TIFF.xpm
    VAPOR_PREPARE.xpm
    VELSTR.xpm
    WASTEBASKET.xpm
    XPIF.xpm
)


#  ---------- start of module-specific icons -------------

if (ENABLE_PLOTTING)
    list(APPEND icons KML.xpm)
endif()

#  ---------- end of module-specific icons -------------


set (outdir ${CMAKE_BINARY_DIR}/share/metview/icons)


##############################################################
#
# Rules to generate the icons
#
###.svg.xpm:
###	p4 edit $*.xpm
###	./svg2xpm $*.svg

foreach( file ${icons} )
    list(APPEND full_icons "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
endforeach()


foreach(file ${full_icons} ${ModuleXpmFiles})

    #message(STATUS "converting ${file}")

    # filename manipulation to get source and destination filenames
    get_filename_component( path     ${file} PATH   )
    get_filename_component( basefile ${file} NAME_WE )
    set(full_icon  ${outdir}/${basefile}.icon)
    set(half_icon  ${outdir}/${basefile}.half)
    set(fixed_icon ${outdir}/${basefile}.fixed)

    # add custom commands to a) copy the source icons into .icon and b) convert the source icons into .half
    add_custom_command( 
        OUTPUT  ${full_icon}
        COMMAND ${CMAKE_COMMAND} -E copy ${file} ${full_icon}
        DEPENDS ${file}
    )
    add_custom_command( 
        OUTPUT  ${half_icon}
        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/fix_last_line ${file} ${fixed_icon} \; convert -geometry 54% -map ${fixed_icon} ${file} ${half_icon} \; ${CMAKE_COMMAND} -E remove -f ${fixed_icon}
        DEPENDS ${file}
    )

    # add custom targets to set the dependencies between source and destination
    add_custom_target( ${basefile}_full_target DEPENDS ${full_icon} )
    add_custom_target( ${basefile}_half_target DEPENDS ${half_icon} )


    # tell CMake that these files are generated
    set_source_files_properties(${full_icon} GENERATED)
    set_source_files_properties(${half_icon} GENERATED)

    # generate lists of these generated files
    list( APPEND full_icons ${full_icon}) 
    list( APPEND half_icons ${half_icon}) 


    # ensure files are installed at install time
    install( FILES       ${full_icon}
             DESTINATION share/metview/icons
             PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
    ADD_DEPENDENCIES(${basefile}_full_target ${file})

    install( FILES       ${half_icon}
             DESTINATION share/metview/icons
             PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)

endforeach()


# add a target which will always be run. This will trigger the creation of the icons if necessary
add_custom_command( 
    OUTPUT  icons_created.txt
    COMMAND touch icons_created.txt
    DEPENDS ${full_icons} ${half_icons}
    )

add_custom_target( all_icons ALL DEPENDS icons_created.txt )

set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES fixed.xpm)  # ensure cleanup


#######EXTRA_DIST = $(icons) fix_last_line svg2xpm
