
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


# find the actual path to the emoslib library
get_property(EMOS_LIB_PATH TARGET emos PROPERTY LOCATION)


# 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
list(REMOVE_ITEM GRIB_API_LIBRARIES_STR "debug")     # remove non-compiler items from the string
list(REMOVE_ITEM GRIB_API_LIBRARIES_STR "optimized") # remove non-compiler items from the string

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_LIB_PATH}")


set(GRIB_API_LIBRARIES_STR "${GRIB_API_LIBRARIES_STR} ${PTHREAD_LIB}") # add pthread in case needed
set(GRIB_API_LIBRARIES_STR_ORIGINAL "${GRIB_API_LIBRARIES_STR}")       # keep a backup for later

# 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 GRIB_API_PATH is not defined, then we need to compute the path
if(NOT GRIB_API_PATH)
    get_property(GRIB_LIB_PATH TARGET grib_api PROPERTY LIBRARY_OUTPUT_DIRECTORY)
    if(GRIB_LIB_PATH)
        set(GRIB_API_PATH ${GRIB_LIB_PATH}/..)
    else()
        if (grib_api_BASE_DIR)
            set(GRIB_API_PATH ${grib_api_BASE_DIR})
        else()
            if(CMAKE_PREFIX_PATH)
                set(GRIB_API_PATH ${CMAKE_PREFIX_PATH})
            endif()
        endif()
    endif()
    message(STATUS "Computed GRIB_PATH: ${GRIB_API_PATH}")
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 -Wl,-rpath,${GRIB_API_PATH}/lib:${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()



list(APPEND METVIEW_INLINE_DEFINITIONS ${GRIB_API_DEFINITIONS})



configure_file(compile.in ${CMAKE_BINARY_DIR}/bin/compile @ONLY)



# in the case of a bundle, the GRIB_API and emoslib locations will have been
# set to their build directories, which is correct when running from 
# the build directory, but not once installed, so we have to change these
# to the location where they will be installed, and create another 'compile'
# script which is the one that will actually be installed. In the case where
# we're not building a bundle, these two compile scripts will be identical.

if(DEFINED METVIEW_BUNDLE)
    set(GRIB_API_LIBRARIES_STR    "-L${CMAKE_INSTALL_PREFIX}/lib ${GRIB_API_LIBRARIES_STR_ORIGINAL}")
    set(GRIB_API_INCLUDE_DIRS_STR "-I${CMAKE_INSTALL_PREFIX}/include")
    string(REPLACE "${CMAKE_BINARY_DIR}/lib"  "${CMAKE_INSTALL_PREFIX}/lib"    EMOS_LIBRARIES_STR  "${EMOS_LIBRARIES_STR}")
endif()

configure_file(compile.in ${CMAKE_BINARY_DIR}/bin/compile_install @ONLY)



install( FILES ${CMAKE_BINARY_DIR}/bin/compile_install  
         DESTINATION ${MV_BIN_DIR}
         RENAME      compile
         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()

if(DEFINED METVIEW_BUNDLE)
    set(METVIEW_BUNDLE_SRC ${CMAKE_SOURCE_DIR})
endif()

if(DEFINED METVIEW_BUNDLE_MAGICS)
    set(MV_BUNDLE_MAGICS_SRC ${CMAKE_SOURCE_DIR}/magics)
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).
#
# NO - this is not really needed with the rpath, and it can lead, in the
# case of a bundle, to the build directory (Magics) going into the
# LD_LIBRARY_PATH even when installed (no harmful effect seen though).
# 
# 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)
#     if(IND EQUAL -1)  # path not in the list of system paths
#         set(METVIEW_LD_PATHS "${LIBPATH}:${METVIEW_LD_PATHS}")
#     endif()
# endforeach()


# create the Metview startup script
configure_file(metview_base.in ${FULL_STARTUP_SCRIPT_PATH} @ONLY)


# 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")

