[buildout]
parts =
  firewalld-patch

extends =
  ../defaults.cfg
  ../libtool/buildout.cfg
  ../intltool/buildout.cfg
  ../glib/buildout.cfg
  ../python-slip/buildout.cfg
  ../dbus/buildout.cfg
  ../flex/buildout.cfg
  ../nftables/buildout.cfg

[firewalld]
recipe = slapos.recipe.cmmi
url = https://github.com/firewalld/firewalld/releases/download/v0.8.6/firewalld-0.8.6.tar.gz
md5sum = a120286a2c2bc12ece24d719ef2879b5
python-egg = ${buildout:parts-directory}/${:_buildout_section_name_}/lib/python${python:version}/site-packages
location = @@LOCATION@@
sbin-dir = sbin-orig
etc-root = ${:location}/etc
etc-dir = ${:etc-root}/firewalld
pre-configure =
  sed -i -E 's,(^SUBDIRS *=.*) doc(\b.*),\1\2,' Makefile.am
  aclocal -I${pkgconfig:location}/share/aclocal -I${gettext:location}/share/aclocal -I${libtool:location}/share/aclocal
  ./autogen.sh
configure-options =
  --sbindir=${:location}/${:sbin-dir}
  --sysconfdir=${:etc-root}
  --disable-docs
  --with-iptables=${iptables:location}/sbin/iptables-legacy
  --with-iptables-restore=${iptables:location}/sbin/iptables-legacy-restore
  --with-ip6tables=${iptables:location}/sbin/ip6tables-legacy
  --with-ip6tables-restore=${iptables:location}/sbin/ip6tables-legacy-restore
  --with-ebtables=${iptables:location}/sbin/ebtables-legacy
  --with-ebtables-restore=${iptables:location}/sbin/ebtables-legacy-restore
  --with-ipset=/bin/false

environment =
  PKG_CONFIG_PATH=${glib:location}/lib/pkgconfig:${pcre:location}/lib/pkgconfig
  PATH=${autoconf:location}/bin:${automake:location}/bin:${pkgconfig:location}/bin:${libtool:location}/bin:${intltool:location}/bin:${gettext:location}/bin:${glib:location}/bin:${perl:location}/bin:%(PATH)s
  CPPFLAGS=-I${gettext:location}/include
  M4=${m4:location}/bin/m4
  ACLOCAL_PATH=${pkgconfig:location}/share/aclocal:${gettext:location}/share/aclocal:${libtool:location}/share/aclocal:${glib:location}/share/aclocal:${intltool:location}/share/aclocal
  FIREWALLD_DEVEL_ENV=${buildout:parts-directory}/${:_buildout_section_name_}/src
  PYTHONPATH=${buildout:parts-directory}/${:_buildout_section_name_}/src

[dbus-python]
recipe = slapos.recipe.cmmi
version = 1.2.16
url = http://dbus.freedesktop.org/releases/dbus-python/dbus-python-${:version}.tar.gz
md5sum = 51a45c973d82bedff033a4b57d69d5d8
python-egg = ${buildout:parts-directory}/${:_buildout_section_name_}/lib/python${python:version}/site-packages
environment =
  PKG_CONFIG_PATH=${dbus:location}/lib/pkgconfig:${dbus-glib:location}/lib/pkgconfig:${glib:location}/lib/pkgconfig:${pcre:location}/lib/pkgconfig
  PATH=${pkgconfig:location}/bin:%(PATH)s
  DBUS_CFLAGS=-I${dbus:location}/include/dbus-1.0 -I${dbus:location}/lib/dbus-1.0/include
  DBUS_LIBS=-L${dbus:location}/lib -ldbus-1
  DBUS_GLIB_CFLAGS=-I${dbus-glib:location}/include/dbus-1.0
  DBUS_GLIB_LIBS=-L${dbus-glib:location}/lib -ldbus-glib-1
  CPPFLAGS=-I${glib:location}/include/glib-2.0 -I${glib:location}/lib/glib-2.0/include
  LDFLAGS=-L${glib:location}/lib -Wl,-rpath=${glib:location}/lib -L${gettext:location}/lib -Wl,-rpath=${gettext:location}/lib
  PYTHON=${buildout:executable}

[gobject-introspection]
recipe = slapos.recipe.cmmi
url = http://ftp.gnome.org/pub/gnome/core/3.22/3.22.2/sources/gobject-introspection-1.50.0.tar.xz
md5sum = 5af8d724f25d0c9cfbe6df41b77e5dc0
configure-options =
  --disable-static

environment =
  PATH=${pkgconfig:location}/bin:${gettext:location}/bin:${glib:location}/bin:${xz-utils:location}/bin:${flex:location}/bin:${bison:location}/bin:%(PATH)s
  PKG_CONFIG_PATH=${glib:location}/lib/pkgconfig:${pcre:location}/lib/pkgconfig
  CPPFLAGS=-I${glib:location}/include/glib-2.0 -I${glib:location}/lib/glib-2.0/include
  LDFLAGS=-L${glib:location}/lib -Wl,-rpath=${glib:location}/lib -L${libffi:location}/lib -Wl,-rpath=${libffi:location}/lib -lffi -L${zlib:location}/lib/ -Wl,-rpath=${zlib:location}/lib/
  GLIB_CFLAGS=-I${glib:location}/include/glib-2.0 -I${glib:location}/lib/glib-2.0/include
  GLIB_LIBS=-L${glib:location}/lib -lglib-2.0 -lintl -lgobject-2.0
  FFI_CFLAGS=-I${libffi:location}/include
  FFI_LIBS=-L${libffi:location}/lib -Wl,-rpath=${libffi:location}/lib -lffi
  GIR_DIR=${buildout:parts-directory}/${:_buildout_section_name_}/share/gir-1.0
  PYTHON=${buildout:executable}

