#!/usr/bin/env python

import os, re, subprocess
from waflib import Options, TaskGen, Utils

top = out = '.'

def options(opt):
	opt.load('rst')
	opt.load('tex')

def configure(conf):
	conf.load('rst')
	conf.load('tex')

def build(bld):
	bld(
	 features='rst',
	 target='test0.html',
	 source='test0.rst',
	)

	bld(
	 target='generated.rst',
	 rule=lambda tsk: tsk.outputs[0].write("Generated contents in %s" % tsk.outputs[0].name),
	)

	bld(
	 target='generated.csv',
	 rule=lambda tsk: tsk.outputs[0].write("a,b,c\n1,2,%s" % tsk.outputs[0].name),
	)

	bld(
	 target='generated.html',
	 rule=lambda tsk: tsk.outputs[0].write("<p>Generated HTML data</p>"),
	)

	for x in bld.path.ant_glob("**/*.svg", remove=False):
		bld(
		 rule='inkscape --export-area-drawing --export-png=${TGT[0].bldpath()} ${SRC[0].bldpath()}',
		 source=x,
		 target=x.change_ext('.png'),
		)
		bld(
		 rule='inkscape --export-area-drawing --export-pdf=${TGT[0].bldpath()} ${SRC[0].bldpath()}',
		 source=x,
		 target=x.change_ext('.pdf'),
		)

	bld.add_group()

	bld(
	 features='rst',
	 target='test1.html',
	 source='test1.rst',
	)

	if bld.env.RST2PDF:
		bld(
			features='rst',
			target='test1.pdf',
			source='test1.rst',
		)

	bld(
	 target='generated.tex',
	 rule=lambda tsk: tsk.outputs[0].write("Generated contents in %s" % tsk.outputs[0].name)),
	)

	bld(
	 features='rst',
	 type='rst2latex',
	 target='test2.tex',
	 source='test2.rst',
	)

	bld(
	 features='tex',
	 source='test2.tex',
	)

