%bcond_with systemd
%bcond_with lua
%global with_python3 0
%if %{with systemd}
%global with_systemd 0
%endif

%global luaver 5.4
%global lualibdir %{_libdir}/lua/%{luaver}
%global luapkgdir %{_datadir}/lua/%{luaver}

%define _unpackaged_files_terminate_build 1

Name:           lxc
Version:        4.0.9
Release:        1%{?_dist_release}%{?with_systemd:.systemd}
Summary:        Linux Resource Containers
Summary(ja):    Linux リソースコンテナ
Group:          virtualization
License:        LGPLv2+ and GPLv2
URL:            https://linuxcontainers.org/
Vendor:         Project Vine
Distribution:   Vine Linux

Source0:        https://linuxcontainers.org/downloads/lxc/%{name}-%{version}.tar.gz
Source10:       lxc-vinelinux
Source11:       vinelinux.common.conf
Source12:       vinelinux.userns.conf

Patch0:         0001-fix-initscript.patch
Patch100:       0002-fix-launching-dnsmasq.patch

BuildRequires:  docbook-utils
BuildRequires:  docbook2X
BuildRequires:  doxygen
BuildRequires:  glibc-static
BuildRequires:  kernel-headers
BuildRequires:  libcap-static
BuildRequires:  libtool
%if %{with lua}
BuildRequires:  lua-devel
%endif
BuildRequires:  perl-XML-SAX
%if 0%{?with_python3}
BuildRequires:  python3-devel >= 3.2
BuildRequires:  python3-rpm-macros
%endif # with_python3
%if 0%{?with_systemd}
BuildRequires:  systemd
%endif # with_systemd
# for lxc-top
%if %{with lua}
Requires:       lua-%{name}%{?_isa} = %{version}-%{release}
Requires:       lua-alt-getopt
%endif
%if !0%{?with_systemd}
Requires:       libcgroup
%endif
Obsoletes:      lxc-extra < 3.0.0
Obsoletes:      lxc-lua < 3.0.0
Obsoletes:      python3-lxc < 3.0.0

%{!?_pkgdocdir: %global _pkgdocdir %{_docdir}/%{name}-%{version}}

%description
Linux Resource Containers provide process and resource isolation without the
overhead of full virtualization.


%package        libs
Summary:        Runtime library files for %{name}
Summary(ja):    %{name}のランタイムライブラリ
Group:          virtualization
%if 0%{?with_systemd}
Requires(post): systemd
Requires(preun): systemd
Requires(postun): systemd
%else
Requires(post): chkconfig
Requires(preun): initscripts, chkconfig
Requires(postun): initscripts
%endif # with_systemd
Obsoletes: compat32-lxc-init-static.32bit < 2.0.1-3 

%description libs
Linux Resource Containers provide process and resource isolation without the
overhead of full virtualization.

The %{name}-libs package contains libraries for running %{name} applications.


%if 0%{?with_python3}
%package        -n python3-%{name}
Summary:        Python binding for %{name}
Summary(ja):    %{name} の Python バインディング
Group:          programming

%description    -n python3-%{name}
Linux Resource Containers provide process and resource isolation without the
overhead of full virtualization.

The python3-%{name} package contains the Python3 binding for %{name}.

%global __provides_exclude %{?__provides_exclude:%__provides_exclude|}_lxc\\..*\\.so


%package        extra
Summary:        Extra tools for %{name}
Summary(ja):    %{name} の追加ツール
Group:          virtualization
Requires:       python3-%{name} = %{version}-%{release}

%description    extra
Linux Resource Containers provide process and resource isolation without the
overhead of full virtualization.

This package contains tools needing the Python3 bindings.
%endif # with_python3


%if %{with lua}
%package        -n lua-%{name}
Summary:        Lua binding for %{name}
Summary(ja):    %{name} の Lua バインディング
Group:          programming

%description    -n lua-%{name}
Linux Resource Containers provide process and resource isolation without the
overhead of full virtualization.

