#!/usr/bin/env bash

set -x
set -e

################################################################
# Vulkan
################################################################

echo "Cleaning Vulkan-Docs"
git -C generate-new/Vulkan-Docs clean -dxf

echo "Generating Vulkan-Docs documentation"
(cd generate-new/Vulkan-Docs &&
  nix-shell -p \
    python3 python3Packages.pyparsing asciidoctor gnumake nodejs nodePackages.he nodePackages.escape-string-regexp \
    --run "./makeAllExts refpages generated")

echo "Building generator"
generate=$(IN_NIX_SHELL='' nix-build  -A generate-new)

echo "Cleaning src"
git rm --quiet -r src/Vulkan src/Vulkan.hs
mkdir -p src
test -f generate-new/out || test -L generate-new/out || ln -s ../src generate-new/out
echo "Generating vulkan"
nix-shell -p asciidoctor --run "sh -c 'cd generate-new && \"$generate/bin/vk\"'"
git add src
nix-shell -p haskellPackages.hpack --run hpack

################################################################
# VMA
################################################################

echo "Cleaning VulkanMemoryAllocator source" &&
git -C VulkanMemoryAllocator/VulkanMemoryAllocator clean -dxf &&

echo "Generating VMA documentation" &&
(cd VulkanMemoryAllocator/VulkanMemoryAllocator &&
  sed -i -e 's|^GENERATE_DOCBOOK.*|GENERATE_DOCBOOK=YES|' \
         -e 's|^BRIEF_MEMBER_DESC.*|BRIEF_MEMBER_DESC=NO|' \
         -e 's|^PREDEFINED *=|PREDEFINED = VMA_STATS_STRING_ENABLED=1 |' \
         -e 's|^PREDEFINED *=|PREDEFINED = VMA_EXTENDS_VK_STRUCT(s)=s |' \
         -e 's|@CMAKE_SOURCE_DIR@/||' \
         Doxyfile &&
  nix-shell -p doxygen --run 'doxygen Doxyfile')

echo "Generating Vulkan-Docs headers"
(cd generate-new/Vulkan-Docs/xml &&
  nix-shell -p \
    python3 python3Packages.pyparsing asciidoctor gnumake nodejs nodePackages.he nodePackages.escape-string-regexp \
    --run "make clean install")

echo "Generating VulkanMemoryAllocator"
nix-shell -p vulkan-headers --run "sh -c 'cd generate-new && \"$generate/bin/vma\"'"
nix-shell -p haskellPackages.hpack --run 'hpack VulkanMemoryAllocator'

echo "Cleaning VMA documentation"
git -C VulkanMemoryAllocator/VulkanMemoryAllocator clean -dxf
git -C VulkanMemoryAllocator/VulkanMemoryAllocator checkout .

################################################################
# OpenXR
################################################################

echo "Cleaning OpenXR Docs"
git -C generate-new/OpenXR-Docs clean -dxf

echo "Generating OpenXR-Docs documentation"
(cd generate-new/OpenXR-Docs/specification &&
  nix-shell -p \
    python3 python3Packages.pyparsing asciidoctor gnumake nodejs nodePackages.he nodePackages.escape-string-regexp \
    --run "./makeAllExts man/apispec.txt generated")

echo "Cleaning src"
git rm --quiet -r openxr/src/OpenXR openxr/src/OpenXR.hs
mkdir -p openxr/src
test -f generate-new/out-xr || test -L generate-new/out-xr || ln -s ../openxr/src generate-new/out-xr
echo "Generating openxr"
nix-shell -p asciidoctor --run "sh -c 'cd generate-new && \"$generate/bin/xr\"'"
git add openxr/src
nix-shell -p haskellPackages.hpack --run 'hpack openxr'
