# vim: set filetype=python :
# arch-tag: general-purpose SCons build file for sgml-common

from glob import glob
import os, re

############################################################
# Setup
############################################################

SConsignFile('.sconsign-master')
#Import('env')
d = env.Dictionary()
if not 'JADE' in d:
    d['JADE'] = 'jade'
if not 'INDEXNODE' in d:
    d['INDEXNODE'] = 'ch.index'
if not 'GTKIMGPATH' in d:
    d['GTKIMGPATH'] = '/usr/share/gtk-doc/data'
if not 'PS2EPSI' in d:
    d['PS2EPSI'] = '../sgml-common/ps2epsi'

def removeindex(l):
    while 'index/index.sgml' in l:
        l.remove('index/index.sgml')

master = d['MASTERBASE'] 
mastersgml = master + '.sgml'
sources = [mastersgml] + glob('*/*.sgml') + glob('*/*/*.sgml')
removeindex(sources)
db2htmlcmd = 'docbook-2-html -D $JADE ${HTMLARGS}  ${SOURCE}'
db2htmlindexcmd = 'docbook-2-html -D $JADE -O -V -O html-index ${HTMLARGS} ${SOURCE}'

##################################################
# SCANNERS
##################################################
def recursescan(scanner, node, env):
    result = scanner(node, env) 
    retval = []
    for item in result:
        retval.append(item)
        retval.extend(recursescan(scanner, item, env))
    return retval

SGML_includes_re = re.compile(r'<!ENTITY[^>]+SYSTEM[^>]+"(.+)"', re.M)
def SGML_includes_scan(node, env, path):
    ret = SGML_includes_re.findall(node.get_contents())
    removeindex(ret)
    return ret

SGML_includes_scanner = Scanner(name = 'SGML_includes',
    function = SGML_includes_scan, recursive = 1, skeys = ['.sgml', '.ent'])

SGML_image_pdf_re = re.compile(r'<(graphic|imagedata).+?fileref="([^"]+\.pdf)"', re.S)
SGML_image_png_re = re.compile(r'<(graphic|imagedata).+?fileref="([^"]+\.png)"', re.S)
def SGML_image_scanner(node, env, path, arg):
    root, ext = os.path.splitext(str(node))
    contents = node.get_contents()
    return SGML_includes_scan(node, env, path) + \
            [os.getcwd() + '/' + x[1] for x in arg.findall(contents)]

SGML_pdf_scanner = Scanner(name = 'SGML_pdf',
         function = SGML_image_scanner, argument = SGML_image_pdf_re,
         recursive = 1)
SGML_png_scanner = Scanner(name = 'SGML_png',
        function = SGML_image_scanner, argument = SGML_image_png_re,
        recursive = 1)

##################################################
# BUILDERS
##################################################

#### PLAIN TEXT
Btxt = Builder(action="docbook2txt $SOURCE", src_suffix='.sgml', suffix='.txt')

#### PDF / POSTSCRIPT
Bpdf = Builder(action="docbook-2-pdf -D ${JADE} -q -O -V -O paper-size=Letter ${PDFARGS} ${SOURCE}",
        src_suffix='.sgml', suffix='.pdf')
Bpdf2ps = Builder(action="pdftops ${SOURCE}", src_suffix='.pdf', suffix='.ps')

#### MAN PAGES
# FIXME: test this
Bman = Builder(action="docbook2man $SOURCE", src_suffix='.sgml', suffix='.1')

#### HTML
Bhtml = Builder(action = [ \
        'if test -d ${TARGET.dir} ; then rm -r ${TARGET.dir} ; fi',
        'mkdir ${TARGET.dir}',
        db2htmlcmd,
        'mv ${MASTERBASE}-html/* ${TARGET.dir}/',
        'rmdir ${MASTERBASE}-html',
        'ln -s ${TOPNODE}.html ${TARGET.dir}/index.html',
        'cp ${GTKIMGPATH}/*.png ${TARGET.dir}/'])

#### PNG
Bepip2png = Builder(action = 'gs -q -dTextAlphaBits=4 -dGraphicsAlphaBits=4 ' +\
        '-r90 -dBATCH -dNOPAUSE -dSAFER -sOutputFile=$TARGET ' + \
        '-sDEVICE=png16m $SOURCE -c showpage', suffix='.png', src_suffix='.pngepi')

#### EPI from PS
def getpagenumfromname(target, source, env, for_signature):
    return re.search('^.*_(\d+)\.(png){0,1}epi$', str(target[0])).group(1)
d['GETPAGE'] = getpagenumfromname

Aps2epi = Action(['psselect -q ${GETPAGE} $SOURCE temp.ps',
        'psresize -w ${WIDTH} -h ${HEIGHT} temp.ps temp2.ps',
        '$PS2EPSI temp2.ps $TARGET',
        'rm temp.ps temp2.ps'])
