
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 metgram.py)
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}")
if( HAVE_ECCODES )
  string (REPLACE " eccodes"  " -leccodes"  GRIB_API_LIBRARIES_STR  "${GRIB_API_LIBRARIES_STR}")
else()
  string (REPLACE " grib_api" " -lgrib_api" GRIB_API_LIBRARIES_STR  "${GRIB_API_LIBRARIES_STR}")
endif()
string (REPLACE ";"         " "           EMOS_LIBRARIES_STR        "${EMOS_LIB_PATH}")


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


# we may need to add the netCDF lib dir to the rpath. We don't seem to have a variable
# which tells us this directly, so we need to make an educated guess...
if(DEFINED NETCDF_INCLUDE_DIR)
    set(NETCDF_LIB_RPATH "-Wl,-rpath,${NETCDF_INCLUDE_DIR}/../lib64:${NETCDF_INCLUDE_DIR}/../lib")
    set(GRIB_API_LIBRARIES_STR "${GRIB_API_LIBRARIES_STR} ${NETCDF_LIB_RPATH}")
endif()

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
set( grib_handling_pkg grib_api )
if( HAVE_ECCODES )
  set( grib_handling_pkg eccodes )
endif()
if(NOT GRIB_API_PATH)
    get_property(GRIB_LIB_PATH TARGET ${grib_handling_pkg} 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()
    ecbuild_info("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 OR METVIEW_BUNDLE_TYPE STREQUAL "MMG")
    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}")
    set(GRIB_API_PATH ${CMAKE_INSTALL_PREFIX})
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()



# MAGPLUS_HOME - under normal circumstances, we link with a specific
# Magics library which knows where it's been installed, so we don't
# need (or want) to set MAGPLUS_HOME, and in fact we want to avoid
# the situation where a user sets it to the wrong path (as can happen
# if a Magics module is set up by default in a user's environment).
# So the simplest thing is to unset MAGPLUS_HOME and let the library
# find its own resource files. This works fine if Magics has been
# installed, but not if we are linking with a Magics in the build tree.
# In this case MAGPLUS_HOME will need to point to the Magics source(!)
# directory. In Magics 2.27.0 onwards, this is all taken care of by the
# CMake variable MAFPLUS_HOME. If this is not set (i.e. earlier version
# of Magics) then we just unset it. MV_MAGPLUS_HOME_PATH_SET is used
# in metview_base.in.

if(DEFINED MAGPLUS_HOME)
    set(MV_MAGPLUS_HOME_PATH_SET "MAGPLUS_HOME=${MAGPLUS_HOME}")
else()
    set(MV_MAGPLUS_HOME_PATH_SET "unset MAGPLUS_HOME")
endif()



# 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
# NOTE: if building as part of a bundle, then we have the option (EXPOSE_SUBPACKAGES)
# of installing everything apart from the startup script into a subdirectory so that
# libs and executables from other packages don't clash with anything else on
# the system. If we elect to do this (NOT EXPOSE_SUBPACKAGES) then we will have already
# modified the CMAKE_INSTALL_PREFIX (in the bundle's top-level CMakeLists.txt file) to
# put everything two levels deeper. But... we want the startup script to go
# into the original CMAKE_INSTALL_PREFIX/bin directory

if((DEFINED METVIEW_BUNDLE OR DEFINED METVIEW_BUNDLE_TYPE) AND (NOT ENABLE_EXPOSE_SUBPACKAGES))
    set (STARTUP_SCRIPT_INSTALL_DIR "../../bin")
else()
    set (STARTUP_SCRIPT_INSTALL_DIR bin)
endif()
ecbuild_info("Startup script will be installed to: ${STARTUP_SCRIPT_INSTALL_DIR}")


install( FILES       ${PROD_TEMPORARY_STARTUP_SCRIPT}
         RENAME      ${METVIEW_SCRIPT}
         DESTINATION ${STARTUP_SCRIPT_INSTALL_DIR} # 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")

