{ stdenv
, fetchurl
, perl
, coreutils
, iproute
, ipset
, iptables
, ebtables
, shorewall-core
, logger
, gnugrep
, gnused
}:
let
  PATH = stdenv.lib.concatStringsSep ":"
           [ "${coreutils}/bin"
             "${iproute}/bin"
             "${iptables}/bin"
             "${ipset}/bin"
             "${ebtables}/bin"
             "${logger}/bin"
             "${gnugrep}/bin"
             "${gnused}/bin"
           ];
in
stdenv.mkDerivation rec {
  baseName = "shorewall";
  version = "5.2.0.5";
  name = "${baseName}-${version}";

  src = fetchurl {
    url = "http://www.shorewall.net/pub/shorewall/5.2/shorewall-5.2.0/${baseName}-${version}.tar.bz2";
    sha256 = "005qv6kybk1jn1i63rchf86kwbxwwn463cxkp03q0mpc0cnj018w";
  };

  buildInputs = [
    coreutils
    iproute
    ipset
    iptables
    ebtables
    shorewall-core
    logger
    gnugrep
    gnused
  ];
  configurePhase = ''
    PATH=$PATH:${shorewall-core}/bin \
    ./configure \
      HOST=linux \
      PREFIX=$out \
      CONFDIR=\$PREFIX/etc \
      SBINDIR=\$PREFIX/sbin \
      SYSCONFDIR=\$PREFIX/etc/default \
      VARLIB=/var/lib \
      INITSOURCE= \
      INITDIR= \
      INITFILE= \
      DEFAULT_PAGER=
  '';
  installPhase = ''
    # Merge with shorewall-core
    cp -r -s ${shorewall-core} $out
    chmod u+w $out $out/*/ $out/*/*/ $out/*/*/*/
    ln -fns bin $out/sbin
    # Install shorewallrc and shorewall
    cp --remove-destination shorewallrc $out/share/shorewall/
    ./install.sh
    cp ${shorewall-core}/sbin/shorewall $out/sbin/shorewall4
    ln -fns shorewall4 $out/sbin/shorewall
    # Actual config will be expected in /etc/shorewall
    sed -i $out/share/shorewall/shorewallrc \
        -e 's~^CONFDIR=.*~CONFDIR=/etc~'
    # Fix PATH
    sed -i $out/sbin/shorewall \
        -e 's~^\. ${shorewall-core}\(/share/shorewall/shorewallrc\)~. '$out'\1~' \
        -e 's~^PRODUCT=.*~&\nexport PATH=${PATH}''${PATH:+:}$PATH~'
    # Fix shorewall.conf
    sed -i $out/etc/shorewall/shorewall.conf \
        -e 's|^LOGFILE=.*|LOGFILE=/var/log/shorewall.log|' \
        -e 's|^PATH=.*|PATH=${PATH}|' \
        -e 's|^PERL=.*|PERL=${perl}/bin/perl|' \
        -e 's|^SHOREWALL_SHELL=.*|SHOREWALL_SHELL=${stdenv.shell}|'
    # FIXME: the default GEOIPDIR=/usr/share/xt_geoip/LE may require attention.
  '';

  meta = {
    homepage = http://www.shorewall.net/;
    description = "An IPv4 gateway/firewall configuration tool for GNU/Linux";
    longDescription = ''
      Shorewall is a high-level tool for configuring Netfilter. You describe your
      firewall/gateway requirements using entries in a set of configuration
      files. Shorewall reads those configuration files and with the help of the
      iptables, iptables-restore, ip and tc utilities, Shorewall configures
      Netfilter and the Linux networking subsystem to match your requirements.
      Shorewall can be used on a dedicated firewall system, a multi-function
      gateway/router/server or on a standalone GNU/Linux system. Shorewall does
      not use Netfilter's ipchains compatibility mode and can thus take
      advantage of Netfilter's connection state tracking capabilities.
    '';
    license = stdenv.lib.licenses.gpl2Plus;
    platforms = stdenv.lib.platforms.linux;
  };
}