###########################################################################
### CMake Build System for the Parametric Hurricane Model (PaHM)
### PlatformFuncs.cmake :: Utility functions
###
### Author: Panagiotis Velissariou <panagiotis.velissariou@noaa.gov>
###########################################################################

function(get_env_hdf5)
  if(NOT _DEFINED_HDF5)
    set(_DEFINED_HDF5 FALSE)
    set(_DEFINED_HDF5 FALSE PARENT_SCOPE)

    if(DEFINED ARGV0)
      string(STRIP "${ARGV0}" _hdf5_env_name)
      if(NOT "$ENV{${_hdf5_env_name}}" STREQUAL "")
        set(HDF5      "$ENV{${_hdf5_env_name}}" PARENT_SCOPE)
        set(HDF5HOME  "$ENV{${_hdf5_env_name}}" PARENT_SCOPE)
        set(HDF5_DIR  "$ENV{${_hdf5_env_name}}" PARENT_SCOPE)
        set(HDF5_PATH "$ENV{${_hdf5_env_name}}" PARENT_SCOPE)
        set(HDF5_ROOT "$ENV{${_hdf5_env_name}}" PARENT_SCOPE)
        set(_DEFINED_HDF5 TRUE)
        set(_DEFINED_HDF5 TRUE PARENT_SCOPE)
      endif()
    endif()

    if(NOT _DEFINED_HDF5)
      if(NOT "$ENV{HDF5}" STREQUAL "")
        set(HDF5      "$ENV{HDF5}" PARENT_SCOPE)
        set(HDF5HOME  "$ENV{HDF5}" PARENT_SCOPE)
        set(HDF5_DIR  "$ENV{HDF5}" PARENT_SCOPE)
        set(HDF5_PATH "$ENV{HDF5}" PARENT_SCOPE)
        set(HDF5_ROOT "$ENV{HDF5}" PARENT_SCOPE)
        set(_DEFINED_HDF5 TRUE)
        set(_DEFINED_HDF5 TRUE PARENT_SCOPE)
      else()
        foreach(_var IN ITEMS DIR HOME PATH ROOT)
          if(NOT "$ENV{HDF5${_var}}" STREQUAL "")
            set(HDF5      "$ENV{HDF5${_var}}" PARENT_SCOPE)
            set(HDF5HOME  "$ENV{HDF5${_var}}" PARENT_SCOPE)
            set(HDF5_DIR  "$ENV{HDF5${_var}}" PARENT_SCOPE)
            set(HDF5_PATH "$ENV{HDF5${_var}}" PARENT_SCOPE)
            set(HDF5_ROOT "$ENV{HDF5${_var}}" PARENT_SCOPE)
            set(_DEFINED_HDF5 TRUE)
            set(_DEFINED_HDF5 TRUE PARENT_SCOPE)
            break()
          elseif(NOT "$ENV{HDF5_${_var}}" STREQUAL "")
            set(HDF5      "$ENV{HDF5_${_var}}" PARENT_SCOPE)
            set(HDF5HOME  "$ENV{HDF5_${_var}}" PARENT_SCOPE)
            set(HDF5_DIR  "$ENV{HDF5_${_var}}" PARENT_SCOPE)
            set(HDF5_PATH "$ENV{HDF5_${_var}}" PARENT_SCOPE)
            set(HDF5_ROOT "$ENV{HDF5_${_var}}" PARENT_SCOPE)
            set(_DEFINED_HDF5 TRUE)
            set(_DEFINED_HDF5 TRUE PARENT_SCOPE)
            break()
          endif()
        endforeach()
      endif()
      unset(_name)
      unset(_var)
    endif()

  endif()
endfunction()


function(get_env_netcdf)
  if(NOT _DEFINED_NETCDF)
    set(_DEFINED_NETCDF FALSE)
    set(_DEFINED_NETCDF FALSE PARENT_SCOPE)

    if(DEFINED ARGV0)
      string(STRIP "${ARGV0}" _ncdf_env_name)
      if(NOT "$ENV{${_ncdf_env_name}}" STREQUAL "")
        set(NETCDF      "$ENV{${_ncdf_env_name}}" PARENT_SCOPE)
        set(NETCDFHOME  "$ENV{${_ncdf_env_name}}" PARENT_SCOPE)
        set(NETCDF_DIR  "$ENV{${_ncdf_env_name}}" PARENT_SCOPE)
        set(NETCDF_PATH "$ENV{${_ncdf_env_name}}" PARENT_SCOPE)
        set(NETCDF_ROOT "$ENV{${_ncdf_env_name}}" PARENT_SCOPE)
        set(_DEFINED_NETCDF TRUE)
        set(_DEFINED_NETCDF TRUE PARENT_SCOPE)
      endif()
      set(_DEFINED_NETCDF TRUE)
      set(_DEFINED_NETCDF TRUE PARENT_SCOPE)
    endif()

    if(NOT _DEFINED_NETCDF)
      foreach(_name IN ITEMS NETCDF NETCDF4 NetCDF NetCDF4)
        if(_DEFINED_NETCDF)
          break()
        endif()

        if(NOT "$ENV{${_name}}" STREQUAL "")
          set(NETCDF      "$ENV{${_name}}" PARENT_SCOPE)
          set(NETCDFHOME  "$ENV{${_name}}" PARENT_SCOPE)
          set(NETCDF_DIR  "$ENV{${_name}}" PARENT_SCOPE)
          set(NETCDF_PATH "$ENV{${_name}}" PARENT_SCOPE)
          set(NETCDF_ROOT "$ENV{${_name}}" PARENT_SCOPE)
          set(_DEFINED_NETCDF TRUE)
          set(_DEFINED_NETCDF TRUE PARENT_SCOPE)
        else()
          foreach(_var IN ITEMS DIR HOME PATH ROOT)
            if(NOT "$ENV{${_name}${_var}}" STREQUAL "")
              set(NETCDF      "$ENV{${_name}${_var}}" PARENT_SCOPE)
              set(NETCDFHOME  "$ENV{${_name}${_var}}" PARENT_SCOPE)
              set(NETCDF_DIR  "$ENV{${_name}${_var}}" PARENT_SCOPE)
              set(NETCDF_PATH "$ENV{${_name}${_var}}" PARENT_SCOPE)
              set(NETCDF_ROOT "$ENV{${_name}${_var}}" PARENT_SCOPE)
              set(_DEFINED_NETCDF TRUE)
              set(_DEFINED_NETCDF TRUE PARENT_SCOPE)
              break()
            elseif(NOT "$ENV{${_name}_${_var}}" STREQUAL "")
              set(NETCDF      "$ENV{${_name}_${_var}}" PARENT_SCOPE)
              set(NETCDFHOME  "$ENV{${_name}_${_var}}" PARENT_SCOPE)
              set(NETCDF_DIR  "$ENV{${_name}_${_var}}" PARENT_SCOPE)
              set(NETCDF_PATH "$ENV{${_name}_${_var}}" PARENT_SCOPE)
              set(NETCDF_ROOT "$ENV{${_name}_${_var}}" PARENT_SCOPE)
              set(_DEFINED_NETCDF TRUE)
              set(_DEFINED_NETCDF TRUE PARENT_SCOPE)
              break()
            endif()
          endforeach()
        endif()
      endforeach()
      unset(_name)
      unset(_var)
    endif()

  endif()
endfunction()
