
set(scripts
    dump metview-mail mvrun mvrun_c mvrun_lnx
    logrun metview_stat metview_create_user_dir
    metview_patches
    mvusage metview_help set_mars_env
    document_macros.pl mv_grib_dump
    metview_local.${MV_ENVIRON} metview_local2.${MV_ENVIRON}
    MarsTiggeWrapper
    MarsOdbWrapper
    generate_video
    mv_compress mv_mail mv_uniquename
    mv_odbsql mv_obstat
    mv_mars_catalog.py
    mv_flextra_prep.mv mv_flextra_prep mv_flextra_run
    mv_flexpart_run
    mv_rttov_run
    mv_scm_run mv_scm_vis.mv
    mv_vapor_prep.mv mv_vapor_gui
    select_mars_config
)

if(AT_ECMWF)
    list(APPEND scripts mv_debug_env.sh)
endif()



# handle these script files
metview_script_files(SCRIPT_FILES ${scripts})




##############################################################
#
# generate the 'compile' script for inline C/Fortran in Macro

# only use the first library returned in EMOS_LIBRARIES
# - this is because it can also return a list of shared libraries used by
# the PGI compiler which can then not be found at run-time.
list(GET EMOS_LIBRARIES 0 EMOS_FIRST_LIB)


# for GRIB_API, we *might* get something like
# grib_api;grib_api_f77;grib_api_f90;/usr/lib64/libjasper.so;/usr/lib64/libjpeg.so;/usr/lib64/libm.so
# which is not all that we need to link. So we convert 'grib_api' into '-lgrib_api'
# and for the Fortran libraries.

set(GRIB_API_LIBRARIES_STR " ${GRIB_API_LIBRARIES}") # add a space to the beginning

string (REPLACE ";"         " "           GRIB_API_LIBRARIES_STR    "${GRIB_API_LIBRARIES_STR}")
string (REPLACE " grib_api" " -lgrib_api" GRIB_API_LIBRARIES_STR    "${GRIB_API_LIBRARIES_STR}")
string (REPLACE ";"         " "           EMOS_LIBRARIES_STR        "${EMOS_FIRST_LIB}")


set(GRIB_API_LIBRARIES_STR "${GRIB_API_LIBRARIES_STR} ${PTHREAD_LIB}") # add pthread in case needed

# if we are linking with shared GRIB_API libraries, then in order to be able
# to run compiled inline Fortran/C/Macro programs, we will need to set
# LD_LIBRARY_PATH to where GRIB_API was installed (this should only be
# needed if GRIB_API was installed in a non-default location).
# GRIB_API_LIB_DIR will be added to METVIEW_LD_PATHS and used in compile.in.

# first, if CMAKE_PREFIX_PATH is defined and GRIB_API_PATH is not, then
# GRIB_API might be installed in CMAKE_PREFIX_PATH
if(CMAKE_PREFIX_PATH)
    if(NOT GRIB_API_PATH)
        set(GRIB_API_PATH ${CMAKE_PREFIX_PATH})
    endif()
endif()

if(GRIB_API_PATH)
    if(EXISTS "${GRIB_API_PATH}/lib")
        set(GRIB_API_LIB_DIR "${GRIB_API_PATH}/lib")
    else()
    endif()

    if(EXISTS "${GRIB_API_PATH}/lib64")
        set(GRIB_API_LIB_DIR "${GRIB_API_PATH}/lib64")
    endif()

    set(GRIB_API_LIBRARIES_STR "-L${GRIB_API_PATH}/lib -L${GRIB_API_PATH}/lib64 ${GRIB_API_LIBRARIES_STR}")
endif()

foreach (inc ${GRIB_API_INCLUDE_DIRS})
    set(GRIB_API_INCLUDE_DIRS_STR "${GRIB_API_INCLUDE_DIRS_STR} -I${inc}")
endforeach()


configure_file(compile.in ${CMAKE_BINARY_DIR}/bin/compile @ONLY)
install( FILES ${CMAKE_BINARY_DIR}/bin/compile  
         DESTINATION ${MV_BIN_DIR}
         PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE)




##################
#
# CONFIG.site

