#!/usr/bin/env python
import commands, os, sys, commands, re

# recommendation: remove installation and reinstall (to test setup.py)

# in addition to the tests below (testdoc, tutorial, manual) one
# should try the easyviz documentation as that docment has a lot
# of figures and cross references (is a nice example of doconce too)

thisdir = os.getcwd()
logfilename = os.path.join(thisdir, 'test.v')
log = open(logfilename, 'w')
log.close()  # just touch the file
outputfilename = os.path.join(thisdir, 'test.output')
f = open(outputfilename, 'w')  # touch
f.close()

def add(filename, logfile):
    print '...adding file', filename
    logfile.write("""
************** File: %s *****************
""" % filename)
    if not os.path.isfile(filename):
        logfile.write('NOT FOUND!')
    else:
        f = open(filename, 'r')
        fstr = f.read()
        f.close()
        logfile.write(fstr)

def clean_and_make(append=True):
    print '\n\nCleaning....................................'
    failure = os.system('sh -x clean.sh > /dev/null')
    if failure:
        raise OSError('Could not run clean.sh successfully')
    print '\n\nRunning make.sh...............................\nin', os.getcwd()
    failure, output = commands.getstatusoutput('sh -x make.sh')
    f = open(outputfilename, 'a');  f.write(output);  f.close()
    if failure:
        where = os.getcwd()
        raise OSError('Could not run %s/make.sh successfully!\nRerun manually (go to %s and run ./make.sh' % (where, where))

# test multiple authors, figure, movie, math, encodings, etc:
print '...running ./make.sh in test'  # works only under Unix...
clean_and_make(append=False)

log = open(logfilename, 'a')
files = '.do.txt', '.html', '.p.tex', '.tex', '.rst', '.sphinx.rst', '.gwiki', '.mwiki', '.cwiki', '.st', '.epytext', '.txt', '.mkd', '.tmp'
files = ['testdoc' + ext for ext in files] + ['tmp_encodings.txt', 'testdoc_pnd_l2h.html', 'testdoc_pnd_d2h.html']
for f in files:
    add(f, log)
add('make.sh', log)
log.close()

# Find date
f = open('testdoc.p.tex', 'r')
text = f.read()
f.close()
m = re.search(r'date\{(.+?)\}', text)
if m:
    date = m.group(1)
else:
    print 'Error: no date in testdoc.p.tex'
    sys.exit(1)

tutdir = os.path.join(os.pardir, 'doc', 'tutorial')
print 'cd', tutdir
os.chdir(tutdir)

print '...running ./make.sh in doc/tutorial'  # works only under Unix...
clean_and_make()

log = open(logfilename, 'a')
add('make.sh', log)
os.chdir('demo')
# concentrate on files generated by doconce (not rst2*.py):
files = '.do.txt', '.html', '.p.tex', '.rst', '.sphinx.rst', '.gwiki', '.mwiki', '.cwiki', '.st', '.epytext', '.txt', '.mkd'
files = ['tutorial' + ext for ext in files]
for f in files:
    add(f, log)
add(os.path.join(os.pardir, '_what_is.do.txt'), log)
add(os.path.join(os.pardir, '_doconce2anything.do.txt'), log)

# test DocWriter:
os.chdir(thisdir)
failure, output = commands.getstatusoutput('python ../lib/doconce/DocWriter.py')
files = 'tmp_Doconce.do.txt', 'tmp_DocWriter.do.txt', 'tmp_DocWriter.html', \
        'tmp_HTML.html'
for f in files:
    add(f, log)

log.close()

# test manual:
mandir = os.path.join(os.pardir, 'doc', 'manual')
print 'cd', mandir
os.chdir(mandir)

clean_and_make()

log = open(logfilename, 'a')
add('make.sh', log)
files = '.do.txt', '.html', '.p.tex', '.rst', '.sphinx.rst', '.gwiki', '.mwiki', '.cwiki', '.st', '.epytext', '.txt', '.mkd'
files = ['manual' + ext for ext in files]
for f in files:
    add(f, log)
add('_install.do.txt', log)

os.chdir(thisdir)
add(outputfilename, log)
log.close()

# test quickref:
quickdir = os.path.join(os.pardir, 'doc', 'quickref')
print 'cd', quickdir
os.chdir(quickdir)

clean_and_make()

log = open(logfilename, 'a')
add('make.sh', log)
files = '.do.txt', '.html', '.p.tex', '.rst', '.sphinx.rst', '.gwiki', '.mwiki', '.cwiki', '.st', '.epytext', '.txt', '.mkd'
files = ['quickref' + ext for ext in files]
for f in files:
    add(f, log)

os.chdir(thisdir)
add(outputfilename, log)
log.close()

# Subst date by some dummy date so we don't need to have differences
# because of the date
log = open(logfilename, 'r')
text = log.read()
log.close()
text = text.replace(date, 'Jan 32, 2100')
log = open(logfilename, 'w')
log.write(text)
log.close()

