#!/usr/bin/env bash

set -e

# create a string to string mapping, file name to expected file location
declare -A file_paths
file_paths=(
  ["bin/breezy_vulkan_uninstall"]="{bin_dir}/breezy_vulkan_uninstall"
  ["vkBasalt.64/libvkbasalt.so"]="{lib_dir}/libvkbasalt.so"
  ["vkBasalt.32/libvkbasalt.so"]="{lib32_dir}/libvkbasalt.so"
  ["Sombrero.frag"]="{reshade_config_dir}/Shaders/Sombrero.frag"
  ["ReShade.fxh"]="{reshade_config_dir}/Shaders/ReShade.fxh"
  ["ReShadeUI.fxh"]="{reshade_config_dir}/Shaders/ReShadeUI.fxh"
  ["calibrating.png"]="{reshade_config_dir}/Textures/calibrating.png"
  ["custom_banner.png"]="{reshade_config_dir}/Textures/custom_banner.png"
  ["xr_driver/manifest"]="{xr_driver_data_dir}/manifest"
)

# verify the file hashes in ./manifest
while IFS= read -r line
do
  # split the line into hash and filename
  manifest_hash=$(echo $line | awk '{print $1}')
  file=$(echo $line | awk '{print $2}')

  actual_file_path=${file_paths[$file]}

  # compute the SHA256 hash of the actual file
  actual_hash=$(sha256sum $actual_file_path | awk '{print $1}')

  # compare the hashes
  if ! [ "$manifest_hash" = "$actual_hash" ]; then
    echo "Verification failed" >&2
    exit 1
  fi
done < "{data_dir}/manifest"

# if our checks succeeded, run the xr_driver verify script
{bin_dir}/xr_driver_verify > /dev/null

echo "Verification succeeded"