<commit_before># when updating version, wait for the build to fail
# run make without sourcing the environment and let libreoffice
# download all extra files
# then list extra files separated by newline and pipe them to
# generate-libreoffice-srcs.sh and copy output to libreoffice-srcs.nix

{ stdenv, fetchurl, pam, python3, tcsh, libxslt, perl, ArchiveZip
, CompressZlib, zlib, libjpeg, expat, pkgconfigUpstream, freetype, libwpd
, libxml2, db, sablotron, curl, fontconfig, libsndfile, neon
, bison, flex, zip, unzip, gtk, libmspack, getopt, file, cairo, which
, icu, boost, jdk, ant, cups, xorg
, openssl, gperf, cppunit, GConf, ORBit2, poppler
, librsvg, gnome_vfs, mesa
, autoconf, automake, openldap, bash, hunspell, librdf_redland, nss, nspr
, libwpg, dbus_glib, glibc, qt4, kde4, clucene_core, libcdr, lcms, vigra
, unixODBC, mdds, saneBackends, mythes, libexttextcat, libvisio
, fontsConf, pkgconfig, libzip, bluez5, libtool, maven
, libatomic_ops, graphite2, harfbuzz, libodfgen
, librevenge, libe-book, libmwaw, glm, glew, gst_all_1
, langs ? [ "en-US" "en-GB" "ca" "ru" "eo" "fr" "nl" "de" "sl" ]
}:

let
  langsSpaces = stdenv.lib.concatStringsSep " " langs;
  major = "4";
  minor = "4";
  patch = "3";
  tweak = "2";
  subdir = "${major}.${minor}.${patch}";
  version = "${subdir}${if tweak == "" then "" else "."}${tweak}";

  # doesn't exist in srcs
  # 0.8 version is in 0.7.0 tarball
  libixion = stdenv.mkDerivation rec {
     version = "0.7.0";
     name = "libixion-${version}";

     src = fetchurl {
       url = "http://kohei.us/files/ixion/src/${name}.tar.bz2";
       sha256 = "10amvz7fzr1kcy3svfspkdykmspqgpjdmk44cyr406wi7v4lwnf9";
     };

     buildInputs = [ boost mdds pkgconfig ];

     configureFlags = [ "--with-boost=${boost.dev}" ];
  };

  fetchThirdParty = {name, md5, brief, subDir ? ""}: fetchurl {
    inherit name md5;
    url = if brief then
            "http://dev-www.libreoffice.org/src/${subDir}${name}"
          else
            "http://dev-www.libreoffice.org/src/${subDir}${md5}-${name}";
  };

  # Can't find Boost inside LO build
  liborcus = stdenv.mkDerivation rec {
    name = "liborcus-0.7.0";
    src = fetchThirdParty (stdenv.lib.findFirst 
      (x: x.name == "${name}.tar.bz2")
      ("Error: update liborcus version inside LO expression")
      (import ./libreoffice-srcs.nix));

    buildInputs = [ boost mdds pkgconfig zlib /*libixion*/ ];

    configureFlags = [ "--with-boost=${boost.dev}" ];
  };

  fetchSrc = {name, sha256}: fetchurl {
    url = "http://download.documentfoundation.org/libreoffice/src/${subdir}/libreoffice-${name}-${version}.tar.xz";
    inherit sha256;
  };

  srcs = {
    third_party = [ (fetchurl rec {
        url = "http://dev-www.libreoffice.org/extern/${md5}-${name}";
        md5 = "185d60944ea767075d27247c3162b3bc";
        name = "unowinreg.dll";
      }) ] ++ (map fetchThirdParty (import ./libreoffice-srcs.nix));

    translations = fetchSrc {
      name = "translations";
      sha256 = "17wfnbwcp7c5cx06c88gmprscfz05qyb5587m72xs6hzr741ygir";
    };

    # TODO: dictionaries

    help = fetchSrc {
      name = "help";
      sha256 = "09im7shbka9dfdh6mq31xq106khlyyw6rr1ij69smlkq0kg463g1";
    };

  };
