#!/bin/bash
# Do not overwrite existing files
# Write the new file as rpmnew
testfile() {
	if [ -e "$1" ]; then
		echo "$1.rpmnew"
	else
		echo "$1"
	fi
}

# Remove .rpmnew files if they are identical
# otherwise record in manifest
modprocess() {
	if [ "${1/.rpmnew/}" != "$1" ]; then
		if cmp "${1/.rpmnew/}" "$1"; then
			rm "$1"
			return
		fi
	fi
	md5sum "${modname}" >>@@oneapi_manifest@@
}

rm -f @@oneapi_manifest@@

# Regenerate the oneAPI modules directory
echo "Generating new oneAPI modulefiles"
/opt/intel/oneapi/modulefiles-setup.sh --ignore-latest --force --output-dir=@@OHPC_MODULEDEPS@@/oneapi/ >/dev/null
if [ ! -d @@OHPC_MODULEDEPS@@/oneapi/compiler ]; then
	echo "ERROR: Failed to create oneAPI module directory"
	exit 1
fi

# Set system defaults for default OBS oneAPI modules
# shellcheck disable=SC2043
for tool in @@exact_deps@@; do
	filename=@@OHPC_MODULEDEPS@@/oneapi/${tool%%/*}/.version
	echo "#%Module1.0" >"${filename}"
	echo "set ModulesVersion \"${tool##*/}\"" >>"${filename}"
done

# Create an OpenHPC module file for each version found in compilers
echo "Creating OpenHPC-style modulefiles for local oneAPI compiler installation(s)."
for compilers in @@OHPC_MODULEDEPS@@/oneapi/compiler/2*; do
	ver=$(basename "${compilers}")
	# Skip 2023.2.0 due to errors that can hang installer
	if [ "${ver}" = "2023.2.0" ]; then
		echo "--> Skipping version=2023.2.0 : environment module format error"
		continue
	fi
	# For the default, also specify the MKL version
	if [ "${ver}" = "@@exact_intel_ver@@" ]; then
		mklver="mkl/@@exact_mkl_ver@@"
	else
		mklver="mkl"
	fi
	echo "--> Installing modulefile for version=${ver}"
	# Do not overwrite existing files
	# Write the new file as rpmnew
	modname=$(testfile @@OHPC_MODULES@@/intel/"${ver}")

	cat <<EOF >"${modname}"
#%Module1.0#####################################################################

set version "${ver}"

proc ModulesHelp { } {
global version
puts stderr "\nThis module loads the oneAPI compiler environment.\n"
puts stderr "\nSee the man pages for icc, icpc, and ifort for detailed information"
puts stderr "on available compiler options and command-line syntax."
puts stderr "\nVersion \$version\n"
}

module-whatis "Name: Intel(R) Compiler"
module-whatis "Version: \$version"
module-whatis "Category: compiler, runtime support"
module-whatis "Description: Intel(R) Compiler Family (C/C++/Fortran for x86_64)"
module-whatis "URL: http://software.intel.com/en-us/articles/intel-compilers/"

# update module path hierarchy
prepend-path    MODULEPATH          @@OHPC_MODULEDEPS@@/intel
prepend-path    MODULEPATH          @@OHPC_MODULEDEPS@@/oneapi

# Assume no PAC device; allow override on each node
if { ![info exists ::env(ACL_SKIP_BSP_CONF)] } {
    setenv          ACL_SKIP_BSP_CONF   1
}

module load "tbb"
module load "compiler-rt"
module load "umf"
module load "compiler/\$version"
module load "${mklver}"

family "compiler"
EOF

	modprocess "${modname}"

	# Provide standalone module for use with GNU toolchain
	modname=$(testfile @@OHPC_MODULEDEPS@@/gnu/mkl/"${ver}")

	cat <<EOF >"${modname}"
#%Module1.0#####################################################################

set version "${ver}"

proc ModulesHelp { } {
global version
puts stderr "\nConfigures oneAPI MKL environment\n"
puts stderr "\$version\n"
}

module-whatis "Name: Intel(R) Math Kernel Library"
module-whatis "Version: \$version"
module-whatis "Category: library, runtime support"
module-whatis "Description: Intel(R) Math Kernel Library for C/C++ and Fortran"
module-whatis "URL: https://software.intel.com/en-us/en-us/intel-mkl"

prepend-path  MODULEPATH       @@OHPC_MODULEDEPS@@/oneapi
module load   "${mklver}"
EOF

	modprocess "${modname}"
done

# Set default version to match that used to build OpenHPC packages
modname=$(testfile @@OHPC_MODULES@@/intel/.version)

cat <<EOF >"${modname}"
#%Module1.0#####################################################################
set     ModulesVersion      "@@exact_intel_ver_module@@"
EOF

modprocess "${modname}"
