%global commit ${GITREVLONG}

%if 0%{?rhel} && 0%{?rhel} <= 7
%define python3_build 0
#%{!?__python2: %global __python2 /usr/bin/python2}
#%{!?python2_sitelib: %global python2_sitelib %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}
#%{!?python2_sitearch: %global python2_sitearch %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
%else
%define python3_build 1
%endif


Name:           libcomps
Version:        ${VERSION}
Release:        ${libcomps_RELEASE}%{?dist}
Summary:        Comps XML file manipulation library

Group:          Development/Libraries
License:        GPLv2+
URL:            https://github.com/midnightercz/libcomps/
Source0:        https://github.com/midnightercz/libcomps/${SOURCE_URL_PATH}
BuildRequires:  libxml2-devel
BuildRequires:  check-devel
BuildRequires:  expat-devel
#%if 0%{?rhel} == 6
#BuildRequires:  cmake28
#%else
BuildRequires:  cmake
#%endif

%description
Libcomps is library for structure-like manipulation with content of
comps XML files. Supports read/write XML file, structure(s) modification.

%package doc
Summary:        Documentation files for libcomps library
Group:          Documentation
Requires:       %{name} = %{version}-%{release}
BuildArch:      noarch
BuildRequires:  doxygen

%description doc
Documentation files for libcomps library

%package -n python-libcomps-doc
Summary:        Documentation files for python bindings libcomps library
Group:          Documentation
Requires:       %{name} = %{version}-%{release}
BuildArch:      noarch
BuildRequires:  python-sphinx

%description -n python-libcomps-doc
Documentation files for python bindings libcomps library

%package devel
Summary:        Development files for libcomps library
Group:          Development/Libraries
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description devel
Development files for libcomps library

%package -n python-libcomps
Summary:        Python2 bindings for libcomps library
Group:          Development/Libraries
BuildRequires:  python-devel
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description -n python-libcomps
Python2 bindings for libcomps library

%if %python3_build
%package -n python3-libcomps
Summary:        Python3 bindings for libcomps library
Group:          Development/Libraries
BuildRequires:  python3-devel
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description -n python3-libcomps
Python3 bindings for libcomps library
%endif

%prep
%setup -qn %{name}-%{commit}

%if %python3_build == 1
rm -rf py3
mkdir ../py3
cp -a . ../py3/
mv ../py3 ./
%endif

%build
%cmake -DPYTHON_DESIRED:STRING=2 libcomps/
make %{?_smp_mflags}
make %{?_smp_mflags} docs
make %{?_smp_mflags} pydocs

%if %python3_build == 1
pushd py3
%cmake -DPYTHON_DESIRED:STRING=3 libcomps/
make %{?_smp_mflags}
popd
%endif


%check
make test
%if %{python3_build}
pushd py3
make pytest
popd
%endif

%install
make install DESTDIR=%{buildroot}

%if %{python3_build}
pushd py3
make install DESTDIR=%{buildroot}
popd
%endif

%clean
rm -rf $buildroot

%post -p /sbin/ldconfig

%postun -p /sbin/ldconfig

%files
%{_libdir}/libcomps.so.*
%doc README.md COPYING

%files devel
%{_libdir}/libcomps.so
%{_includedir}/*

%files doc
%doc docs/libcomps-doc/html

%files -n python-libcomps-doc
%doc src/python/docs/html

%files -n python-libcomps
%{_libdir}/python2*
#%exclude %{_libdir}/python2/libcomps/__pycache__

%if %{python3_build}
%files -n python3-libcomps
%{_libdir}/python3*
#%exclude %{_libdir}/python3/libcomps/__pycache__
%endif

%changelog
${CHANGELOG}
