from __future__ import absolute_import
from __future__ import print_function
import os
# pylint: disable=no-name-in-module, import-error
import subprocess
from distutils.dir_util import copy_tree, remove_tree
from distutils.file_util import copy_file, move_file
from distutils.core import run_setup
from distutils.archive_util import make_archive


# Run clean
import clean #pylint: disable=unused-import

print("Starting dist.\n")

VERSION = __import__('dxleposervice').get_version()
RELEASE_NAME = "dxleposervice-python-dist-" + str(VERSION)
CONFIG_RELEASE_NAME = "dxleposervice-python-dist-config-" + str(VERSION)

DIST_PY_FILE_LOCATION = os.path.dirname(os.path.realpath(__file__))
DIST_DIRECTORY = os.path.join(DIST_PY_FILE_LOCATION, "dist")
DIST_CONFIG_DIRECTORY = os.path.join(DIST_DIRECTORY, "config")
DIST_DOCTMP_DIR = os.path.join(DIST_DIRECTORY, "doctmp")
SETUP_PY = os.path.join(DIST_PY_FILE_LOCATION, "setup.py")
DIST_LIB_DIRECTORY = os.path.join(DIST_DIRECTORY, "lib")
DIST_RELEASE_DIR = os.path.join(DIST_DIRECTORY, RELEASE_NAME)
CONFIG_RELEASE_DIR = os.path.join(DIST_DIRECTORY, CONFIG_RELEASE_NAME)
SAMPLE_RELEASE_DIR = os.path.join(DIST_DIRECTORY, "sample")

# Remove the dist directory if it exists
if os.path.exists(DIST_DIRECTORY):
    print("\nRemoving dist directory: " + DIST_DIRECTORY + "\n")
    remove_tree(DIST_DIRECTORY, verbose=1)

print("\nMaking dist directory: " + DIST_DIRECTORY + "\n")
os.makedirs(DIST_DIRECTORY)

print("\nCalling sphinx-apidoc\n")
subprocess.check_call(["sphinx-apidoc",
                       "--force",
                       "--separate",
                       "--no-toc",
                       "--output-dir=" + DIST_DOCTMP_DIR,
                       os.path.join(DIST_PY_FILE_LOCATION, "dxleposervice")])

print("\nCopying conf.py, docutils.conf, and sdk directory\n")
copy_file(os.path.join(DIST_PY_FILE_LOCATION, "doc", "conf.py"),
          os.path.join(DIST_DOCTMP_DIR, "conf.py"))
copy_file(os.path.join(DIST_PY_FILE_LOCATION, "doc", "docutils.conf"),
          os.path.join(DIST_DOCTMP_DIR, "docutils.conf"))
copy_tree(os.path.join(DIST_PY_FILE_LOCATION, "doc", "sdk"), DIST_DOCTMP_DIR)

print("\nCalling sphinx-build\n")
subprocess.check_call(["sphinx-build", "-b", "html", DIST_DOCTMP_DIR,
                       os.path.join(DIST_DIRECTORY, "doc")])

# Delete .doctrees
remove_tree(os.path.join(os.path.join(DIST_DIRECTORY, "doc"), ".doctrees"), verbose=1)
# Delete .buildinfo
os.remove(os.path.join(os.path.join(DIST_DIRECTORY, "doc"), ".buildinfo"))

print("\nMoving README.html\n")
move_file(os.path.join(DIST_DOCTMP_DIR, "README.html"), DIST_DIRECTORY)

print("\nDeleting doctmp directory\n")
remove_tree(DIST_DOCTMP_DIR)

print("\nRunning setup.py sdist\n")
run_setup(SETUP_PY,
          ["sdist",
           "--format",
           "zip",
           "--dist-dir",
           DIST_LIB_DIRECTORY])

print("\nRunning setup.py bdist_wheel\n")
run_setup(SETUP_PY,
          ["bdist_wheel",
           "--dist-dir",
           DIST_LIB_DIRECTORY,
           "--universal"])

print("\nCopying config into dist directory\n")
copy_tree(os.path.join(DIST_PY_FILE_LOCATION, "config"), DIST_CONFIG_DIRECTORY)

print("\nCopying sample into dist directory\n")
copy_tree(os.path.join(DIST_PY_FILE_LOCATION, "sample"), SAMPLE_RELEASE_DIR)

print("\nCopying dist to " + DIST_RELEASE_DIR + "\n")
copy_tree(DIST_DIRECTORY, DIST_RELEASE_DIR)

print("\nRemoving build directory\n")
remove_tree(os.path.join(DIST_PY_FILE_LOCATION, "build"))

print("\nRemoving dxleposervice.egg-info\n")
remove_tree(os.path.join(DIST_PY_FILE_LOCATION, "dxleposervice.egg-info"))

print("\nMaking dist zip\n")
make_archive(DIST_RELEASE_DIR, "zip", DIST_DIRECTORY, RELEASE_NAME)

print("\nMaking dist config zip\n")
make_archive(CONFIG_RELEASE_DIR, "zip", os.path.join(DIST_RELEASE_DIR, "config"))

print("\nRemoving " + DIST_RELEASE_DIR + "\n")
remove_tree(DIST_RELEASE_DIR)

@@ -340,16 +340,69 @@
 archive%0A
+from tempfile import mkstemp%0Afrom shutil import move%0A
 %0A%0A# Run 
@@ -452,16 +452,407 @@
 import%0A%0A
+def replace(file_path, pattern, subst):%0A    # Create temp file%0A    fh, abs_path = mkstemp()%0A    with open(abs_path, 'w') as new_file:%0A        with open(file_path) as old_file:%0A            for line in old_file:%0A                new_file.write(line.replace(pattern, subst))%0A    os.close(fh)%0A    # Remove original file%0A    os.remove(file_path)%0A    # Move new file%0A    move(abs_path, file_path)%0A%0A
 print(%22S
@@ -2759,16 +2759,139 @@
 oc%22)%5D)%0A%0A
+replace(os.path.join(DIST_DIRECTORY, %22doc%22, %22_static%22, %22classic.css%22),%0A        %22text-align: justify%22, %22text-align: none%22)%0A%0A
 # Delete

Disable justification in Sphinx doc generation