[buildout]

extends = 
  ../fontconfig/buildout.cfg
  ../tar/buildout.cfg
  ../xorg/buildout.cfg

parts =
  tar
  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

# script to install.
script =
  location = %(location)r
  self.failIfPathExists(location)
  import sys
  ARCH_DIR_MAP = { 'x86': 'x86', 'x86-64': 'x86_64' }
  WK_SUFIX_MAP = { 'x86': 'i386', 'x86-64': 'amd64' }
  platform = guessPlatform()
  url = self.options['url_' + platform]
  md5sum = self.options['md5sum_' + platform]
  path = self.download(url, md5sum)
  import tempfile
  extract_dir = tempfile.mkdtemp(self.name)
  self.cleanup_dir_list.append(extract_dir)
  self.logger.debug('Created working directory ' + repr(extract_dir))
  call(["${tar:location}/bin/tar", "xJf", path, "-C", extract_dir])
  shutil.move(os.path.join(extract_dir, "wkhtmltox"), location)
  wrapper_location = os.path.join("%(location)s", "wkhtmltopdf")
  wrapper = open(wrapper_location, 'w')
  wrapper.write("""#!${dash:location}/bin/dash
  export LD_LIBRARY_PATH=%(location)s/lib/:${libXrender:location}/lib/:${fontconfig:location}/lib/:${libX11:location}/lib/:${libXext:location}/lib/
  export PATH=${fontconfig:location}/bin:$PATH
  exec %(location)s/bin/wkhtmltopdf "$@"
  """)
  wrapper.close()
  os.chmod(wrapper_location, 0755)
