#!/bin/sh -e

# Copyright 2023 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0

# Build vulkan_core.h to be included with dfdutils. It is the standard
# vulkan_core.h with the unreleased VK_EXT_texture_compression_ASTC_3d
# extension force included and VK_KHR_video_decode_h264 and
# VK_KHR_video_decode_h265 removed due to their including other large
# header files.

# You need python3 installed to use this script. You also need
# the pyparsing module. Install with `pip install pyparsing`.

# Note that genvk.py explicitly calls for /usr/bin/python3. If you are
# on macOS, the only way to get this is to install the Xcode command
# line tools. If this is not possible, use Docker.

# If you do not have python but have Docker and the Vulkan docker image
# installed you  can do the work with that but you will have to manually
# run the command and copy the resulting file here.
#

function usage() {
  echo "$0 <path>"
  echo "Path to vulkan directory in your clone of the Khronos GitLab vulkan"
  echo "repo or path to your clone of the GitHub KhronosGroup/Vulkan-Docs"
  echo "repo. GitLab is the preferred source."
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
  usage
  exit 0
fi

if [ $# -ne 1 ]; then
  echo "Need path."
  usage
  exit 2
fi

# Generated file goes in same directory as this script.
dst=$(dirname $0)
if [ ! "${dst:0:1}" = "/" ]; then
  dst="$(pwd)/$dst"
fi

cd $1
scripts/genvk.py -extension VK_EXT_extension_289 -removeExtensions '(VK_KHR_video_decode_av1|VK_KHR_video_decode_h264|VK_KHR_video_encode_h264|VK_KHR_video_decode_h265|VK_KHR_video_encode_h265)' -registry xml/vk.xml -o $dst vulkan_core.h

# To use with Docker
#   1. Install Docker
#   2. In a terminal
#      $ cd <your clone of the GitLab vulkan repo>
#      $ scripts/runDocker
#   3. At the prompt in the shell started by Docker
#      $ <paste the above scripts/genvk command with $dst replaced with '.'>
#      $ ^d   # to exit the Docker shell.
#   4. Back at the prompt in no. 2
#      $ mv vulkan_core.h <path to your dfdutils repo clone/vulkan/
#      $ cd <path to your dfdutils repo>
# The pasting and copying steps are needed because programs cannot reach
# outside docker to the host file system.

