# SPDX-License-Identifier: GPL-2.0-or-later

adoc_sources = [
  'nvme',
  'nvme-admin-passthru',
  'nvme-ana-log',
  'nvme-attach-ns',
  'nvme-boot-part-log',
  'nvme-capacity-mgmt',
  'nvme-changed-ns-list-log',
  'nvme-cmdset-ind-id-ns',
  'nvme-compare',
  'nvme-connect',
  'nvme-connect-all',
  'nvme-copy',
  'nvme-create-ns',
  'nvme-dapustor-smart-log-add',
  'nvme-delete-ns',
  'nvme-dera-stat',
  'nvme-detach-ns',
  'nvme-device-self-test',
  'nvme-dim',
  'nvme-dir-receive',
  'nvme-dir-send',
  'nvme-disconnect',
  'nvme-disconnect-all',
  'nvme-discover',
  'nvme-dsm',
  'nvme-effects-log',
  'nvme-endurance-event-agg-log',
  'nvme-endurance-log',
  'nvme-error-log',
  '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-fid-support-effects-log',
  'nvme-flush',
  'nvme-format',
  'nvme-fw-commit',
  'nvme-fw-download',
  'nvme-fw-log',
  'nvme-gen-hostnqn',
  'nvme-get-feature',
  'nvme-get-lba-status',
  'nvme-get-log',
  'nvme-get-ns-id',
  'nvme-get-property',
  'nvme-help',
  'nvme-huawei-id-ctrl',
  'nvme-huawei-list',
  'nvme-id-ctrl',
  'nvme-id-domain',
  'nvme-id-iocs',
  'nvme-id-ns',
  'nvme-id-nvmset',
  '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-lba-status-log',
  'nvme-list',
  'nvme-list-ctrl',
  'nvme-list-endgrp',
  'nvme-list-ns',
  'nvme-list-subsys',
  'nvme-lockdown',
  'nvme-mi-cmd-support-effects-log',
  'nvme-micron-clear-pcie-errors',
  'nvme-micron-internal-log',
  'nvme-micron-nand-stats',
  'nvme-micron-pcie-stats',
  'nvme-micron-selective-download',
  'nvme-micron-smart-add-log',
  'nvme-micron-temperature-stats',
  'nvme-netapp-ontapdevices',
  'nvme-netapp-smdevices',
  'nvme-ns-descs',
  'nvme-ns-rescan',
  'nvme-nvm-id-ctrl',
  '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-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-ocp-internal-log',
  'nvme-persistent-event-log',
  'nvme-pred-lat-event-agg-log',
  'nvme-predictable-lat-log',
  'nvme-primary-ctrl-caps',
  'nvme-read',
  'nvme-reset',
  'nvme-resv-acquire',
  'nvme-resv-notif-log',
  'nvme-resv-register',
  'nvme-resv-release',
  'nvme-resv-report',
  'nvme-rpmb',
  'nvme-sanitize',
  'nvme-sanitize-log',
  '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-self-test-log',
  'nvme-set-feature',
  'nvme-set-property',
  'nvme-show-hostnqn',
  'nvme-show-regs',
  'nvme-show-topology',
  'nvme-smart-log',
  '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-drive-status',
  'nvme-sndk-get-dev-capabilities-log',
  '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-telemetry-log',
  'nvme-tls-key',
  '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',
  'nvme-sed-discover',
  'nvme-sed-initialize',
  'nvme-sed-lock',
  'nvme-sed-password',
  'nvme-sed-revert',
  'nvme-sed-unlock',
  'nvme-mangoboost-id-ctrl',
]

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

want_docs = get_option('docs')
want_docs_build = get_option('docs-build')
if want_docs != 'false'
  mandir = join_paths(get_option('mandir'), 'man1')
  htmldir = join_paths(get_option('htmldir'), 'nvme')

  asciidoctor = find_program('asciidoc', required: get_option('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 adoc : adoc_sources
          input = adoc + '.txt'
          subst = configure_file(
            input: adoc + '.txt',
            output: adoc + '.msubst',
            configuration: substs)
          xml = custom_target(
            adoc.underscorify() + '_xml',
            input: subst,
            output: '@BASENAME@.xml',
            command: [asciidoctor,
                      '-f', files('asciidoc.conf'),
                      '-b', 'docbook',
                      '-d', 'manpage',
                      '-o', '@OUTPUT@',
                      '@INPUT@'],
          )
          custom_target(
            adoc.underscorify() + '_man',
            input: xml,
            output: '@BASENAME@.1',
            command: [xmlto,
                      '-m', files('manpage-normal.xsl'),
                      '-o', '@OUTDIR@',
                      '--skip-validation',
                      'man',
                      '@INPUT@'],
            install: true,
            install_dir: mandir)
        endforeach
      endif
    endif

    # html
    if want_docs == 'all' or want_docs == 'html'
      foreach adoc : adoc_sources
        input = adoc + '.txt'
        subst = configure_file(
          input: adoc + '.txt',
          output: adoc + '.hsubst',
         configuration: substs)
        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 adoc : adoc_sources
      if want_docs == 'all' or want_docs == 'man'
        man = files(adoc + '.1')
        install_data(man, install_dir: mandir)
      endif
      if want_docs == 'all' or want_docs == 'html'
        html = files(adoc + '.html')
        install_data(html, install_dir: htmldir)
      endif
    endforeach

  endif
endif
