#!/usr/bin/python3
#
# Copyright 2016-2024 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0

import argparse
import subprocess
import sys

from genspec import *

# Eventually, these may be defined by the extdependency module
Version1_3 = [ 'VK_VERSION_1_0', 'VK_VERSION_1_1', 'VK_VERSION_1_2', 'VK_VERSION_1_3' ]
Version1_2 = [ 'VK_VERSION_1_0', 'VK_VERSION_1_1', 'VK_VERSION_1_2' ]
Version1_1 = [ 'VK_VERSION_1_0', 'VK_VERSION_1_1' ]
Version1_0 = [ 'VK_VERSION_1_0' ]

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('-internal', action='store_true',
                        help='Generate internal build, not public')
    parser.add_argument('-norefpages', action='store_true',
                        help='Do not generate refpages')
    parser.add_argument('-singlerefpage', action='store_true',
                        help='Generate single-page refpage - NOT SUPPORTED')
    parser.add_argument('-chunked', action='store_true',
                        help='Generate chunked HTML outputs')
    parser.add_argument('-pdf', action='store_true',
                        help='Generate PDF outputs')

    parser.add_argument('-nov13', action='store_false', dest='v13',
                        help='Suppress Vulkan 1.3 targets')
    parser.add_argument('-v12', action='store_true',
                        help='Generate Vulkan 1.2 targets')
    parser.add_argument('-v11', action='store_true',
                        help='Generate Vulkan 1.1 targets')
    parser.add_argument('-v10', action='store_true',
                        help='Generate Vulkan 1.0 targets')

    parser.add_argument('-nocorespec', action='store_false', dest='corespec',
                        help='Do not generate core API-only targets')
    parser.add_argument('-noratspec', action='store_false', dest='ratspec',
                        help='Do not generate core API + ratified extensions-only targets')
    parser.add_argument('-noallspec', action='store_false', dest='allspec',
                        help='Do not generate full API + all extensions targets')

    parser.add_argument('-registry', action='store',
                        default=None,
                        help='Path to API XML registry file specifying version and extension dependencies')
    parser.add_argument('-apiname', action='store',
                        default=None,
                        help='API name to generate')

    parser.add_argument('-gitroot', action='store',
                        default='/home/tree/git',
                        help='Set the directory containing gitlab vulkan and github Vulkan-Docs repo clones to build from')
    parser.add_argument('-repodir', action='store', dest='repoDir',
                        default=None,
                        help='Set the repository directory to build from (overrides defaults)')
    parser.add_argument('-outdir', action='store', dest='outDir',
                        default=None,
                        help='Set the output directory to build into (overrides defaults)')

    args = parser.parse_args()

    # Look for scripts/extdependency.py
    # This requires makeSpec to be invoked from the repository root, but we
    # could derive that path.
    sys.path.insert(0, 'scripts')
    from extdependency import ApiDependencies

    deps = ApiDependencies(args.registry, args.apiname)

    allExts = deps.allExtensions()
    ratifiedExts = deps.ratifiedExtensions()

    if args.internal:
        # For internal build & pseudo-release
        if args.repoDir is None:
            args.repoDir = f'{args.gitroot}/vulkan'
        if args.outDir is None:
            args.outDir = f'{args.gitroot}/vulkan/out'
    else:
        # For public release
        if args.repoDir is None:
            args.repoDir = f'{args.gitroot}/Vulkan-Docs'
        if args.outDir is None:
            args.outDir = f'{args.gitroot}/registry/vulkan/specs'

    refPageTargets = ''

    if not args.norefpages:
        # Generate separate reference pages
        refPageTargets += ' manhtmlpages'

    if args.singlerefpage:
        # Generate single-page refpage.
        refPageTargets += ' manhtml'
        if args.pdf:
            refPageTargets += ' manpdf'
        print('echo Info: single-page refpage targets are NOT SUPPORTED')

    specTargets = ' html'
    if args.chunked:
        specTargets += ' chunked'
    if args.pdf:
        specTargets += ' pdf'

    print('echo Info: Building release from', args.repoDir, 'to', args.outDir)
    print('echo Info: Building spec targets', specTargets)
    print('')

    # Current Vulkan 1.3 specs
    if args.v13:
        if args.allspec:
            # Build ref pages and validusage targets only for 1.3 + all exts
            # Formerly set xmlTargets = 'clobber install', but we no longer
            # generate headers in the registry tree.
            buildBranch(targetDir = '1.3-extensions',
                        versions = Version1_3,
                        extensions = allExts,
                        ratified = False,
                        apititle = '(with all registered Vulkan extensions)',
                        specTargets = specTargets + ' validusage' + refPageTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)

        if args.ratspec:
            buildBranch(targetDir = '1.3-khr-extensions',
                        versions = Version1_3,
                        extensions = ratifiedExts,
                        ratified = True,
                        apititle = '(with all ratified extensions)',
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)

        if args.corespec:
            # Build style guide and registry documentation targets only for 1.3
            # + no extensions.
            buildBranch(targetDir = '1.3',
                        versions = Version1_3,
                        extensions = None,
                        ratified = True,
                        apititle = None,
                        specTargets = specTargets + ' styleguide registry',
                        repoDir = args.repoDir,
                        outDir = args.outDir,
                        needRefSources = True)

    # Vulkan 1.2 specs
    if args.v12:
        if args.allspec:
            buildBranch(targetDir = '1.2-extensions',
                        versions = Version1_2,
                        extensions = allExts,
                        ratified = False,
                        apititle = '(with all registered Vulkan extensions)',
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)

        if args.ratspec:
            buildBranch(targetDir = '1.2-khr-extensions',
                        versions = Version1_2,
                        extensions = ratifiedExts,
                        ratified = True,
                        apititle = '(with all ratified extensions)',
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)

        if args.corespec:
            # Build style guide and registry documentation targets only for 1.2
            # + no extensions.
            buildBranch(targetDir = '1.2',
                        versions = Version1_2,
                        extensions = None,
                        ratified = True,
                        apititle = None,
                        specTargets = specTargets + ' styleguide registry',
                        repoDir = args.repoDir,
                        outDir = args.outDir,
                        needRefSources = True)

    # Vulkan 1.1 specs
    if args.v11:
        if args.allspec:
            buildBranch(targetDir = '1.1-extensions',
                        versions = Version1_1,
                        extensions = allExts,
                        ratified = False,
                        apititle = '(with all registered Vulkan extensions)',
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)

        if args.ratspec:
            buildBranch(targetDir = '1.1-khr-extensions',
                        versions = Version1_1,
                        extensions = ratifiedExts,
                        ratified = True,
                        apititle = '(with all ratified extensions)',
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)

        if args.corespec:
            buildBranch(targetDir = '1.1',
                        versions = Version1_1,
                        extensions = None,
                        ratified = True,
                        apititle = None,
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)
    else:
        print('echo Info: Not building 1.1 specs yet')


    # Vulkan 1.0 specs.
    if args.v10:
        if args.allspec:
            buildBranch(targetDir = '1.0-extensions',
                        versions = Version1_0,
                        extensions = allExts,
                        ratified = False,
                        apititle = '(with all registered Vulkan extensions)',
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)

        if args.ratspec:
            buildBranch(targetDir = '1.0-wsi_extensions',
                        versions = Version1_0,
                        extensions = ratifiedExts,
                        ratified = True,
                        apititle = '(with all ratified extensions)',
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)

        if args.corespec:
            buildBranch(targetDir = '1.0',
                        versions = Version1_0,
                        extensions = None,
                        ratified = True,
                        apititle = None,
                        specTargets = specTargets,
                        repoDir = args.repoDir,
                        outDir = args.outDir)
    else:
        print('echo Info: Not building 1.0 specs yet')

    print('echo Info: post-generation cleanup')
    createTags(releaseNum(), buildOnFriday())