[pygobject3]
recipe = slapos.recipe.cmmi
url = http://ftp.gnome.org/pub/gnome/core/3.22/3.22.2/sources/pygobject-3.22.0.tar.xz
python-egg = ${buildout:parts-directory}/${:_buildout_section_name_}/lib/python${python:version}/site-packages
md5sum = ed4117ed5d554d25fd7718807fbf819f
pre-configure =
  sed -i 's#/usr/local#${gobject-introspection:location}#g' ${gobject-introspection:location}/lib/pkgconfig/gobject-introspection-1.0.pc
configure-options =
  --disable-static
  --disable-cairo

environment =
  PATH=${pkgconfig:location}/bin:${libtool:location}/bin:${glib:location}/bin:${xz-utils:location}/bin:%(PATH)s
  PKG_CONFIG_PATH=${glib:location}/lib/pkgconfig:${gobject-introspection:location}/lib/pkgconfig:${pcre:location}/lib/pkgconfig
  FFI_CFLAGS=-I${libffi:location}/include
  FFI_LIBS=-L${libffi:location}/lib -Wl,-rpath=${libffi:location}/lib -lffi
  CPPFLAGS=-I${glib:location}/include/glib-2.0 -I${glib:location}/lib/glib-2.0/include -I${gettext:location}/include -I${libffi:location}/include
  LDFLAGS=-L${glib:location}/lib -Wl,-rpath=${glib:location}/lib -L${gettext:location}/lib -Wl,-rpath=${gettext:location}/lib
  GIO_LIBS=-L${glib:location}/lib -lgio-2.0
  GI_CFLAGS=-I${gobject-introspection:location}/include/gobject-introspection-1.0
  GI_LIBS=-L${gobject-introspection:location}/lib -lgirepository-1.0
  PYTHON=${buildout:executable}

[trusted-config]
recipe = slapos.recipe.build:download
url = https://raw.githubusercontent.com/t-woerner/firewalld/v0.3.14/config/zones/${:filename}
filename = trusted.xml
md5sum = 893752ba2e93a1e96334dfee19f884ad

[firewalld-patch]
recipe = plone.recipe.command
python = ${buildout:bin-directory}/${firewalld-eggs:interpreter}
stop-on-error = True
command =
  set -e
  sed -i 's#python3#${:python}#' ${firewalld:location}/bin/firewall-applet
  sed -i 's#python3#${:python}#' ${firewalld:location}/bin/firewall-cmd
  sed -i 's#python3#${:python}#' ${firewalld:location}/bin/firewall-config
  sed -i 's#python3#${:python}#' ${firewalld:location}/bin/firewall-offline-cmd
  sed -i 's#python3#${:python}#' ${firewalld:location}/${firewalld:sbin-dir}/firewalld

  sed -i 's#python3 /usr/bin/firewall-config#${:python} ${firewalld:location}/bin/firewall-config#' ${firewalld:etc-dir}/lockdown-whitelist.xml

  sed -i 's#DefaultZone=public#DefaultZone=trusted#' ${firewalld:etc-dir}/firewalld.conf
  sed -i 's#FirewallBackend=nftables#FirewallBackend=iptables#' ${firewalld:etc-dir}/firewalld.conf

  sed -i "s#'/etc/firewalld'#'${firewalld:etc-dir}'#"     ${firewalld:python-egg}/firewall/config/__init__.py
  sed -i "s#'/usr/lib/firewalld'#'${firewalld:location}/lib/firewalld'#" ${firewalld:python-egg}/firewall/config/__init__.py
  sed -i "s#'/usr/share/'#'${firewalld:location}/share'#"                ${firewalld:python-egg}/firewall/config/__init__.py

  sed -i "s#import sys#import sys, os\n\nos.environ['GI_TYPELIB_PATH'] = '${gobject-introspection:location}/lib/girepository-1.0/'#" ${:python}

  cp -f ${firewalld:location}/lib/firewalld/zones/trusted.xml ${firewalld:etc-dir}/zones/
  mkdir -p ${firewalld:location}/sbin
  echo -n '#!/bin/sh\nLD_LIBRARY_PATH=${nftables:location}/lib exec ${firewalld:location}/${firewalld:sbin-dir}/firewalld "$@"' > ${firewalld:location}/sbin/firewalld
  chmod a+x ${firewalld:location}/sbin/firewalld

  # no need to patch dbus if not in top level compilation
  [ "$(stat -c '%U' ${dbus:location}/share/dbus-1/system.conf)" = "slapsoft" ] || exit 0
  sed -i 's#<user>messagebus</user>#<user>slapsoft</user>#' ${dbus:location}/share/dbus-1/system.conf
  cp -f ${firewalld:location}/share/dbus-1/system.d/FirewallD.conf ${dbus:location}/share/dbus-1/system.d/

update-command = ${:command}
stop-on-error = true

[firewalld-eggs]
recipe = zc.recipe.egg:scripts
interpreter = python.eggs
eggs =
  decorator
  six
  ${python-slip-egg:eggs}

extra-paths =
  ${dbus-python:python-egg}
  ${firewalld:python-egg}
  ${pygobject3:python-egg}
  ${nftables:python-egg}