Bps2epi = Builder(action=Aps2epi, src_suffix='.ps', suffix='.epi')
Bps2epip = Builder(action=Aps2epi, src_suffix='.ps', suffix='.pngepi')
Bepi2pdf = Builder(action="epstopdf -o=${TARGET} ${SOURCE}", suffix='.pdf',
        src_suffix='.epi')

#### PLUCKER
Bplucker = Builder(action = 'plucker-build --bpp=4 --compression=zlib ' + \
        '--doc-name="${MASTERBASE}" -H file:${SOURCE.abspath} -M 5 ' + \
        '--maxheight=320 --maxwidth=310 --staybelow=file:`pwd`/${SOURCE.dir} ' + \
        '--title="${MASTERBASE}" -p . -f ${MASTERBASE}')

##################################################
# General setup
##################################################

env.Append(BUILDERS = {'Text': Btxt, 'PDF2PS': Bpdf2ps, 'PDF': Bpdf, 'HTML': Bhtml,
        'Plucker': Bplucker, 'PS2EPI': Bps2epi, 'PS2EPIP': Bps2epip,
        'EPI2PDF': Bepi2pdf, 'EPIP2PNG': Bepip2png, 'MAN': Bman})

#### INDEX GENERATION
if 'DOINDEX' in d:
    Bindex = Builder(action = ['if test -d ${TARGET.dir}  ; then rm -r ${TARGET.dir} ; fi',
        "mkdir ${TARGET.dir}",
        "collateindex.pl -i $INDEXNODE -N -o $TARGET",
        db2htmlindexcmd,
        "mv ${MASTERBASE}-html/HTML.index ${TARGET.dir}/",
        "rm -r ${MASTERBASE}-html",
        "collateindex.pl -i $INDEXNODE -g -o $TARGET ${TARGET.dir}/HTML.index"])
    env['BUILDERS']['Index'] = Bindex
    index = env.Index('index/index.sgml', mastersgml)
    env.Depends(index, sources)
    env.Clean(index, 'index')
    deps = sources + [index]
else:
    deps = sources

##################################################
# BUILD RULES
###################################################
# Text
text = env.Text(mastersgml)
env.Depends(text, deps)
env.Alias('text', text)

# PDF
pdfsgml = File(mastersgml)
pdf = env.PDF(pdfsgml)
figsindoc = [x for x in recursescan(SGML_pdf_scanner, pdfsgml, env) if str(x).endswith('.pdf')]
epipdf = []
for file in figsindoc:
    pdfname = re.sub('_\d+\.pdf$', '.pdf', str(file))
    if pdfname == str(file):
        # This is not a filename that fits our pattern; add unmodified.
        epipdf.append(file)
        continue
    psfile = env.PDF2PS(source = pdfname)
    epifile = env.PS2EPI(str(file).replace(".pdf", ".epi"), psfile,
            WIDTH='6.375in', HEIGHT='8.25in')
    epipdf.append(env.EPI2PDF(source = epifile))

env.Depends(pdf, deps)
env.Depends(pdf, epipdf)
env.Alias('pdf', pdf)
env.Clean(pdf, ['jadetex.cfg', '${MASTERBASE}.aux', '${MASTERBASE}.dvi',
        '${MASTERBASE}.jtex', '${MASTERBASE}.log', '${MASTERBASE}.out',
        'jade-out.fot'])

# PS
ps = env.PDF2PS(source = pdf)
env.Alias('ps', ps)

# HTML
htmlsgml = File(mastersgml)
buildhtml = env.HTML('html/index.html', htmlsgml)
figsindoc = [x for x in recursescan(SGML_png_scanner, htmlsgml, env) if str(x).endswith('.png')]
epipng = []
for file in figsindoc:
    pdfname = re.sub('_\d+\.png$', '.pdf', str(file))
    if pdfname == str(file):
        # This is not a filename that fits our pattern; add unmodified. 
        epipng.append(file)
        continue
    psfile = env.PDF2PS(source = pdfname)
    epifile = env.PS2EPIP(str(file).replace(".png", ".pngepi"), psfile,
            WIDTH='8.5in', HEIGHT='11in')
    epipng.append(env.EPIP2PNG(source = epifile))

env.Depends(buildhtml, epipng)
env.Depends(buildhtml, deps)
pnginstalls = env.InstallAs(['html/' + str(x) for x in epipng], epipng)
env.Depends(pnginstalls, buildhtml)
html = env.Alias('html', buildhtml)
html = env.Alias('html', pnginstalls)
env.Clean(buildhtml, 'html')

# Plucker
plucker = env.Plucker(master + '.pdb', 'html/index.html')
env.Alias('plucker', plucker)

env.Default(html)
