#!/usr/bin/env python

# ==================================================
# open
# --------------------------------------------------

from __future__ import print_function
from __future__ import absolute_import

import os
import shutil
import sys
import tct

from tct import deepget

params = tct.readjson(sys.argv[1])
binabspath = sys.argv[2]
facts = tct.readjson(params['factsfile'])
milestones = tct.readjson(params['milestonesfile'])
resultfile = params['resultfile']
result = tct.readjson(resultfile)
loglist = result['loglist'] = result.get('loglist', [])
toolname = params['toolname']
toolname_pure = params['toolname_pure']
workdir = params['workdir']
exitcode = CONTINUE = 0


# ==================================================
# Make a copy of milestones for later inspection?
# --------------------------------------------------

if 0 or milestones.get('debug_always_make_milestones_snapshot'):
    tct.make_snapshot_of_milestones(params['milestonesfile'], sys.argv[1])


# ==================================================
# Helper functions
# --------------------------------------------------

def lookup(D, *keys, **kwdargs):
    result = deepget(D, *keys, **kwdargs)
    loglist.append((keys, result))
    return result


# ==================================================
# define
# --------------------------------------------------

publish_dir = None
publish_dir_buildinfo = None
publish_html_done = None
publish_language_dir = None
publish_project_dir = None
publish_project_parent_dir = None
publish_removed_old = None
xeq_name_cnt = 0


# ==================================================
# Check params
# --------------------------------------------------

if exitcode == CONTINUE:
    loglist.append('CHECK PARAMS')

    publish_dir_planned = lookup(milestones, 'publish_dir_planned',
                                 default=None)
    publish_language_dir_planned = lookup(milestones,
                                          'publish_language_dir_planned',
                                          default=None)
    publish_project_dir_planned = lookup(milestones,
                                         'publish_project_dir_planned',
                                         default=None)
    publish_project_parent_dir_planned = lookup(milestones,
                                                'publish_project_parent_dir_planned',
                                                default=None)
    TheProjectResult = lookup(milestones, 'TheProjectResult', default=None)
    TheProjectResultVersion = lookup(milestones, 'TheProjectResultVersion',
                                     default=None)

    if not (
            publish_dir_planned and
            publish_language_dir_planned and
            publish_project_dir_planned and
            publish_project_parent_dir_planned and
            TheProjectResult and
            TheProjectResultVersion):
        exitcode = 22

if exitcode == CONTINUE:
    loglist.append('PARAMS are ok')
else:
    loglist.append('Bad PARAMS or nothing to do')


# ==================================================
# work
# --------------------------------------------------

if exitcode == CONTINUE:

    if not os.path.exists(publish_project_parent_dir_planned):
        os.makedirs(publish_project_parent_dir_planned)
    publish_project_parent_dir = publish_project_parent_dir_planned

    if not os.path.exists(publish_project_dir_planned):
        os.mkdir(publish_project_dir_planned)
    publish_project_dir = publish_project_dir_planned

    if os.path.isdir(publish_dir_planned):
        shutil.rmtree(publish_dir_planned)
        publish_removed_old = 1

    # should not happen
    if os.path.exists(publish_dir_planned):
        os.remove(publish_dir_planned)

    if os.path.exists(publish_dir_planned):
        loglist.append(('cannot remove `publish_dir_planned`',
                        publish_dir_planned))
        publish_removed_old = 0
        exitcode = 22

if exitcode == CONTINUE:
    # move our new build in place
    # shutil.move(TheProjectResultVersion, publish_dir_planned)
    shutil.copytree(TheProjectResultVersion, publish_dir_planned)
    publish_dir = publish_dir_planned
    publish_project_dir = publish_project_dir_planned
    publish_language_dir = publish_project_dir_planned
    if not os.path.isdir(publish_dir):
        loglist.append(('cannot move or copy to `publish_dir`', publish_dir))
        exitcode = 22

if exitcode == CONTINUE:
    publish_html_done = 1
    publish_dir_buildinfo_planned = lookup(
        milestones, 'publish_dir_buildinfo_planned', default=None)
    if (publish_dir_buildinfo_planned
            and os.path.isdir(publish_dir_buildinfo_planned)):
        publish_dir_buildinfo = publish_dir_buildinfo_planned


# ==================================================
# Set MILESTONE
# --------------------------------------------------

D = {}

if publish_html_done:
    D['publish_html_done'] = publish_html_done

if publish_removed_old:
    D['publish_removed_old'] = publish_removed_old

if publish_project_dir:
    D['publish_project_dir'] = publish_project_dir

if publish_project_parent_dir:
    D['publish_project_parent_dir'] = publish_project_parent_dir

if publish_dir:
    D['publish_dir'] = publish_dir

if publish_language_dir:
    D['publish_language_dir'] = publish_language_dir

if publish_project_dir:
    D['publish_project_dir'] = publish_project_dir

if publish_dir_buildinfo:
    D['publish_dir_buildinfo'] = publish_dir_buildinfo

if D:
    result['MILESTONES'].append(D)

# ==================================================
# save result
# --------------------------------------------------

tct.writejson(result, resultfile)


# ==================================================
# Return with proper exitcode
# --------------------------------------------------

sys.exit(exitcode)

@@ -1744,16 +1744,478 @@
 RAMS')%0A%0A
+    build_html = lookup(milestones, 'build_html', default=None)%0A    TheProjectResult = lookup(milestones, 'TheProjectResult', default=None)%0A    TheProjectResultVersion = lookup(milestones, 'TheProjectResultVersion', default=None)%0A%0A    if not(1%0A            and build_html%0A            and TheProjectResult%0A            and TheProjectResultVersion%0A    ):%0A        # stop this folder%0A        exitcode = 22%0A%0Aif exitcode == CONTINUE:%0A    loglist.append('CHECK PARAMS')%0A%0A
     publ
@@ -3354,18 +3354,67 @@
 tVersion
-):
+%0A    ):%0A        # stop processing of rest of folder
 %0A       

Extend parameter tests run_03-Publish-html.py