# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of nvme-cli.
# Copyright (c) 2026 SUSE Software Solutions
#
# Authors: Daniel Wagner <dwagner@suse.de>

adoc_sources_man1 = [
    'nvme',
    'nvme-admin-passthru',
    'nvme-capacity-mgmt',
    'nvme-compare',
    'nvme-config-convert',
    'nvme-config-create',
    'nvme-config-show',
    'nvme-config-status',
    'nvme-config-validate',
    'nvme-connect',
    'nvme-connect-all',
    'nvme-copy',
    'nvme-dapustor-smart-log-add',
    'nvme-dera-stat',
    'nvme-device-self-test',
    'nvme-dim',
    'nvme-dir-receive',
    'nvme-dir-send',
    'nvme-disconnect',
    'nvme-disconnect-all',
    'nvme-discover',
    'nvme-dsm',
    'nvme-exclusion-add',
    'nvme-exclusion-create',
    'nvme-exclusion-delete',
    'nvme-exclusion-edit',
    'nvme-exclusion-list',
    'nvme-exclusion-remove',
    'nvme-fdp-configs',
    'nvme-fdp-events',
    'nvme-fdp-feature',
    'nvme-fdp-set-events',
    'nvme-fdp-stats',
    'nvme-fdp-status',
    'nvme-fdp-update',
    'nvme-fdp-usage',
    'nvme-flush',
    'nvme-format',
    'nvme-fw-commit',
    'nvme-fw-download',
    'nvme-gen-hostnqn',
    'nvme-get-feature',
    'nvme-get-lba-status',
    'nvme-get-log',
    'nvme-get-property',
    'nvme-help',
    'nvme-huawei-id-ctrl',
    'nvme-huawei-list',
    'nvme-id-ctrl',
    'nvme-id-ctrl-list',
    'nvme-id-domain',
    'nvme-id-endgrp-list',
    'nvme-id-iocs',
    'nvme-id-ns',
    'nvme-id-ns-descs',
    'nvme-id-ns-ind',
    'nvme-id-ns-list',
    'nvme-id-nvm-ctrl',
    'nvme-id-nvm-ns',
    'nvme-id-nvm-ns-lba-format',
    'nvme-id-nvmset',
    'nvme-id-primary-ctrl-caps',
    'nvme-id-secondary-ctrl-list',
    'nvme-inspur-nvme-vendor-log',
    'nvme-intel-id-ctrl',
    'nvme-intel-internal-log',
    'nvme-intel-lat-stats',
    'nvme-intel-market-name',
    'nvme-intel-smart-log-add',
    'nvme-intel-temp-stats',
    'nvme-io-mgmt-recv',
    'nvme-io-mgmt-send',
    'nvme-io-passthru',
    'nvme-keys-check-kxchap-secret',
    'nvme-keys-check-tls-psk',
    'nvme-keys-export',
    'nvme-keys-gen-kxchap-secret',
    'nvme-keys-gen-tls-psk',
    'nvme-keys-import',
    'nvme-keys-insert-tls-psk',
    'nvme-keys-revoke',
    'nvme-list',
    'nvme-list-subsys',
    'nvme-lockdown',
    'nvme-log-ana',
    'nvme-log-ave-discovery',
    'nvme-log-boot-part',
    'nvme-log-changed-alloc-ns-list',
    'nvme-log-changed-ns-list',
    'nvme-log-dispersed-ns-participating-nss',
    'nvme-log-effects',
    'nvme-log-endurance',
    'nvme-log-endurance-event-agg',
    'nvme-log-error',
    'nvme-log-fid-support-effects',
    'nvme-log-fw',
    'nvme-log-host-discovery',
    'nvme-log-lba-status',
    'nvme-log-media-unit-stat',
    'nvme-log-mgmt-addr-list',
    'nvme-log-mi-cmd-support-effects',
    'nvme-log-persistent-event',
    'nvme-log-phy-rx-eom',
    'nvme-log-pred-lat-event-agg',
    'nvme-log-predictable-lat',
    'nvme-log-reachability-associations',
    'nvme-log-reachability-groups',
    'nvme-log-resv-notif',
    'nvme-log-rotational-media-info',
    'nvme-log-sanitize',
    'nvme-log-self-test',
    'nvme-log-smart',
    'nvme-log-supported-cap-config',
    'nvme-log-telemetry',
    'nvme-mangoboost-id-ctrl',
    'nvme-micron-clear-pcie-errors',
    'nvme-micron-id-ctrl',
    'nvme-micron-internal-log',
    'nvme-micron-nand-stats',
    'nvme-micron-pcie-stats',
    'nvme-micron-selective-download',
    'nvme-micron-smart-add-log',
    'nvme-micron-smart-log',
    'nvme-micron-temperature-stats',
    'nvme-netapp-ontapdevices',
    'nvme-netapp-smdevices',
    'nvme-ns-attach',
    'nvme-ns-create',
    'nvme-ns-delete',
    'nvme-ns-detach',
    'nvme-ns-get-id',
    'nvme-ns-rescan',
    'nvme-nvme-mi-recv',
    'nvme-nvme-mi-send',
    'nvme-ocp-clear-fw-activate-history',
    'nvme-ocp-clear-pcie-correctable-error-counters',
    'nvme-ocp-device-capability-log',
    'nvme-ocp-eol-plp-failure-mode',
    'nvme-ocp-error-recovery-log',
    'nvme-ocp-get-dssd-async-event-config',
    'nvme-ocp-get-dssd-power-state-feature',
    'nvme-ocp-get-plp-health-check-interval',
    'nvme-ocp-internal-log',
    'nvme-ocp-latency-monitor-log',
    'nvme-ocp-set-dssd-async-event-config',
    'nvme-ocp-set-dssd-power-state-feature',
    'nvme-ocp-set-plp-health-check-interval',
    'nvme-ocp-set-telemetry-profile',
    'nvme-ocp-smart-add-log',
    'nvme-ocp-telemetry-string-log',
    'nvme-ocp-unsupported-reqs-log',
    'nvme-read',
    'nvme-registry-delete',
    'nvme-registry-list',
    'nvme-registry-retrieve',
    'nvme-registry-update',
    'nvme-reset',
    'nvme-resv-acquire',
    'nvme-resv-register',
    'nvme-resv-release',
    'nvme-resv-report',
    'nvme-rpmb-info',
    'nvme-rpmb-program-key',
    'nvme-rpmb-read-config',
    'nvme-rpmb-read-counter',
    'nvme-rpmb-read-data',
    'nvme-rpmb-write-config',
    'nvme-rpmb-write-data',
    'nvme-sanitize',
    'nvme-seagate-clear-fw-activate-history',
    'nvme-seagate-clear-pcie-correctable-errors',
    'nvme-seagate-cloud-SSD-plugin-version',
    'nvme-seagate-get-ctrl-tele',
    'nvme-seagate-get-host-tele',
    'nvme-seagate-help',
    'nvme-seagate-plugin-version',
    'nvme-seagate-version',
    'nvme-seagate-vs-fw-activate-history',
    'nvme-seagate-vs-internal-log',
    'nvme-seagate-vs-log-page-sup',
    'nvme-seagate-vs-pcie-stats',
    'nvme-seagate-vs-smart-add-log',
    'nvme-seagate-vs-temperature-stats',
    'nvme-security-recv',
    'nvme-security-send',
    'nvme-sed-discover',
    'nvme-sed-initialize',
    'nvme-sed-lock',
    'nvme-sed-password',
    'nvme-sed-revert',
    'nvme-sed-unlock',
    'nvme-set-feature',
    'nvme-set-property',
    'nvme-show-hostnqn',
    'nvme-show-regs',
    'nvme-show-topology',
    'nvme-sndk-capabilities',
    'nvme-sndk-clear-assert-dump',
    'nvme-sndk-clear-fw-activate-history',
    'nvme-sndk-clear-pcie-correctable-errors',
    'nvme-sndk-cloud-SSD-plugin-version',
    'nvme-sndk-cloud-boot-SSD-version',
    'nvme-sndk-cu-smart-log',
    'nvme-sndk-drive-resize',
    'nvme-sndk-get-dev-capabilities-log',
    'nvme-sndk-get-drive-status',
    'nvme-sndk-get-error-recovery-log',
    'nvme-sndk-get-latency-monitor-log',
    'nvme-sndk-get-unsupported-reqs-log',
    'nvme-sndk-log-page-directory',
    'nvme-sndk-namespace-resize',
    'nvme-sndk-set-latency-monitor-feature',
    'nvme-sndk-vs-cloud-log',
    'nvme-sndk-vs-device-waf',
    'nvme-sndk-vs-drive-info',
    'nvme-sndk-vs-error-reason-identifier',
    'nvme-sndk-vs-fw-activate-history',
    'nvme-sndk-vs-hw-rev-log',
    'nvme-sndk-vs-internal-log',
    'nvme-sndk-vs-nand-stats',
    'nvme-sndk-vs-pcie-stats',
    'nvme-sndk-vs-smart-add-log',
    'nvme-sndk-vs-telemetry-controller-option',
    'nvme-sndk-vs-temperature-stats',
    'nvme-solidigm-clear-fw-activate-history',
    'nvme-solidigm-clear-pcie-correctable-errors',
    'nvme-solidigm-cloud-SSDplugin-version',
    'nvme-solidigm-garbage-collect-log',
    'nvme-solidigm-id-ctrl',
    'nvme-solidigm-latency-tracking-log',
    'nvme-solidigm-log-page-directory',
    'nvme-solidigm-market-log',
    'nvme-solidigm-parse-telemetry-log',
    'nvme-solidigm-smart-log-add',
    'nvme-solidigm-temp-stats',
    'nvme-solidigm-vs-drive-info',
    'nvme-solidigm-vs-fw-activate-history',
    'nvme-solidigm-vs-internal-log',
    'nvme-solidigm-vs-smart-add-log',
    'nvme-solidigm-workload-tracker',
    'nvme-subsystem-reset',
    'nvme-supported-log-pages',
    'nvme-toshiba-clear-pcie-correctable-errors',
    'nvme-toshiba-vs-internal-log',
    'nvme-toshiba-vs-smart-add-log',
    'nvme-transcend-badblock',
    'nvme-transcend-healthvalue',
    'nvme-verify',
    'nvme-virtium-save-smart-to-vtview-log',
    'nvme-virtium-show-identify',
    'nvme-wdc-cap-diag',
    'nvme-wdc-capabilities',
    'nvme-wdc-clear-assert-dump',
    'nvme-wdc-clear-fw-activate-history',
    'nvme-wdc-clear-pcie-correctable-errors',
    'nvme-wdc-cloud-SSD-plugin-version',
    'nvme-wdc-cloud-boot-SSD-version',
    'nvme-wdc-cu-smart-log',
    'nvme-wdc-drive-essentials',
    'nvme-wdc-drive-log',
    'nvme-wdc-drive-resize',
    'nvme-wdc-enc-get-log',
    'nvme-wdc-get-crash-dump',
    'nvme-wdc-get-dev-capabilities-log',
    'nvme-wdc-get-drive-status',
    'nvme-wdc-get-error-recovery-log',
    'nvme-wdc-get-latency-monitor-log',
    'nvme-wdc-get-pfail-dump',
    'nvme-wdc-get-unsupported-reqs-log',
    'nvme-wdc-id-ctrl',
    'nvme-wdc-log-page-directory',
    'nvme-wdc-namespace-resize',
    'nvme-wdc-purge',
    'nvme-wdc-purge-monitor',
    'nvme-wdc-vs-cloud-log',
    'nvme-wdc-vs-device-waf',
    'nvme-wdc-vs-drive-info',
    'nvme-wdc-vs-error-reason-identifier',
    'nvme-wdc-vs-fw-activate-history',
    'nvme-wdc-vs-hw-rev-log',
    'nvme-wdc-vs-internal-log',
    'nvme-wdc-vs-nand-stats',
    'nvme-wdc-vs-smart-add-log',
    'nvme-wdc-vs-telemetry-controller-option',
    'nvme-wdc-vs-temperature-stats',
    'nvme-write',
    'nvme-write-uncor',
    'nvme-write-zeroes',
    'nvme-zns-changed-zone-list',
    'nvme-zns-close-zone',
    'nvme-zns-finish-zone',
    'nvme-zns-id-ctrl',
    'nvme-zns-id-ns',
    'nvme-zns-offline-zone',
    'nvme-zns-open-zone',
    'nvme-zns-report-zones',
    'nvme-zns-reset-zone',
    'nvme-zns-set-zone-desc',
    'nvme-zns-zone-append',
    'nvme-zns-zone-mgmt-recv',
    'nvme-zns-zone-mgmt-send',
]

