#!/bin/sh
isdev=false
for arg in "$@"; do
  if [ "$arg" = "dev" ]; then
    isdev=true
  fi;
done

INSTALLBASE=~/.local/share/gnome-shell/extensions
UUID=`grep -E '^[ ]*"uuid":' _build/metadata.json | sed 's@^[ ]*"uuid":[ ]*"\(.\+\)",[ ]*@\1@'`
UUIDDEV=`grep -E '^[ ]*"uuid-dev":' _build/metadata.json | sed 's@^[ ]*"uuid-dev":[ ]*"\(.\+\)",[ ]*@\1@'`
GV=`gnome-shell --version | awk '{print $3}'`

if $isdev
then
    sed -i "s/${UUID}/${UUIDDEV}/g" _build/metadata.json
    rm -fr "${INSTALLBASE}/${UUID}"
    UUID=$UUIDDEV
else
    rm -fr "${INSTALLBASE}/${UUIDDEV}"
fi

if [ -d "${INSTALLBASE}/${UUID}" ]; then
  rm -rf "${INSTALLBASE}/${UUID}/*"
else
  mkdir -p "${INSTALLBASE}/${UUID}"
fi

cp -r _build/* "${INSTALLBASE}/${UUID}/"

gnome-extensions info "${UUID}"
INSTALLED="$?"

if [ "$INSTALLED" -eq 0 ]; then
  echo "reset"
  gnome-extensions reset "${UUID}"
  echo "enable"
  gnome-extensions enable "${UUID}"

  echo "Please restart GNOME Shell if you are on X11 or logout and login again when you use Wayland."
else
  echo "You probably install the extension for the first time. Please restart GNOME Shell or Logout and enable the extension via the Extensions app or extensions.gnome.org."
fi

exit 0;
