#! /usr/bin/python
# -*- mode: python encoding: UTF-8 -*-

'''
run:
$ waf configure
$ waf
$ waf --view

or just:
$ waf --view
'''

VERSION = '0.0.1'
APPNAME = 'the_docs'

top = '.'

import os

PYGMENTS_WARNING = '''
!IMPORTANT!

This Pdflatex document uses Minted for syntax highlighting, and
this requires disabling the shell escaping options for Pdflatex.
A maliciously creafted document might run arbitrary commands
on your computer.

Please set an environment variable to indicate that you understand
the security implications, and only build Semantik documents that
you have created yourself:

export DISABLING_SHELL_ESCAPE_FOR_SEMANTIK=1
'''

def options(opt):
	opt.add_option('--view', action='store_true', default=False, help='View the document')

def configure(conf):
	conf.load('tex')
	cwd = os.getcwd()

	if conf.path.find_node('use_minted.txt'):
		conf.find_program(['pygmentize', 'pygmentex'], var='pygments')
	conf.find_program(['okular', 'kpdf', 'xpdf', 'gnome-open'], var='VIEW', mandatory=False)

	conf.env.LATEX = "; export GS_OPTIONS=\"-sPAPERSIZE=a4\"; %s" % conf.env.LATEX

def build(bld):
	# such options are sometimes necessary
	#bld.env.PDFLATEXFLAGS = ['--shell-escape', '-file-line-error']

	if bld.path.find_node('use_minted.txt'):
		if os.environ.get('DISABLING_SHELL_ESCAPE_FOR_SEMANTIK'):
			bld.env.append_unique('PDFLATEXFLAGS', ['--shell-escape'])
		else:
			bld.fatal(PYGMENTS_WARNING)

	# set prompt=0 to avoid prompting on errors
	dep_files = bld.path.ant_glob(['*.minted', '*.png', '*.pdf'])
	bld(features='tex', type='pdflatex', source='main.tex', prompt=1, deps=dep_files)

	def view(ctx):
		ctx.exec_command(ctx.env.VIEW + ['build/main.pdf'])

	if bld.env.VIEW and bld.options.view:
		bld.add_post_fun(view)