The lua-%{name} package contains the Lua binding for %{name}.

%endif

%global __provides_exclude %{?__provides_exclude:%__provides_exclude|}core\\.so\\.0


%package        templates
Summary:        Templates for %{name}
Summary(ja):    %{name} のテンプレート
Group:          virtualization
Requires:       %{name}-libs%{?_isa} = %{version}-%{release}
# Note: Requirements for the template scripts (busybox, dpkg,
# debootstrap, vbootstrap, rsync, openssh-server, dhclient, apt, pacman, zypper,
# ubuntu-cloudimg-query etc...) are not explicitly mentioned here:
# their presence varies wildly on supported Fedora/EPEL releases and
# archs, and they are in most cases needed for a single template
# only. Also, the templates normally fail graciously when such a tool
# is missing. Moving each template to its own subpackage on the other
# hand would be overkill.


%description    templates
Linux Resource Containers provide process and resource isolation without the
overhead of full virtualization.

The %{name}-templates package contains templates for creating containers.


%package        devel
Summary:        Development files for %{name}
Summary(ja):    %{name}の開発ファイル
Group:          programming
Requires:       %{name}-libs%{?_isa} = %{version}-%{release}
Requires:       pkgconfig

%description    devel
Linux Resource Containers provide process and resource isolation without the
overhead of full virtualization.

The %{name}-devel package contains libraries and header files for
developing applications that use %{name}.


%package        doc
Summary:        Documentation for %{name}
Summary(ja):    %{name} のドキュメント
Group:          documentation
BuildArch:      noarch

%description    doc
This package contains documentation for %{name}.


%prep
%setup -q -n %{name}-%{version}
%if !0%{?with_systemd}
%patch0 -p1
%endif
%patch100 -p1


%build
./autogen.sh
%configure --with-distro=centos \
           --enable-doc \
           --enable-api-docs \
           --disable-silent-rules \
           --docdir=%{_pkgdocdir} \
           --disable-werror \
           --disable-rpath \
           --disable-static \
           --disable-apparmor \
           --disable-selinux \
           --enable-capabilities \
           --enable-examples \
           --enable-bash \
%if %{with lua}
           --enable-lua \
%endif
%if 0%{?with_python3}
           --enable-python \
%endif # with_python3
%if 0%{?with_systemd}
           --with-init-script=systemd \
%else
           --with-init-script=sysvinit \
%endif # with_systemd
           %{nil}

make %{?_smp_mflags}


%install
%{make_install}
make DESTDIR=%{buildroot} install
mkdir -p %{buildroot}%{_localstatedir}/lib/%{name}
%if %{with lua}
chmod -x %{buildroot}%{luapkgdir}/lxc.lua
%endif