in
stdenv.mkDerivation rec {
  name = "libreoffice-${version}";

  src = fetchurl {
    url = "http://download.documentfoundation.org/libreoffice/src/${subdir}/libreoffice-${version}.tar.xz";
    sha256 = "0rl9x01ngxwnqwzxkrqy4vks4rb024m75z0w4zidwyp0az0m8qdd";
  };

  # Openoffice will open libcups dynamically, so we link it directly
  # to make its dlopen work.
  NIX_LDFLAGS = "-lcups";

  # If we call 'configure', 'make' will then call configure again without parameters.
  # It's their system.
  configureScript = "./autogen.sh";
  dontUseCmakeConfigure = true;

  postUnpack = ''
    mkdir -v $sourceRoot/src
  '' + (stdenv.lib.concatMapStrings (f: "ln -sv ${f} $sourceRoot/src/${f.outputHash}-${f.name}\nln -sv ${f} $sourceRoot/src/${f.name}\n") srcs.third_party)
  + ''
    ln -sv ${srcs.help} $sourceRoot/src/${srcs.help.name}
    ln -svf ${srcs.translations} $sourceRoot/src/${srcs.translations.name}
  '';

  patchPhase = ''
    find . -type f -print0 | xargs -0 sed -i \<commit_msg>LibreOffice: update 4.4.3.2 -> 4.4.4.3<commit_after># when updating version, wait for the build to fail
# run make without sourcing the environment and let libreoffice
# download all extra files
# then list extra files separated by newline and pipe them to
# generate-libreoffice-srcs.sh and copy output to libreoffice-srcs.nix

{ stdenv, fetchurl, pam, python3, tcsh, libxslt, perl, ArchiveZip
, CompressZlib, zlib, libjpeg, expat, pkgconfigUpstream, freetype, libwpd
, libxml2, db, sablotron, curl, fontconfig, libsndfile, neon
, bison, flex, zip, unzip, gtk, libmspack, getopt, file, cairo, which
, icu, boost, jdk, ant, cups, xorg
, openssl, gperf, cppunit, GConf, ORBit2, poppler
, librsvg, gnome_vfs, mesa
, autoconf, automake, openldap, bash, hunspell, librdf_redland, nss, nspr
, libwpg, dbus_glib, glibc, qt4, kde4, clucene_core, libcdr, lcms, vigra
, unixODBC, mdds, saneBackends, mythes, libexttextcat, libvisio
, fontsConf, pkgconfig, libzip, bluez5, libtool, maven
, libatomic_ops, graphite2, harfbuzz, libodfgen
, librevenge, libe-book, libmwaw, glm, glew, gst_all_1
, langs ? [ "en-US" "en-GB" "ca" "ru" "eo" "fr" "nl" "de" "sl" ]
}:

let
  langsSpaces = stdenv.lib.concatStringsSep " " langs;
  major = "4";
  minor = "4";
  patch = "4";
  tweak = "3";
  subdir = "${major}.${minor}.${patch}";
  version = "${subdir}${if tweak == "" then "" else "."}${tweak}";

  # doesn't exist in srcs
  # 0.8 version is in 0.7.0 tarball
  libixion = stdenv.mkDerivation rec {
     version = "0.7.0";
     name = "libixion-${version}";

     src = fetchurl {
       url = "http://kohei.us/files/ixion/src/${name}.tar.bz2";
       sha256 = "10amvz7fzr1kcy3svfspkdykmspqgpjdmk44cyr406wi7v4lwnf9";
     };

     buildInputs = [ boost mdds pkgconfig ];

     configureFlags = [ "--with-boost=${boost.dev}" ];
  };

  fetchThirdParty = {name, md5, brief, subDir ? ""}: fetchurl {
    inherit name md5;
    url = if brief then
            "http://dev-www.libreoffice.org/src/${subDir}${name}"
          else
            "http://dev-www.libreoffice.org/src/${subDir}${md5}-${name}";
  };

  # Can't find Boost inside LO build
  liborcus = stdenv.mkDerivation rec {
    name = "liborcus-0.7.0";
    src = fetchThirdParty (stdenv.lib.findFirst 
      (x: x.name == "${name}.tar.bz2")
      ("Error: update liborcus version inside LO expression")
      (import ./libreoffice-srcs.nix));

    buildInputs = [ boost mdds pkgconfig zlib /*libixion*/ ];

    configureFlags = [ "--with-boost=${boost.dev}" ];
  };

  fetchSrc = {name, sha256}: fetchurl {
    url = "http://download.documentfoundation.org/libreoffice/src/${subdir}/libreoffice-${name}-${version}.tar.xz";
    inherit sha256;
  };

  srcs = {
    third_party = [ (fetchurl rec {
        url = "http://dev-www.libreoffice.org/extern/${md5}-${name}";
        md5 = "185d60944ea767075d27247c3162b3bc";
        name = "unowinreg.dll";
      }) ] ++ (map fetchThirdParty (import ./libreoffice-srcs.nix));

    translations = fetchSrc {
      name = "translations";
      sha256 = "1zyfpbdsx0kjkabdpkf0lg7hjnvxsf6hj9ljs8v9iqw3x3b7llib";
    };

    # TODO: dictionaries

    help = fetchSrc {
      name = "help";
      sha256 = "1jbbbv63p63mwby52ynz2yk79pb32wsnakhxfhc75ng1br6cpll0";
    };

  };
in
stdenv.mkDerivation rec {
  name = "libreoffice-${version}";

  src = fetchurl {
    url = "http://download.documentfoundation.org/libreoffice/src/${subdir}/libreoffice-${version}.tar.xz";
    sha256 = "0wns7ny19bsl5ar1rq7n4033rfijl2cjn9l8bj1gwhpqlkd8db1i";
  };

  # Openoffice will open libcups dynamically, so we link it directly
  # to make its dlopen work.
  NIX_LDFLAGS = "-lcups";

  # If we call 'configure', 'make' will then call configure again without parameters.
  # It's their system.
  configureScript = "./autogen.sh";
  dontUseCmakeConfigure = true;

  postUnpack = ''
    mkdir -v $sourceRoot/src
  '' + (stdenv.lib.concatMapStrings (f: "ln -sv ${f} $sourceRoot/src/${f.outputHash}-${f.name}\nln -sv ${f} $sourceRoot/src/${f.name}\n") srcs.third_party)
  + ''
    ln -sv ${srcs.help} $sourceRoot/src/${srcs.help.name}
    ln -svf ${srcs.translations} $sourceRoot/src/${srcs.translations.name}
  '';

  patchPhase = ''
    find . -type f -print0 | xargs -0 sed -i \
{'fraction_non_alphanumeric': 0.15308905490335586, 'fraction_numerical': 0.03344005691924582, 'mean_word_length': 3.4530095036958817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 2}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3393919', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3230, 'n_words': 701}