#
# INSTALL_TEST_CMAKE, cmake test script for install non-reg suite
# Copyright (C) 2017-2021 Xavier Delaruelle
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

##########################################################################

cmake_policy(SET CMP0007 NEW)

if(NOT EXISTS ${initfile})
   message(FATAL_ERROR "Cannot read ${initfile}")
endif()

# source module init file
include(${initfile})

# execute command list
set(code 0)
set(func "module")
string(REPLACE ":" ";" cmdsplit "${cmdlist}")
# empty command list, means one empty command
list (LENGTH cmdlist cmdcnt)
if(${cmdcnt} EQUAL 0)
   module("")
else()
   foreach(cmd ${cmdsplit})
      # call ml procedure instead of module
      if("${cmd}" STREQUAL "ml")
         set(func "ml")
      else()
         set(quotedArgs "")
         # if command equals to NOARG string, means call with no arg passed
         if(NOT "${cmd}" STREQUAL "NOARG")
            string(REPLACE "," ";" cmdelt "${cmd}")
            list (LENGTH cmdelt eltcnt)
            # empty arg list, means one empty arg
            if (${eltcnt} EQUAL 0)
               string(APPEND quotedArgs "\"\"")
            else()
               foreach(elt IN LISTS cmdelt)
                  if(NOT "${quotedArgs}" STREQUAL "")
                     string(APPEND quotedArgs " ")
                  endif()
                  string(APPEND quotedArgs "[===[${elt}]===]")
               endforeach()
            endif()
         endif()
         cmake_language(EVAL CODE "${func}(${quotedArgs})")
      endif()
      if(NOT module_result AND NOT ${module_result} STREQUAL "")
         set(code 1)
      elseif(NOT ${module_result} STREQUAL "TRUE")
         message(STATUS ${module_result})
      endif()
   endforeach()
endif()

if(${code})
   message(FATAL_ERROR "")
endif()
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent:
