import os
import sys
import shutil
from glob import glob

# --------------------------------------------------------------------------
DOC_DIR = 'hr-html'
PYWRAPS_FN = 'idaapi.py'

# --------------------------------------------------------------------------
def add_footer(lines):
    S1 = 'Generated by Epydoc'
    S2 = '</table>'
    p =  lines.find(S1)

    if p == -1:
        return None

    p = lines.find(S2, p)
    if p == -1:
        return None

    p += len(S2)

    return lines[0:p] + '\n<!--#include virtual="/footer.shtml" -->' + lines[p:]

# --------------------------------------------------------------------------
def define_idaapi_resolver():
    """
    Whenever a module named \"idaapi_<something>\" is
    spotted, turn it into \"idaapi\".
    """
    import epydoc.apidoc
    dn = epydoc.apidoc.DottedName.__init__
    def resolver(piece):
        if piece is not None and isinstance(piece, basestring) and piece.startswith("idaapi_"):
            return "idaapi"
        else:
            return piece
    def wrapper(self, *pieces, **options):
        return dn(self, *map(resolver, pieces), **options);
    epydoc.apidoc.DottedName.__init__ = wrapper

# --------------------------------------------------------------------------
def gen_docs():
    


# --------------------------------------------------------------------------
def patch_docs():
    shutil.copy('../../hrdoc.css', 'epydoc.css')
    os.system('chmod +w epydoc.css')

    for fn in glob('*.html'):
        f = open(fn, 'r')
        lines = f.read()
        f.close()

        r = add_footer(lines)
        if not r:
            print "-",
            continue

        f = open(fn, 'w')
        f.write(r)
        f.close()
        print "+",

    print "\nDocumentation patched!"

# --------------------------------------------------------------------------
def main():
    # Save old directory and adjust import path
    curdir = os.getcwd() + os.sep
    sys.path.append(curdir + 'python')
    sys.path.append(curdir + 'tools')
    sys.path.append(curdir + 'docs')

    old_dir = os.getcwd()

    try:
        print "Generating documentation....."

        os.chdir('docs')
        gen_docs()

        os.chdir(DOC_DIR)
        patch_docs()

        print "Documentation generated!"

    finally:
        os.chdir(old_dir)

# --------------------------------------------------------------------------
if __name__ == '__main__':
    main()
    Exit(0)

import epydoc.cli
    import swigdocs

    define_idaapi_resolver()

    swigdocs.gen_docs(outfn = 'pywraps.py')
    # append obj/x86_win_vc_32/idaapi.py to it
#    os.system(r'copy /b idaapi.py+..\obj\x86_win_vc_32\idaapi.py idaapi.py')

    # delete all output files
    for fn in glob('hr-html/*'):
      os.unlink(fn)

    epydoc.cli.optparse.sys.argv = [ 'epydoc',
                                     '--config', '../hrdoc.cfg',
                                     '--simple-term'
                                   ]

    # Generate the documentation
    epydoc.cli.cli()