import os
import subprocess

# hardcoded paths
HUNTER_DIR='..'
PACKAGES_DIR=os.path.join(HUNTER_DIR, 'cmake/projects')
DOCS_PKG_DIR=os.path.join(HUNTER_DIR, 'docs', 'packages', 'pkg')

# get all wiki entries
docs_filenames = [x for x in os.listdir(DOCS_PKG_DIR) if x.endswith('.rst')]
docs_entries = [x[:-4] for x in docs_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 in docs_entries]
# packages only in hunter
pkg_only_hunter = [x for x in pkg_entries if x not in pkg_match]

# output directories
packages_dir = 'packages'
tmp_dir = 'packages/tmp'
only_hunter_dir = 'packages/only_hunter'

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

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

    {}

.. _pkg.{}:

{}
{}

"""

# 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)

print("pkg_match entries:       ", len(pkg_match))
print("pkg_only_hunter entries: ", len(pkg_only_hunter))
add docs_create_missing_stubs