if want_deprecated_cmds
    adoc_sources_man1 += [
        'nvme-ana-log',
        'nvme-attach-ns',
        'nvme-ave-discovery-log',
        'nvme-boot-part-log',
        'nvme-changed-alloc-ns-list-log',
        'nvme-changed-ns-list-log',
        'nvme-check-dhchap-key',
        'nvme-check-tls-key',
        'nvme-cmdset-ind-id-ns',
        'nvme-create-ns',
        'nvme-delete-ns',
        'nvme-detach-ns',
        'nvme-dispersed-ns-participating-nss-log',
        'nvme-effects-log',
        'nvme-endurance-event-agg-log',
        'nvme-endurance-log',
        'nvme-error-log',
        'nvme-fid-support-effects-log',
        'nvme-fw-log',
        'nvme-gen-dhchap-key',
        'nvme-gen-tls-key',
        'nvme-get-ns-id',
        'nvme-host-discovery-log',
        'nvme-lba-status-log',
        'nvme-list-ctrl',
        'nvme-list-endgrp',
        'nvme-list-ns',
        'nvme-list-secondary',
        'nvme-media-unit-stat-log',
        'nvme-mgmt-addr-list-log',
        'nvme-mi-cmd-support-effects-log',
        'nvme-ns-descs',
        'nvme-nvm-id-ctrl',
        'nvme-nvm-id-ns',
        'nvme-nvm-id-ns-lba-format',
        'nvme-persistent-event-log',
        'nvme-phy-rx-eom-log',
        'nvme-pred-lat-event-agg-log',
        'nvme-predictable-lat-log',
        'nvme-primary-ctrl-caps',
        'nvme-reachability-associations-log',
        'nvme-reachability-groups-log',
        'nvme-resv-notif-log',
        'nvme-rotational-media-info-log',
        'nvme-sanitize-log',
        'nvme-self-test-log',
        'nvme-smart-log',
        'nvme-supported-cap-config-log',
        'nvme-telemetry-log',
        'nvme-tls-key',
    ]
