#!/bin/sh

SCRIPT_DIR=$(dirname "$0")
WORKFLOW=$"KaleidoscopeIntegration.alfredworkflow"
APP_SUPPORT=$"$HOME/Library/Application Support/Kaleidoscope"
TOOL_ENDPATH="$APP_SUPPORT/$WORKFLOW"
TOOL_PATH="$TOOL_ENDPATH"
TOOL_SRCPATH="$SCRIPT_DIR/$WORKFLOW"

if [ -r "$TOOL_SRCPATH" ] ; then
  #echo "Source found $TOOL_SRCPATH"
  :
else
  echo "Could not read source $TOOL_SRCPATH" >&2
  exit 1
fi

if [ -r "$TOOL_PATH" ] ; then
  #echo "Could read target $TOOL_PATH"
  :
else
  echo "Could not read target $TOOL_PATH" >&2
  exit 1
fi

# note: when we drop support for Big Sur, we can replace xpath with `plutil -extract "version" raw -o - -`
#	   also note that -n of xpath is important for it not to do network checking for the DTDs

INSTALL_VERSION=$(unzip -p "$TOOL_PATH" "info.plist" | plutil -extract "version" xml1 -o - - | xpath -n -q -e "/plist/string/text()")
CURRENT_VERSION=$(unzip -p "$TOOL_SRCPATH" "info.plist" | plutil -extract "version" xml1 -o - - | xpath -n -q -e "/plist/string/text()")

#echo "Installed: $INSTALL_VERSION"
#echo "Current: $CURRENT_VERSION"

echo "$INSTALL_VERSION"

if [ $CURRENT_VERSION \> $INSTALL_VERSION ] ; then
  exit 210
else
  exit 0
fi
