#!/usr/bin/env bash

set -e

# create a string to string mapping, file name to expected file location
declare -A file_paths
file_paths=(
  ["xr_driver/manifest"]="{xr_driver_data_dir}/manifest"
  ["breezydesktop@xronlinux.com"]="{gnome_shell_data_dir}/extensions/breezydesktop@xronlinux.com"
)

# 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]}

  # check if the file path is a directory
    if [ -d "$actual_file_path" ]; then
        # compute the SHA256 hash of the directory contents
        pushd $actual_file_path > /dev/null
        actual_hash=$(find -L . -type f ! -name "*.compiled" -exec sha256sum {} \; | sort | sha256sum | sed 's/ .*//')
        popd > /dev/null
    else
        # compute the SHA256 hash of the actual file
        actual_hash=$(sha256sum $actual_file_path | awk '{print $1}')
    fi

  # 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"