#!/usr/bin/env bash

# exit when any command fails
set -e

ARCH=${ARCH:-$(uname -m)}

# https://stackoverflow.com/a/246128
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
ROOT_DIR=$(realpath $SCRIPT_DIR/..)
VULKAN_DIR=$ROOT_DIR/vulkan

if [ -z "$GNOME_44_MAX" ]; then
  # standard build for GNOME 45 and up
  echo "Building Breezy GNOME for $ARCH, GNOME versions 45 and up"
  GNOME_DIR=$ROOT_DIR/gnome
  BUILD_FILE_NAME=breezyGNOME-$ARCH.tar.gz
else
  # special patched build for GNOME 44 and below
  echo "Building Breezy GNOME for $ARCH, GNOME versions 44 and below"

  # use a special directory for the GNOME 44 max build, ignored by git
  GNOME_DIR=$ROOT_DIR/gnome-44-max
  rm -rf $GNOME_DIR
  mkdir $GNOME_DIR
  BUILD_FILE_NAME=breezyGNOME-44-max-$ARCH.tar.gz

  # copy the GNOME extension source code to the new directory and apply 
  # a patch that makes it compatible with GNOME 44 and below
  cp -ra $ROOT_DIR/gnome/* $GNOME_DIR
  rm -rf $GNOME_DIR/build
  git apply gnome-44-max.patch
fi
GNOME_BUILD_DIR=$GNOME_DIR/build

mkdir -p $GNOME_BUILD_DIR
if [ -e "$GNOME_BUILD_DIR/$BUILD_FILE_NAME" ]; then
  rm $GNOME_BUILD_DIR/$BUILD_FILE_NAME
fi

PACKAGE_DIR=$GNOME_BUILD_DIR/breezy_gnome
rm -rf $PACKAGE_DIR
mkdir -p $PACKAGE_DIR

XR_DRIVER_DIR=$ROOT_DIR/modules/XRLinuxDriver
source $XR_DRIVER_DIR/bin/inject_ua

# if a custom_banner image exists, copy it over the sombrero one
if [ -e "$VULKAN_DIR/custom_banner.png" ]; then
  cp $VULKAN_DIR/custom_banner.png $PACKAGE_DIR
fi

# copy vulkan setup scripts and configs
mkdir -p $PACKAGE_DIR/bin
copy_and_inject_ua "$XR_DRIVER_DIR/bin/ua.sh" "$PACKAGE_DIR/bin" "$GNOME_DIR/bin/setup" "$GNOME_DIR/bin/breezy_gnome_verify" "$GNOME_DIR/bin/breezy_gnome_uninstall"

XR_DRIVER_BINARY=$XR_DRIVER_DIR/out/xrDriver-$ARCH.tar.gz

if [ ! -e "$XR_DRIVER_BINARY" ] || [ "$1" == "--rebuild-driver" ] || [ "$1" == "--rebuild-all" ]; then
  # if a file exists at custom_banner_config.yml, copy it to the xrealAirLinuxDriver directory
  if [ -e "$VULKAN_DIR/custom_banner_config.yml" ]; then
    cp $VULKAN_DIR/custom_banner_config.yml $XR_DRIVER_DIR
  fi

  pushd $XR_DRIVER_DIR

  # strange issue where the base library produces a .so file if the build is not cleaned
  rm -rf build/

  docker-build/init.sh
  docker-build/run-build.sh $ARCH
  popd
fi

TMP_DIR=$(mktemp -d -t breezy-gnome-XXXXXXXXXX)
pushd $TMP_DIR
cp $XR_DRIVER_BINARY $TMP_DIR/xrDriver.tar.gz
tar -xf $TMP_DIR/xrDriver.tar.gz

XR_DRIVER_MANIFEST_LINE=$(sha256sum xr_driver/manifest)
popd
rm -rf $TMP_DIR

cp $XR_DRIVER_BINARY $PACKAGE_DIR/xrDriver.tar.gz
cp $XR_DRIVER_DIR/bin/xr_driver_setup $PACKAGE_DIR/bin

$GNOME_DIR/bin/package_extension
cp $GNOME_DIR/out/breezydesktop@xronlinux.com.shell-extension.zip $PACKAGE_DIR

# create a checksum that combines the checksums of all files in the directory
pushd $GNOME_DIR/src
GNOME_MANIFEST_LINE=$(find -L . -type f ! -name "*.compiled" -exec sha256sum {} \; | sort | sha256sum | sed 's/ .*//')
popd

UI_BUILD_ARTIFACT=ui/out/breezyUI-$ARCH.tar.gz
if [ ! -e "$UI_BUILD_ARTIFACT" ] || [ "$1" == "--rebuild-ui" ] || [ "$1" == "--rebuild-all" ]; then
  pushd ui
  bin/package $ARCH
  popd
fi
tar -xf $UI_BUILD_ARTIFACT -C $PACKAGE_DIR

# create manifest file for verifying installed file checksums against the originally packaged versions
# include any file that doesn't get modified during setup (e.g. vkBasalt.json files)
pushd $PACKAGE_DIR
echo $XR_DRIVER_MANIFEST_LINE > manifest
echo -e "$GNOME_MANIFEST_LINE breezydesktop@xronlinux.com" >> manifest
popd

# bundle everything up
pushd $GNOME_BUILD_DIR
tar -zcvf $BUILD_FILE_NAME breezy_gnome
popd

mkdir -p out
if [ -e "out/$BUILD_FILE_NAME" ]; then
  rm out/$BUILD_FILE_NAME
fi
cp $GNOME_BUILD_DIR/$BUILD_FILE_NAME out