rm -rf rpmdoc
mkdir -p rpmdoc/api
cp -a doc/api/html/* rpmdoc/api/
mv -f %{buildroot}%{_docdir}/lxc/examples rpmdoc/

# cache dir
mkdir -p %{buildroot}%{_localstatedir}/cache/%{name}

rm -rf %{buildroot}%{_datadir}/lxc/selinux

# install vinelinux template
install -m775 %{SOURCE10} %{buildroot}%{_datadir}/lxc/templates/
install -m775 %{SOURCE11} %{buildroot}%{_datadir}/lxc/config/
install -m775 %{SOURCE12} %{buildroot}%{_datadir}/lxc/config/

rm -f %{buildroot}%{_libdir}/liblxc.{a,la}


%check
make check


%post libs
%if 0%{?with_systemd}
%systemd_post %{name}-net.service
%systemd_post %{name}.service
%systemd_post %{name}@.service
%else
/sbin/chkconfig --add %{name}
%endif # with_systemd


%preun libs
%if 0%{?with_systemd}
%systemd_preun %{name}-net.service
%systemd_preun %{name}.service
%systemd_preun %{name}@.service
%else
if [ $1 -eq 0 -o -x /bin/systemctl ]; then
        /sbin/service %{name} stop > /dev/null 2>&1
        /sbin/chkconfig --del %{name}
fi
%endif # with_systemd


%postun libs
%if 0%{?with_systemd}
%systemd_postun %{name}-net.service
%systemd_postun %{name}.service
%systemd_postun %{name}@.service

%else
if [ $1 -ge 1 ]; then
        /sbin/service %{name} condrestart > /dev/null 2>&1 || :
fi
%endif # with_systemd


%files
%license COPYING LICENSE*
%doc AUTHORS MAINTAINERS README*
%doc rpmdoc/examples
%{_bindir}/%{name}-*
%{_mandir}/man1/%{name}*
%{_mandir}/*/man1/%{name}*
# in lxc-libs:
%exclude %{_bindir}/%{name}-autostart
%exclude %{_mandir}/man1/%{name}-autostart*
%exclude %{_mandir}/*/man1/%{name}-autostart*
%exclude %{_mandir}/man1/%{name}-user-nic*
%exclude %{_mandir}/*/man1/%{name}-user-nic*
%{_datadir}/%{name}/%{name}.functions
%if 0%{?with_python3}
%exclude %{_bindir}/%{name}-device
%exclude %{_bindir}/%{name}-ls
%exclude %{_mandir}/man1/%{name}-device*
%exclude %{_mandir}/man1/%{name}-ls*
%exclude %{_mandir}/*/man1/%{name}-device*
%exclude %{_mandir}/*/man1/%{name}-ls*
%endif
%dir %{_sysconfdir}/bash_completion.d/
%{_sysconfdir}/bash_completion.d/%{name}


%files libs
%dir %{_datadir}/%{name}
%dir %{_datadir}/%{name}/templates
%dir %{_datadir}/%{name}/config
%{_datadir}/%{name}/hooks
%{_datadir}/%{name}/%{name}-patch.py*
%{_libdir}/liblxc.so.*
%{_libdir}/%{name}
%{_libexecdir}/%{name}
%{_sbindir}/init.%{name}
%{_sbindir}/init.%{name}.static
%{_bindir}/%{name}-autostart
%{_localstatedir}/lib/%{name}
%dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/default.conf
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}
%{_mandir}/man1/%{name}-autostart*
%{_mandir}/*/man1/%{name}-autostart*
%{_mandir}/man1/%{name}-user-nic*
%{_mandir}/*/man1/%{name}-user-nic*
%{_mandir}/man5/%{name}*
%{_mandir}/man7/%{name}*
%{_mandir}/*/man5/%{name}*
%{_mandir}/*/man7/%{name}*
%if 0%{?with_systemd}
%{_unitdir}/%{name}.service
%{_unitdir}/%{name}@.service
%{_unitdir}/%{name}-net.service
%else
%{_sysconfdir}/rc.d/init.d/%{name}
%{_sysconfdir}/rc.d/init.d/%{name}-net
%endif # with_systemd
%dir %{_localstatedir}/cache/%{name}