endif

adoc_sources_man5 = [
    'nvme-cli.conf',
    'nvme-fabrics.conf',
]

adoc_sources_man8 = [
]

if want_discoverd
    adoc_sources_man8 += [
        'nvme-discoverd',
    ]
endif

adoc_includes = [
    'cmd-plugins.txt',
    'cmds-main.txt',
    'global-options.txt',
    'fabrics-options.txt',
]

if want_docs != 'false'
    man1dir = join_paths(prefixdir, get_option('mandir'), 'man1')
    man5dir = join_paths(prefixdir, get_option('mandir'), 'man5')
    man8dir = join_paths(prefixdir, get_option('mandir'), 'man8')
    htmldir = join_paths(prefixdir, get_option('htmldir'), 'nvme')

    man_sets = [
        {'sources': adoc_sources_man1,
         'section': '1',
         'dir': man1dir},
        {'sources': adoc_sources_man5,
         'section': '5',
         'dir': man5dir},
        {'sources': adoc_sources_man8,
         'section': '8',
         'dir': man8dir},
    ]

    asciidoctor = find_program('asciidoc', required: want_docs_build)
    if want_docs_build and asciidoctor.found()
        # Build documentation before installing
        foreach file : adoc_includes
            configure_file(input: file, output: file, copy: true)
        endforeach

        # man pages
        if want_docs == 'all' or want_docs == 'man'
            xmlto = find_program('xmlto', required: false)
            if xmlto.found()
                foreach man_set : man_sets
                    section = man_set['section']
                    foreach adoc : man_set['sources']
                        input = adoc + '.txt'
                        subst = configure_file(
                            input: adoc + '.txt',
                            output: adoc + '.m' + section + 'subst',
                            configuration: substs
                        )
                        xml = custom_target(
                            adoc.underscorify() + '_xml' + section,
                            input: subst,
                            output: '@BASENAME@.xml',
                            command: [
                                asciidoctor,
                                '-f', files('asciidoc.conf'),
                                '-b', 'docbook',
                                '-d', 'manpage',
                                '-o', '@OUTPUT@',
                                '@INPUT@',
                            ],
                        )
                        doc_tgts += custom_target(
                            adoc.underscorify() + '_man' + section,
                            input: xml,
                            output: '@BASENAME@.' + section,
                            command: [
                                xmlto,
                                '-m', files('manpage-normal.xsl'),
                                '-o', '@OUTDIR@',
                                '--skip-validation',
                                'man',
                                '@INPUT@',
                            ],
                            install: true,
                            install_dir: man_set['dir'],
                        )
                    endforeach
                endforeach
            endif
        endif

        # html
        if want_docs == 'all' or want_docs == 'html'
          foreach adoc : ( adoc_sources_man1 +
                           adoc_sources_man5 +
                           adoc_sources_man8 )
                input = adoc + '.txt'
                subst = configure_file(
                    input: adoc + '.txt',
                    output: adoc + '.hsubst',
                    configuration: substs,
                )
                doc_tgts += custom_target(
                    adoc.underscorify() + '_html',
                    input: subst,
                    output: '@BASENAME@.html',
                    command: [
                        asciidoctor,
                        '-f', files('asciidoc.conf'),
                        '-b', 'xhtml11',
                        '-d', 'manpage',
                        '-o', '@OUTPUT@',
                        '@INPUT@'],
                    install: true,
                    install_dir: htmldir,
                )
            endforeach
        endif
    else
        # asciidoctor not found, install pre compiled documetationx
        foreach man_set : man_sets
            section = man_set['section']
            foreach adoc : man_set['sources']
                if want_docs == 'all' or want_docs == 'man'
                    man = files(adoc + '.' + section)
                    install_data(man, install_dir: man_set['dir'])
                endif
                if want_docs == 'all' or want_docs == 'html'
                    html = files(adoc + '.html')
                    install_data(html, install_dir: htmldir)
                endif
            endforeach
        endforeach
    endif
endif