if( DEFINED NETCDF_PATH )
    set(NETCDF_BIN "${NETCDF_PATH}/bin")
else()
    set(NETCDF_BIN "")
endif()


ecbuild_get_date(TODAYS_DATE)


# Add certain paths to LD_LIBRARY_PATH.
# If the given path is not a standard system one, then add it to our list
# of paths to add to LD_LIBRARY_PATH on Metview's startup. We want to
# avoid adding system paths here because they can override non-default
# paths (case in point: the wrong OpenGL library has been known to be
# picked up - used by VAPOR).

set(SYS_LIBDIRS "/usr/lib64" "/usr/lib64/" "/usr/lib" "/usr/lib/" "")
set(METVIEW_LD_PATHS "")

foreach(LIBPATH ${MAGICS_LIB_DIR} ${ODB_API_LIB_DIR} ${GRIB_API_LIB_DIR})
    list(FIND SYS_LIBDIRS ${LIBPATH} IND)
    #message(STATUS "IS ${LIBPATH} ${IND}")
    if(IND EQUAL -1)  # path not in the list of system paths
        set(METVIEW_LD_PATHS "${LIBPATH}:${METVIEW_LD_PATHS}")
    endif()
endforeach()


configure_file(CONFIG.site.in  CONFIG.site @ONLY)


# concatenate CONFIG.site and metview_base into the Metview startup script

add_custom_command( 
    OUTPUT  ${FULL_STARTUP_SCRIPT_PATH}
    COMMAND cat  ${CMAKE_CURRENT_BINARY_DIR}/CONFIG.site > ${FULL_STARTUP_SCRIPT_PATH} \; cat ${CMAKE_CURRENT_SOURCE_DIR}/metview_base >> ${FULL_STARTUP_SCRIPT_PATH} \; chmod uga+x ${FULL_STARTUP_SCRIPT_PATH}
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/CONFIG.site ${CMAKE_CURRENT_SOURCE_DIR}/metview_base
)


# create a 'production' version of this - just the same, but with
# METVIEW_VERSION=prod instead of dev

set(PROD_TEMPORARY_STARTUP_SCRIPT "${FULL_STARTUP_SCRIPT_PATH}_prod")

add_custom_command( 
    OUTPUT  ${PROD_TEMPORARY_STARTUP_SCRIPT}
    COMMAND sed "s/METVIEW_VERSION=dev/METVIEW_VERSION=prod/g" ${FULL_STARTUP_SCRIPT_PATH} > ${PROD_TEMPORARY_STARTUP_SCRIPT}
    DEPENDS ${FULL_STARTUP_SCRIPT_PATH}
)


ADD_CUSTOM_TARGET(mvs ALL DEPENDS ${FULL_STARTUP_SCRIPT_PATH} ${PROD_TEMPORARY_STARTUP_SCRIPT})


# when we install, we actually want to install the production script and rename it
install( FILES       ${PROD_TEMPORARY_STARTUP_SCRIPT}
         RENAME      ${METVIEW_SCRIPT}
         DESTINATION bin # NOT ${MV_BIN_DIR}!
         PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE)



## TODO
## If we are building this outside ECMWF, then we want different proxy settings
## (used currently by the OGC clients). We achieve this by simply changing the
## default setting in the ConfigDef file. We can do this with a  a search/replace
## operation. This relies very much on the proxy setting having a uniqe 'signature'
## so that the search/replace can operate on a single line.
#
#if test "x$SITE_NAME" != 'xecmwf'; then
#    configdef=share/metview/etc/ConfigDef
#    configtmp=${configdef}.tmp
#    echo "Not at ECMWF: resetting the default proxy setting in ${configdef}."
#    chmod u+w $configdef
#    sed "s/	} = yes/	} = no/g"  $configdef > $configtmp
#    mv $configtmp $configdef
#fi

#ecbuild_dont_pack(FILES mv_cp metview_local.ecmwf metview_local2.ecmwf)

ecbuild_dont_pack(REGEX "metview_local*.ecm*")
ecbuild_dont_pack(FILES "mv_cp;mv_debug_env.sh;update_MetApps_files.sh")

