#!/usr/bin/env bash

set -e

check_command() {
    for cmd in "$@"; do
        if command -v "$cmd" &>/dev/null; then
            return
        fi
    done

    echo "Please install one of the following: ${*}, and make sure it's available in your \$PATH, then rerun the setup."
    exit 1
}

check_command "gnome-extensions"
check_command "glib-compile-schemas"
check_command "update-desktop-database"
check_command "gtk-update-icon-cache"
check_command "python" "python3"

# This script gets packaged with the release and should do the bulk of the setup work. This allows this setup to be tied
# to a specific release of the code, and guarantees it will never run along-side newer or older binaries.

USER_HOME=$(realpath ~)

if [ -z "$XDG_DATA_HOME" ]; then
  XDG_DATA_HOME="$USER_HOME/.local/share"
fi
XR_DRIVER_DATA_DIR="$XDG_DATA_HOME/xr_driver"
GNOME_SHELL_DATA_DIR="$XDG_DATA_HOME/gnome-shell"
BREEZY_GNOME_DATA_DIR="$XDG_DATA_HOME/breezy_gnome"

if [ -z "$XDG_BIN_HOME" ]; then
  XDG_BIN_HOME="$USER_HOME/.local/bin"
fi

if [ -d "$XDG_BIN_HOME" ]; then
  # check ownership and permissions before doing chown and chmod
  XDG_BIN_USER=$(stat -c %U $XDG_BIN_HOME)
  XDG_BIN_GROUP=$(stat -c %G $XDG_BIN_HOME)

  USER=$(whoami)
  GROUP=$(id -gn)

  if [ "$XDG_BIN_USER" != "$USER" ] || [ "$XDG_BIN_GROUP" != "$GROUP" ]; then
    echo "Fixing ownership and permissions of $XDG_BIN_HOME"
    sudo chown -R $USER:$GROUP $XDG_BIN_HOME
    sudo chmod -R 700 $XDG_BIN_HOME
  fi
fi

UA_EVENT_NAME="breezy_gnome_install"
if [ -e "$XDG_BIN_HOME/breezy_gnome_uninstall" ]; then
  echo "Cleaning up the previous installation"

  # ` || true` will ensure that this can't cause a failure, even with `set -e`
  $XDG_BIN_HOME/breezy_gnome_uninstall --for-install || true

  UA_EVENT_NAME="breezy_gnome_update"
fi

UA_CLIENT_ID="BreezyGNOME"
UA_EVENT_VERSION="$1"
#INJECT_UA_CALL

# escaping sed replace: https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern
ESCAPED_XDG_BIN_HOME=$(printf '%s\n' "$XDG_BIN_HOME" | sed -e 's/[\/&]/\\&/g')
ESCAPED_BREEZY_GNOME_DATA_DIR=$(printf '%s\n' "$BREEZY_GNOME_DATA_DIR" | sed -e 's/[\/&]/\\&/g')
ESCAPED_XR_DRIVER_DATA_DIR=$(printf '%s\n' "$XR_DRIVER_DATA_DIR" | sed -e 's/[\/&]/\\&/g')
ESCAPED_GNOME_SHELL_DATA_DIR=$(printf '%s\n' "$GNOME_SHELL_DATA_DIR" | sed -e 's/[\/&]/\\&/g')

echo "Copying the breezy_gnome scripts to ${XDG_BIN_HOME}"
mkdir -p $XDG_BIN_HOME
cp bin/breezy_gnome_uninstall $XDG_BIN_HOME
sed -i -e "s/{bin_dir}/$ESCAPED_XDG_BIN_HOME/g" \
       -e "s/{data_dir}/$ESCAPED_BREEZY_GNOME_DATA_DIR/g" \
       -e "s/{xr_driver_data_dir}/$ESCAPED_XR_DRIVER_DATA_DIR/g" \
       -e "s/{gnome_shell_data_dir}/$ESCAPED_GNOME_SHELL_DATA_DIR/g" \
          bin/breezy_gnome_verify
cp bin/breezy_gnome_verify $XDG_BIN_HOME

echo "Copying the manifest file to ${BREEZY_GNOME_DATA_DIR}"
mkdir -p $BREEZY_GNOME_DATA_DIR
cp manifest $BREEZY_GNOME_DATA_DIR

echo "Installing the breezydesktop@xronlinux.com GNOME extension"
gnome-extensions install --force breezydesktop@xronlinux.com.shell-extension.zip

echo "Installing the Breezy Desktop UI application"
cp -r breezy_ui/data/* $XDG_DATA_HOME
cp -r breezy_ui/bin/* $XDG_BIN_HOME

# update copied files to use the local XDG paths
ESCAPED_XDG_DATA_HOME=$(printf '%s\n' "$XDG_DATA_HOME" | sed -e 's/[\/&]/\\&/g')
sed -i -e "s/\/usr\/local\/share/$ESCAPED_XDG_DATA_HOME/g" $XDG_BIN_HOME/breezydesktop
sed -i "/Exec/c\Exec=$XDG_BIN_HOME/breezydesktop" $XDG_DATA_HOME/applications/com.xronlinux.BreezyDesktop.desktop

glib-compile-schemas $XDG_DATA_HOME/glib-2.0/schemas
update-desktop-database $XDG_DATA_HOME/applications
gtk-update-icon-cache

# refresh bash session so new commands in the PATH are available
hash -r

# set up the XR driver using the local binary
echo "Installing xrDriver"
echo "BEGIN - xr_driver_setup"
if [ -z "$1" ]
then
  sudo bin/xr_driver_setup $(pwd)/xrDriver.tar.gz
else
  sudo bin/xr_driver_setup -v $1 $(pwd)/xrDriver.tar.gz
fi

echo "END - xr_driver_setup"