# ---------------------------
# contour style preview images
#----------------------------

# based on ecbuild_download_resource

function( metview_download_resource _p_WORKING_DIR _p_OUT _p_URL CMD_RESULT)

  if( NOT EXISTS ${_p_WORKING_DIR}/${_p_OUT} )

  ecbuild_info("Trying to download ${_p_URL}")

    find_program( CURL_PROGRAM curl )
    mark_as_advanced(CURL_PROGRAM)
    if( CURL_PROGRAM )

      message(STATUS "Running curl... from ${_p_WORKING_DIR} to ${_p_OUT}")
      execute_process( COMMAND ${CURL_PROGRAM} --silent --show-error --fail
                               --retry 1
                               --connect-timeout 20
                               --output ${_p_OUT} ${_p_URL}
                       WORKING_DIRECTORY ${_p_WORKING_DIR} RESULT_VARIABLE _CMD_RESULT )
    else()

      find_program( WGET_PROGRAM wget )

      if( WGET_PROGRAM )

        execute_process( COMMAND ${WGET_PROGRAM} -nv -O ${_p_OUT}
                                 -t 1
                                 -T 5 ${_p_URL}
                         WORKING_DIRECTORY ${_p_WORKING_DIR} RESULT_VARIABLE _CMD_RESULT )

      else()
        ecbuild_warn("Could not find curl or wget. Error downloading ${_p_URL}")
      endif()

    endif()

    if(_CMD_RESULT)
      ecbuild_warn("Error downloading ${_p_URL}")
    endif()

    set(CMD_RESULT ${_CMD_RESULT} PARENT_SCOPE) # pass the return value back

  else()

    ecbuild_info("Already downloaded ${_p_URL} - not trying again")

  endif()

endfunction()



# The preview images are not stored in git but the reside in a tar file on
# the download server!

set(STYLE_PREVIEWS_TAR_NAME "style_previews.tar.gz")
set(STYLE_PREVIEWS_TAR_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(STYLE_PREVIEWS_BIN_DIR "${CMAKE_BINARY_DIR}/share/metview/eccharts/style_previews")
set(HAVE_STYLES_TARBALL 1)

if(NOT EXISTS ${STYLE_PREVIEWS_TAR_DIR}/${STYLE_PREVIEWS_TAR_NAME})

    # Download end extract the tarfile from the download server. It will try to download at
    # CMake configure time
    metview_download_resource (${STYLE_PREVIEWS_TAR_DIR}  # dest dir
                               ${STYLE_PREVIEWS_TAR_NAME} # dest file
                               http://download.ecmwf.org/test-data/metview/eccharts/${STYLE_PREVIEWS_TAR_NAME} # source
                               CMD_RESULT)

    if(CMD_RESULT)
        ecbuild_warn("Could not download the eccharts style previews. Check your internet connection. These are not "
                     "essential for running Metview, so we will continue. Error code: ${CMD_RESULT}")
        set(HAVE_STYLES_TARBALL 0)
    endif()
    
endif()


if(HAVE_STYLES_TARBALL)
    # untar/unzip if not already done
    if( NOT EXISTS ${STYLE_PREVIEWS_BIN_DIR}/cape_extra1.png )
        ecbuild_info("Untarring eccharts style previews")
        execute_process(COMMAND ${CMAKE_COMMAND} -E tar x ${STYLE_PREVIEWS_TAR_DIR}/${STYLE_PREVIEWS_TAR_NAME}
                        WORKING_DIRECTORY ${STYLE_PREVIEWS_BIN_DIR}
                        RESULT_VARIABLE CMD_RESULT)  # untar/zip
        if(CMD_RESULT)
	        ecbuild_warn("Could not untar/unzip the eccharts style previews. These are not "
                         "essential for running Metview, so we will continue. Error code: ${CMD_RESULT}")
        endif()
    else()
        ecbuild_info("eccharts style previews already unzipped")
    endif()
endif()