%if 0%{?with_python3}
%files -n python3-%{name}
%{python3_sitearch}/*


%files extra
%{_bindir}/%{name}-device
%{_bindir}/%{name}-ls
%{_mandir}/man1/%{name}-device*
%{_mandir}/man1/%{name}-ls*
%{_mandir}/*/man1/%{name}-device*
%{_mandir}/*/man1/%{name}-ls*
%endif # with_python3


%if %{with lua}
%files -n lua-%{name}
%{lualibdir}/%{name}
%{luapkgdir}/%{name}.lua
%endif


%files templates
%{_datadir}/%{name}/templates/lxc-*
%{_datadir}/%{name}/config/*


%files devel
%{_libdir}/pkgconfig/%{name}.pc
%{_includedir}/lxc
%{_libdir}/liblxc.so


%files doc
%license COPYING LICENSE*
%doc AUTHORS MAINTAINERS README*
%doc rpmdoc/api


%changelog
* Wed May 12 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.0.9-1
- new upstream release,

* Mon Aug 10 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.0.4-1
- new upstream release,
- added systemd support (disabled as default).

* Sat Apr 04 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.0.0-1
- updated to 4.0.0.
- updated lxc-vinelinux.

* Mon Mar 16 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.0.4-1
- updated to 3.0.4.
- disabled cgmanager.

* Tue Apr 09 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.0.3-2
- updated the Vine Linux template for lxc-3.0.x.

* Fri Jan 11 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.0.3-1
- updated to 3.0.3.
- updated Patch0.
- updated Patch100.
- disabled lua.
- disabled python.
- added R:libcgroup.

* Wed Aug 30 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.0.8-2
- enabled cgmanager.

* Tue Aug 29 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.0.8-1
- updated to 2.0.8.
- updated Patch0.
- dropped Patch1: added an option to get same effect.

* Wed Jun 29 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.0.1-3
- made lxc-net to be able to disable dnsmasq.
- dropped compat32-lxc-init-static.

* Sun Jun 26 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.0.1-2
- enabled init.lxc.static for lxc-execute.

* Thu Jun 23 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.0.1-1
- updated to 2.0.1.
- replaced all patches.

* Tue Nov 17 2015 Daisuke SUZUKI <daisuke@vinelinux.org> 1.1.5-2
- enable python3 support by default

* Sun Nov 15 2015 Daisuke SUZUKI <daisuke@vinelinux.org> 1.1.5-1
- update vinelinux template
  - add vinelinux.{common,userns}.conf
- add patch1 to disable setproctitle error message,
  cherry picked from upstream git.

* Sun Nov 15 2015 Daisuke SUZUKI <daisuke@vinelinux.org> 1.1.4-1
- update to 1.1.4-1
- add vinelinux template

* Wed Apr 10 2013 Daisuke SUZUKI <daisuke@linux.or.jp> 0.8.0-3
- add patch10 to not fail on failure to link kmsg

* Wed Nov 21 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 0.8.0-2
- update to 0.8.0 release

* Fri May 25 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 0.8.0-1.20120525
- update to git master

* Wed Apr 25 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 0.8.0-0.rc1
- new upstream release 

* Thu Jan 26 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 0.7.5-1.20120105
- update to git current (20120105)

* Mon Jun 06 2011 Daisuke SUZUKI <daisuke@linux.or.jp> 0.7.4.2-1
- update to 0.7.4.2

* Thu Apr 28 2011 Daisuke SUZUKI <daisuke@linux.or.jp> 0.7.4.1-2
- include all templates

* Wed Apr 27 2011 Daisuke SUZUKI <daisuke@linux.or.jp> 0.7.4.1-1
- initial build for Vine Linux

* Fri Mar 25 2011 Silas Sewell <silas@sewell.ch> - 0.7.4.1-1
- Update to 0.7.4.1

* Tue Feb 08 2011 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 0.7.2-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_15_Mass_Rebuild

* Mon Jul 26 2010 Silas Sewell <silas@sewell.ch> - 0.7.2-1
- Update to 0.7.2
- Remove templates

* Tue Jul 06 2010 Silas Sewell <silas@sewell.ch> - 0.7.1-1
- Update to 0.7.1

* Wed Feb 17 2010 Silas Sewell <silas@sewell.ch> - 0.6.5-1
- Update to latest release
- Add /var/lib/lxc directory
- Patch for sys/stat.h

* Fri Nov 27 2009 Silas Sewell <silas@sewell.ch> - 0.6.4-1
- Update to latest release
- Add documentation sub-package

* Mon Jul 27 2009 Silas Sewell <silas@sewell.ch> - 0.6.3-2
- Apply patch for rawhide kernel

* Sat Jul 25 2009 Silas Sewell <silas@sewell.ch> - 0.6.3-1
- Initial package