import os
import subprocess

# hardcoded paths
HUNTER_DIR='..'
WIKI_DIR=os.path.join(HUNTER_DIR, '..', 'hunter.wiki')
PACKAGES_DIR=os.path.join(HUNTER_DIR, 'cmake/projects')

# get all wiki entries
wiki_filenames = [x for x in os.listdir(WIKI_DIR) if x.startswith('pkg.') and x.endswith('.md')]
wiki_entries = [x[4:-3] for x in wiki_filenames]

# get all hunter package entries   
pkg_entries = [x for x in os.listdir(PACKAGES_DIR) if os.path.isdir(os.path.join(PACKAGES_DIR, x))]
pkg_entries_lower = [x.lower() for x in pkg_entries]

# packages both in hunter and wiki
pkg_match = [x for x in pkg_entries if x.lower() in wiki_entries]
# packages only in hunter
pkg_only_hunter = [x for x in pkg_entries if x not in pkg_match]
# packages only in wiki
pkg_only_wiki = [x for x in wiki_entries if x not in pkg_entries_lower]

# output directories
packages_dir = 'packages'
pkg_dir = 'packages/pkg'
tmp_dir = 'packages/tmp'
only_hunter_dir = 'packages/only_hunter'
only_wiki_dir = 'packages/only_wiki'

# create if not exist
for d in [packages_dir, pkg_dir, tmp_dir, only_hunter_dir, only_wiki_dir]:
    if not os.path.exists(d):
        os.mkdir(d)

# header for rst files
header_format_string = """.. spelling::

    {}

.. _pkg.{}:

    {}
    {}
"""

# convert matching entries
for entry in pkg_match:
    source_md = os.path.join(WIKI_DIR, 'pkg.' + entry.lower() + '.md')
    tmp_rst = os.path.join(tmp_dir, entry + '.rst')
    target_rst = os.path.join(pkg_dir, entry + '.rst')
    
    underscores = "=" * len(entry)
    header = header_format_string.format(entry, entry, entry, underscores)
    #print(header)
    cmd = ['pandoc', source_md, '-o', tmp_rst]
    print("cmd: ", cmd)
    subprocess.call(cmd)
    
    with open(target_rst, 'w') as f:
        f.write(header)
        with open(tmp_rst, 'r') as tmp:
            for line in tmp.readlines():
                f.write(line)

# create dummy entries for packages only in hunter
for entry in pkg_only_hunter:
    source_md = os.path.join(WIKI_DIR, 'pkg.' + entry.lower() + '.md')
    tmp_rst = os.path.join(tmp_dir, entry + '.rst')
    target_rst = os.path.join(only_hunter_dir, entry + '.rst')
    
    underscores = "=" * len(entry)
    header = header_format_string.format(entry, entry, entry, underscores)
    #print(header)
     
    with open(target_rst, 'w') as f:
        f.write(header)

# convert wiki entry, put in extra folder
for entry in pkg_only_wiki:
    source_md = os.path.join(WIKI_DIR, 'pkg.' + entry + '.md')
    tmp_rst = os.path.join(tmp_dir, entry + '.rst')
    target_rst = os.path.join(only_wiki_dir, entry + '.rst')
    
    underscores = "=" * len(entry)
    header = header_format_string.format(entry, entry, entry, underscores)
    #print(header)
    cmd = ['pandoc', source_md, '-o', tmp_rst]
    print("cmd: ", cmd)
    subprocess.call(cmd)
    
    with open(target_rst, 'w') as f:
        f.write(header)
        with open(tmp_rst, 'r') as tmp:
            for line in tmp.readlines():
                f.write(line)

@@ -1245,16 +1245,249 @@
 %7B%7D%0A%22%22%22%0A%0A
+def append_file(f, input_file):%0A    with open(input_file, 'r') as tmp:%0A        for line in tmp.readlines():%0A            if line.startswith('.. code::'):%0A                line = '.. code-block::' + line%5B10:%5D%0A            f.write(line)%0A%0A
 # conver
@@ -2015,109 +2015,30 @@
     
-with open(tmp_rst, 'r') as tmp:%0A            for line in tmp.readlines():%0A                f.write(line
+append_file(f, tmp_rst
 )%0A%0A#
@@ -3059,107 +3059,28 @@
     
-with open(tmp_rst, 'r') as tmp:%0A            for line in tmp.readlines():%0A                f.write(line
+append_file(f, tmp_rst
 )%0A

replace '.. code::' with '.. code-block::'