# Copyright AllSeen Alliance. All rights reserved.
#
#    Permission to use, copy, modify, and/or distribute this software for any
#    purpose with or without fee is hereby granted, provided that the above
#    copyright notice and this permission notice appear in all copies.
#
#    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

import os

Import('ajenv')
Import('core_headers')

docenv = ajenv.Clone()
docenv['ENV']['CPP_DISTDIR'] = str(docenv.Dir('$CPP_DISTDIR'))

installedDocs = None
if docenv['DOCS'] != None:
    docenv.SConscript('snippets/SConscript', variant_dir=docenv['OBJDIR_ALLJOYN_CORE'] + '/docs/snippets', duplicate = 0)

# Build docs
if docenv['DOCS'] == 'html':
    installedDocs = docenv.Doxygen(source='Doxygen_html')

elif docenv['DOCS'] == 'dev':
    installedDocs = docenv.Doxygen(source='Doxygen_dev')

elif docenv['DOCS'] == 'pdf':
    docenv.Doxygen(source='Doxygen_pdf', target=['./latex/refman.tex'])
    # copy the custom style to the latex folder generated by doxygen.
    # this command is not needed if we are using doxygen 1.8 or newer.
    Command('./latex/extra.sty','./extra.sty',Copy("$TARGET", "$SOURCE"))

    # override the default pdflatex command line command.  This is identical to
    # built in command for pdflatex except the '-' character has been added to
    # the start of the command.  Since doxygen is producing the *.tex source
    # files we have no control on the output.  We do not modify the produced
    # *.tex files.  If there is an error the '-' will prevent this command
    # halting the build for something we have no control over.
    docenv['PDFLATEXCOM'] = '-cd ${TARGET.dir} && $PDFLATEX $PDFLATEXFLAGS ${SOURCE.file}'
    # Increase the default LATEXRETRIES from 3 to 5.  This will improve the cross
    # reference links in the produced documentation.
    docenv['LATEXRETRIES'] = 5

    # since creation of the pdf produces many files other than the final pdf
    # we build the pdf in the docs folder then copy the final file into the
    # distribution folder. This way we don't get meaningless output in the
    # distribution folder.
    output = docenv.PDF('./pdf/alljoyn_api_manual.pdf', './latex/refman.tex')
    installedDocs = docenv.Install('$CPP_DISTDIR/docs/pdf', output)
    docenv.Clean('Doxygen_pdf', Dir('pdf'))
    docenv.Depends(installedDocs, output)

elif docenv['DOCS'] == 'sandcastle':
    if not os.environ.has_key('SHFBROOT'):
        print 'Missing required environment build variable SHFBROOT'
        if not GetOption('help'):
            Exit(1)

    chm_env = docenv.Clone();

    # import the envrionment variable from the system
    shfbroot = chm_env.Dir(os.environ['SHFBROOT'])

if installedDocs:
    docenv.Depends(installedDocs, core_headers);

# Build Alias to make cleaning, building and rebuilding the documentation when
# working only on the documentation simpler. This can be run by using
# `scons core_docs`
docenv.Alias('core_docs', installedDocs)
