#!../../common/bats/bin/bats
# -*-sh-*-

load ../../common/test_helper_functions || exit 1
source ../../common/functions || exit 1

family=$LMOD_FAMILY_COMPILER

@test "[Compilers] compiler module loaded ($family)" {
    module list $family >& .cmd_output || exit 1
    local my_module=`cat .cmd_output | sed '/^\s*$/d' | sed 's/^[ \t]*//' | tail -1`
    echo $my_module | grep -q "1) $family/" || exit 1
}

@test "[Compilers] compiler module version available ($family)" {
    module list $family >& .cmd_output || exit 1
    local my_version=`cat .cmd_output | sed '/^\s*$/d' | sed 's/^[ \t]*//' | tail -1 | awk -F "$family/" '{print $2}'`

    if [ -z "$my_version" ];then
	flunk "ERROR: unable to ascertain module version ($my_version)"
    fi
    rm -f .cmd_output
}

@test "[Compilers] C, C++, and Fortran versions match module ($family)" {
    module list $family >& .cmd_output || exit 1
    local my_mod_version=`cat .cmd_output | sed '/^\s*$/d' | sed 's/^[ \t]*//' | tail -1 | awk -F "$family/" '{print $2}'`

    if [[ "$family" = "intel" ]];then
	# intel has no consistency here, have to punt
## 	compiler=`which icc`
## 	version=`icc --version | head -1 | awk '{print $3}'`
## 	assert_equal "$my_mod_version" "$version"
	version="assuming all is good"
  elif [[ "$family" =~ "arm" ]];then
	assert_equal "$my_mod_version" "compat"
    elif [[ "$family" =~ "gnu"  ]];then
	version=`gcc --version | head -1 | awk '{print $3}'`
	assert_equal "$my_mod_version" "$version"
	version=`g++ --version | head -1 | awk '{print $3}'`
	assert_equal "$my_mod_version" "$version"
	version=`gfortran --version | head -1 | awk '{print $4}'`
	assert_equal "$my_mod_version" "$version"
    elif [[ "$family" =~ "llvm"  ]];then
	version=`clang --version | head -1 | awk '{print $3}'`
	assert_equal "$my_mod_version" "$version"
	version=`clang++ --version | head -1 | awk '{print $3}'`
	assert_equal "$my_mod_version" "$version"
	version=`flang --version | head -1 | awk '{print $3}'`
	assert_equal "$my_mod_version" "$version"
    else
	flunk "Unsupported compiler toolchain"
    fi

    rm -f .cmd_output
}


