[buildout]

extends = 
  ../freetype/buildout.cfg
  ../fontconfig/buildout.cfg
  ../libpng/buildout.cfg
  ../tar/buildout.cfg
  ../xorg/buildout.cfg
  ../xz-utils/buildout.cfg
  ../zlib/buildout.cfg

parts =
  wkhtmltopdf

[wkhtmltopdf]
recipe = slapos.recipe.build

# here, two %s are used, first one is for directory name (eg. x86_64), and second one is for filename (eg. x86-64).
url_x86-64 = http://download.gna.org/wkhtmltopdf/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
url_x86 = http://download.gna.org/wkhtmltopdf/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-i386.tar.xz

# supported architectures md5sums
md5sum_x86 = ce1a2c0b2cf786ccc5d5828c42c99ddd
md5sum_x86-64 = 96b7306cebb9e65355f69f7ab63df68b

install =
  import os,shutil, sys, tempfile
  platform = guessPlatform()
  url = options['url_' + platform]
  md5sum = options['md5sum_' + platform]
  path = self.download(url, md5sum)
  extract_dir = tempfile.mkdtemp(self.name)
  self.cleanup_dir_list.append(extract_dir)
  self.logger.debug('Created working directory %s', extract_dir)
  env = os.environ.copy()
  env["PATH"] = "${tar:location}/bin:${xz-utils:location}/bin" + (":" + env["PATH"] if env.get("PATH") else "")
  env["LD_LIBRARY_PATH"] = "${xz-utils:location}/lib" + (":" + env["LD_LIBRARY_PATH"] if env.get("LD_LIBRARY_PATH") else "")
  call(["tar", "xJf", path, "-C", extract_dir], env=env)
  shutil.move(os.path.join(extract_dir, "wkhtmltox"), location)
  with open(os.path.join(location, "wkhtmltopdf"), 'w') as wrapper:
    wrapper.write("""#!/bin/sh
  export LD_LIBRARY_PATH=%(location)s/lib:${fontconfig:location}/lib:${freetype:location}/lib:${libX11:location}/lib:${libXext:location}/lib:${libXrender:location}/lib:${libpng12:location}/lib:${zlib:location}/lib
  export PATH=${fontconfig:location}/bin:$PATH
  exec %(location)s/bin/wkhtmltopdf "$@"
  """ % options)
    os.fchmod(wrapper.fileno(), 0o755)
