%global macrosdir %(d=%{_rpmconfigdir}/macros.d; [ -d $d ] || d=%{_sysconfdir}/rpm; echo $d)
# We'd like to be noarch, but Lmod encodes the system lua path
%global debug_package %{nil}

Name:           Lmod
Version:        8.1.17
Release:        2
Summary:        Environmental Modules System in Lua

License:        MIT and LGPLv2
URL:            https://github.com/TACC/Lmod
Source0:        https://github.com/TACC/Lmod/archive/%{version}.tar.gz#/Lmod-%{version}.tar.gz
Source1:        macros.%{name}

# redirect echo stmts to stderr in init/bash.in
# Having them go to stdout breaks openQA tests when Lmod is installed
# Backported version of
# https://github.com/TACC/Lmod/commit/fa2e3a1a84951b38a56c76391e6b0c340c0199a7
Patch0:         lmod-8.1.17-echo_stderr.patch
Patch1:         Support-for-lua-5.4-added.patch

BuildRequires:  gcc
BuildRequires:  lua-devel
BuildRequires:  lua-filesystem
BuildRequires:  lua-json
BuildRequires:  lua-posix
BuildRequires:  lua-term
BuildRequires:  tcl-devel
BuildRequires:  zsh
Requires:       lua-filesystem
Requires:       lua-json
Requires:       lua-posix
Requires:       lua-term
Requires:       /usr/bin/ps
Provides:       environment(modules)

%description
Lmod is a Lua based module system that easily handles the MODULEPATH
Hierarchical problem.  Environment Modules provide a convenient way to
dynamically change the users' environment through modulefiles. This includes
easily adding or removing directories to the PATH environment variable.
Modulefiles for library packages provide environment variables that specify
where the library and header files can be found.


%prep
%setup -q
%patch0 -p1
%patch1 -p1
sed -i -e 's,/usr/bin/env ,/usr/bin/,' src/*.tcl
# Remove bundled lua-filesystem and lua-term
rm -r pkgs/{luafilesystem,term} tools/json.lua
#sed -i -e 's, pkgs , ,' Makefile.in
# Remove unneeded shbangs
sed -i -e '/^#!/d' init/*.in


%build
%configure --prefix=%{_datadir} PS=/usr/bin/ps
%make_build


%install
%make_install
# init scripts are sourced
find %{buildroot}%{_datadir}/lmod/%{version}/init -type f -exec chmod -x {} +
mkdir -p %{buildroot}%{_sysconfdir}/modulefiles
mkdir -p %{buildroot}%{_datadir}/modulefiles
mkdir -p %{buildroot}%{_sysconfdir}/profile.d

cat <<'EOF' > %{buildroot}%{_sysconfdir}/profile.d/00-modulepath.sh
[ -z "$MODULEPATH" ] &&
  [ "$(readlink /etc/alternatives/modules.sh)" = "/usr/share/lmod/lmod/init/profile" -o -f /etc/profile.d/z00_lmod.sh ] &&
  export MODULEPATH=%{_sysconfdir}/modulefiles:%{_datadir}/modulefiles || :
EOF

cat << 'EOF' > %{buildroot}%{_sysconfdir}/profile.d/00-modulepath.csh
if (! $?MODULEPATH && ( `readlink /etc/alternatives/modules.csh` == /usr/share/lmod/lmod/init/cshrc || -f /etc/profile.d/z00_lmod.csh ) ) then
  setenv MODULEPATH %{_sysconfdir}/modulefiles:%{_datadir}/modulefiles
endif
EOF

# Add a snippet to make sure that the 00-modulepath.* is included, when
# the user calls /etc/profile.d/modules.sh directly, just below
# the shbang line.
sed -i '2i\. /etc/profile.d/00-modulepath.sh\n' \
  %{buildroot}%{_datadir}/lmod/lmod/init/profile

# Install the rpm config file
install -Dpm 644 %{SOURCE1} %{buildroot}/%{macrosdir}/macros.%{name}

%files
%license License
%doc INSTALL README.md README_lua_modulefiles.txt
%{_sysconfdir}/modulefiles
%config(noreplace) %{_sysconfdir}/profile.d/00-modulepath.csh
%config(noreplace) %{_sysconfdir}/profile.d/00-modulepath.sh
%ghost %{_sysconfdir}/profile.d/modules.csh
%ghost %{_sysconfdir}/profile.d/modules.sh
%{_datadir}/lmod
%{_datadir}/modulefiles
%{macrosdir}/macros.%{name}


%changelog
* Sat May 7 2022 liyanan <liyanan32@h-partners.com> - 8.1.17-2
- Support for lua 5.4 added

* Fri Oct 10 2020 wangxiao <wangxiao65@huawei.com> - 8.1.17-1
- package init.
