#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import os
import stat

name = os.path.basename(os.path.abspath('.'))

gitignore = """# general stuff
*~
unittest/testresults/*
unittest/output_data/*

# eclipse stuff
.settings/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/
"""

unittest_py = """#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import optparse
import os
import report
import unittest
import tests

parser = optparse.OptionParser("usage: %prog [clean|run|coverage|pdf|copy|release]")
parser.add_option("-e", "--execution-level", dest="execution_level", default="full", help="sets the execution level [full | short | smoke | single]")
(options, args) = parser.parse_args()


if report.TCEL_REVERSE_NAMED.get(tests.execution_level, report.TCEL_FULL) < report.TCEL_REVERSE_NAMED.get(options.execution_level, report.TCEL_FULL):
    options.execution_level = tests.execution_level

unittest.run.unittest(options, args, os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
"""

config_py = """#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import optparse
import os
import %s as lib

release_unittest_version = 'xxx'
lib_path = os.path.realpath(lib.__path__[0])
additional_loggers_to_catch = []

if __name__ == "__main__":
    parser = optparse.OptionParser("usage: %%prog [options] folder_for_document")
    parser.add_option("-p", "--path", dest="libpath", action="store_true", default=False, help="prints the library path")
    (options, args) = parser.parse_args()

    if options.libpath:
        print(lib_path)
""" % name

test_init_py = """#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import %s

from report import TCEL_FULL
from report import TCEL_SHORT
from report import TCEL_SINGLE
from report import TCEL_SMOKE

#from tests import test_

execution_level = 'full'

def testrun(tcl):
    #
    # caching.property_cache_json
    #
    tcl.testCase('description', TCEL_FULL, test_.func, *args, **kwargs)
""" % name

main_makefile = """MODULE_NAME := $(shell basename `pwd`)

view_unittest:
	xdg-open pylibs/$(MODULE_NAME)/_testresults_/unittest.pdf

view_requirements:
	xdg-open pylibs/$(MODULE_NAME)/_requirements_/specification.pdf

view_docs:
	xdg-open pylibs/$(MODULE_NAME)/_docs_/index.html

clean:
	make -kC docs clean; make -kC requirements cleanall; make -kC unittest clean
"""

docs_makefile = """# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS    =
SPHINXBUILD   = sphinx-build
SPHINXPRJ     = state_machine
SOURCEDIR     = .
BUILDDIR      = _build

# Put it first so that "make" without argument is like "make help".
help:
	@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).
%: $(SPHINXPRJ)
	@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

$(SPHINXPRJ): Makefile
	make -C $@/_examples_ all

release: html
	rm -rf $(SPHINXPRJ)/_docs_
	mv $(BUILDDIR)/html $(SPHINXPRJ)/_docs_

view_html: html
	xdg-open $(BUILDDIR)/html/index.html
"""

with open('.gitignore', 'w') as fh:
    fh.write(gitignore)
with open('Makefile', 'w') as fh:
    fh.write(main_makefile)
os.mkdir('docs')
with open('docs/Makefile', 'w') as fh:
    fh.write(docs_makefile)
os.symlink('../pylibs/%s' % name, 'docs/%s' % name)
os.mkdir('pylibs')
os.mkdir('requirements')
os.symlink('../pylibs/reqif/scripts/Makefile', 'requirements/Makefile')
os.symlink('../pylibs/reqif', 'requirements/reqif')
os.mkdir('unittest')
os.symlink('../pylibs/unittest/scripts/Makefile', 'unittest/Makefile')
os.mkdir('unittest/input_data')
os.mkdir('unittest/output_data')
os.mkdir('unittest/src')
with open('unittest/src/config.py', 'w') as fh:
    fh.write(config_py)
with open('unittest/src/unittest.py', 'w') as fh:
    fh.write(unittest_py)
for lib_name in [name, 'report', 'unittest', 'fstools', 'reqif']:
    os.symlink('../../pylibs/%s' % lib_name, 'unittest/src/%s' % lib_name)
os.mkdir('unittest/src/tests')
with open('unittest/src/tests/__init__.py', 'w') as fh:
    fh.write(test_init_py)