# figure out why --debugger:native does not work with *-debuginfo packets
%define __nim_common_opts -d:release
%define __nim_harden_config() \
  for file in %*; do \
    echo "gcc.options.always %= \\"\\${gcc.options.always} ${CFLAGS:-%optflags}\\"" >> $file; \
    echo "gcc.options.linker %= \\"\\${gcc.options.linker} ${LDFLAGS:-%__global_ldflags}\\"" >> $file; \
  done

%global bashcompdir     %(pkg-config --variable=completionsdir bash-completion 2>/dev/null)
%global bashcomproot    %(dirname %{bashcompdir} 2>/dev/null)

%global rev 2.g4164ec4f8

Name:           nim
Version:        0.18.0
Release:        %{rev}%{?dist}
Summary:        Statically typed, imperative programming language
# compiler is MIT, nimble package manager is BSD
License:        MIT and BSD
URL:            https://nim-lang.org
ExclusiveArch:  %{nim_arches}

Source0:        https://avsej.fedorapeople.org/nim.src/%{version}-%{rev}/nim-%{version}.tar.xz
Source1:        nim.1
Source2:        nimgrep.1
Source3:        nimble.1
Source4:        nimsuggest.1

Patch0:         0001-allow-to-override-directories-in-install-script.patch
Patch1:         0002-use-_datadir-for-platform-independent-library-path.patch
Patch6:         0006-use-versioned-source-links-in-docs.patch
# https://github.com/nim-lang/Nim/issues/6747
Patch7:         0007-fix-floats-in-tests.patch
Patch8:         0008-fix-compiler-binary-lookup-in-docgen.patch
Patch9:         0009-allow-to-pass-extra-compiler-options-for-tools.patch

# Default gcc options in nim.conf include hardened specs
Requires:       redhat-rpm-config
# Currently compiler uses C sources as intermediate representation
Requires:       gcc

BuildRequires:  nim-srpm-macros
BuildRequires:  gcc openssl-devel
BuildRequires:  pkgconfig(bash-completion)

# test dependencies
BuildRequires:  nodejs-devel
BuildRequires:  gc-devel

# doc dependencies
BuildRequires:  tex(latex)
BuildRequires:  tex(geometry.sty)
BuildRequires:  tex(inputenc.sty)
BuildRequires:  tex(fontenc.sty)
BuildRequires:  tex(graphicx.sty)
BuildRequires:  tex(lmodern.sty)
BuildRequires:  tex(courier.sty)
BuildRequires:  tex(lmodern.sty)
BuildRequires:  tex(tabularx.sty)
BuildRequires:  tex(hyperref.sty)


%if ! (0%{?rhel} && 0%{?rhel} <= 7)
Recommends: %{name}-tools%{_isa} = %{version}-%{release}
# for using --gc:boehm
Suggests: gc-devel
Suggests: %{name}-docs = %{version}-%{release}
%endif

%description
Nim is a compiled, garbage-collected systems programming language with a
design that focuses on efficiency, expressiveness, and elegance (in that
order of priority).

%package tools
Summary:    Tools for Nim programming language
%description tools
Nim is a compiled, garbage-collected systems programming language with a
design that focuses on efficiency, expressiveness, and elegance (in that
order of priority).

This package provides various tools, which help Nim programmers.

%package doc
Summary:    Documentation for Nim programming language
BuildArch:  noarch
%description doc
Nim is a compiled, garbage-collected systems programming language with a
design that focuses on efficiency, expressiveness, and elegance (in that
order of priority).

This package provides documentation and reference manual for the language
and its standard library.

%prep
%autosetup -p1

%build
# build bootstrap compiler
./build.sh

%__nim_harden_config compiler/nim.cfg
%__nim_harden_config config/nim.cfg

# build the koch management tool
./bin/nim compile %{__nim_common_opts} koch
# build a new version of the compiler
./koch boot %{__nim_common_opts} -d:useLinenoise
# build tools
./koch tools %{__nim_common_opts}

# generate documentation
./koch web --onlyDocs
sed -i '/<link.*fonts.googleapis.com/d' doc/html/*.html
./koch pdf

rm -rf examples install.sh lib/pure/unidecode/gen.py
# generate installer script
./koch geninstall %{__nim_common_opts}

%install
sh install.sh \
  %{buildroot}%{_bindir} \
  %{buildroot}%{_sysconfdir} \
  %{buildroot}%{_datadir} \
  %{buildroot}%{_docdir} \
  %{buildroot}%{_datadir}

install -Dp -m755 bin/nim{ble,grep,suggest} %{buildroot}%{_bindir}
install -Dp -m644 tools/nim.bash-completion %{buildroot}%{bashcompdir}/nim
install -Dp -m644 dist/nimble/nimble.bash-completion %{buildroot}%{bashcompdir}/nimble
install -Dp -m644 -t%{buildroot}%{_mandir}/man1 %SOURCE1 %SOURCE2 %SOURCE3 %SOURCE4

mkdir -p %{buildroot}%{_docdir}/%{name}/{pdf,html}
cp -a doc/html/*.html %{buildroot}%{_docdir}/%{name}/html/
cp -a doc/pdf/*.pdf %{buildroot}%{_docdir}/%{name}/pdf/
cp tools/dochack/nimcache/dochack.js %{buildroot}%{_docdir}/%{name}/
ln -sr %{buildroot}%{_docdir}/%{name}/html/{overview,index}.html

%check
for cat in gc threads nimble-all examples lib io async rodfiles js debugger
do
  ./koch tests --pedantic category $cat -d:nimCoroutines || (echo "$cat test category failed" &&  exit 1)
done

%files
%license copying.txt dist/nimble/license.txt
%doc readme.txt
%config(noreplace) %{_sysconfdir}/*.cfg
%{_datadir}/nim/
%{_datadir}/nimble/
%{_bindir}/nim{,ble}
%{_mandir}/man1/nim{,ble}.1*
%{bashcompdir}/nim*

%files tools
%license copying.txt
%{_bindir}/nim{grep,suggest}
%{_mandir}/man1/nim{grep,suggest}.1*

%files doc
%doc %{_docdir}/nim

%changelog
* Sun Mar 4 2018 Sergey Avseyev <sergey.avseyev@gmail.com> - 0.18.0-2.g4164ec4f8
- Update development package

* Wed Jan 3 2018 Sergey Avseyev <sergey.avseyev@gmail.com> - 0.17.2-5
- Revert back to makefile for bootstrap compiler.

* Wed Jan 3 2018 Sergey Avseyev <sergey.avseyev@gmail.com> - 0.17.2-4
- Update runtime dependencies, and use build.sh instead of makefile
  to build bootstrap compiler.

* Fri Dec 1 2017 Sergey Avseyev <sergey.avseyev@gmail.com> - 0.17.2-3
- Do not load remote fonts in local HTML documentation

* Sat Nov 18 2017 Sergey Avseyev <sergey.avseyev@gmail.com> - 0.17.2-2
- Generate and install PDF documentation

* Tue Sep 26 2017 Sergey Avseyev <sergey.avseyev@gmail.com> - 0.17.2-1
- Initial package