#! /usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2006-2010 (ita)

VERSION='0.0.1'
APPNAME='wafdocs'

import os, re, shutil
import Utils, Options

top = '.'
out = 'build'

re_xi = re.compile('''^(include|image)::([^.]*.(txt|png|eps))\[''', re.M)
def ascii_doc_scan(self):
	p = self.inputs[0].parent
	node_lst = [self.inputs[0]]
	seen = []
	depnodes = []
	while node_lst:
		nd = node_lst.pop(0)
		if nd.id in seen: continue
		seen.append(nd.id)

		code = nd.read(self.env)
		for m in re_xi.finditer(code):
			name = m.group(2)
			k = p.find_resource(name)
			if k:
				depnodes.append(k)
				if k.name.rfind('.txt') > 0:
					node_lst.append(k)
	return [depnodes, ()]

def set_options(opt):
	opt.add_option('--exe', action='store_true', default=False, help='Execute the program after it is compiled')

def configure(conf):
	conf.find_program('a2x', var='A2X', mandatory=True)
	conf.find_program('asciidoc', var='ADOC', mandatory=True)


re_size = re.compile('''width=(\d+)\\]''', re.M)
def repl(m):
	if m.group(1):
		return "width=%d]" % (1.7 * int(m.group(1)))
	return ''

def repli(task):
	txt = Utils.readf(task.inputs[0].abspath(task.env))
	txt = txt.replace('.txt[', '_ug.txt[')
	txt = txt.replace('.eps[', '.png[')
	txt = txt.replace('[source,make]', '[source,makefile]')
	txt = txt.replace('[source,shishell]', '')
	txt = re_size.sub(repl, txt)
	f = open(task.outputs[0].abspath(task.env), 'w')
	f.write(txt)
	f.close()

import TaskGen
TaskGen.declare_chain(
    name = 'repl',
    rule = repli,
    ext_in = '.txt',
	ext_out = '_ug.txt',
	reentrant = False
)

def build(bld):
	for k in ['pics']:
		files = os.listdir(k)
		for x in files:
			if x == '.svn':
				continue
			bld.new_task_gen(name='copy', rule='cp ${SRC} ${TGT}', source=k+'/'+x, target=x)

	for k in ['callouts']:
		files = os.listdir(k)
		for x in files:
			if x == '.svn':
				continue
			bld.new_task_gen(name='copy', rule='cp ${SRC} ${SRC[0].parent.abspath(env)}/${SRC[0].name}', source=k+'/'+x)


	bld(source=bld.path.ant_glob('*.txt', src=True, bld=False, dir=False))
	bld(name='copysym', rule='cp ${SRC} symbols.lang', source='symbols.lang.in', before='single')

	if 1 == 1:
		bld(rule='${ADOC} -a icons=true -a iconsdir=. -a toc -n -d book -o ${TGT} -v ${SRC}',
			source='waf_ug.txt', target='single.html', after='copy copysym repl', name='single', scan=ascii_doc_scan)

	if 1 == 1:
		bld(rule='${A2X} -L -a toc --icons-dir=.   -v   --stylesheet=waf.css --icons -D default -d book -f pdf ${SRC}',
		source='waf.txt', target='waf.pdf', after='copysym', name='single', scan=ascii_doc_scan)

	if Options.options.exe:
		def exe(ctx):
			p = Utils.pproc.Popen('konqueror build/default/single.html', shell=True)
			p.wait()
		bld.add_post_fun(exe)


"""
Add the following to asciidoc/dblatex/asciidoc-dblatex.sty

\usepackage{color}
\usepackage{listings}
\definecolor{gray}{gray}{0.5}
\definecolor{plum}{rgb}{0.55078125,0.09765625,0.55859375}
\lstset{commentstyle=\color{plum}}
\lstdefinelanguage{shishell} {
  morekeywords={},
  sensitive=false,
  morecomment=[l]{\$}
}

Set the following values in asciidoc-dblatex.xsl:
  <xsl:param name="doc.publisher.show">0</xsl:param>
  ...
  <xsl:param name="latex.output.revhistory">0</xsl:param>
"""

