#!/bin/sh

SCRIPT_DIR=$(dirname "$0")
SCRIPT=$"Diff Unsaved Changes in Kaleidoscope.sh"
APP_SUPPORT=$"$HOME/Library/Application Support/BBEdit/Scripts"
ALT_APP_SUPPORT=$"$HOME/Library/Containers/com.barebones.bbedit/Data/Library/Application Support/BBEdit/Scripts"
TOOL_ENDPATH="$APP_SUPPORT/$SCRIPT"
ALT_TOOL_ENDPATH="$ALT_APP_SUPPORT/$SCRIPT"

TOOL_SRCPATH="$SCRIPT_DIR/$SCRIPT"

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

if [ -r "$APP_SUPPORT" ] ; then
	echo "Found regular scripts folder in $APP_SUPPORT"
	TOOL_PATH="$TOOL_ENDPATH"
elif [ -r "$ALT_APP_SUPPORT" ] ; then
	echo "Found alternative scripts folder in $ALT_APP_SUPPORT"
	TOOL_PATH="$ALT_TOOL_ENDPATH"
else
	echo "Could not find either scripts folder\n$TOOL_PATH\n$ALT_TOOL_ENDPATH" >&2
	exit 1
fi

if [ -r "$TOOL_PATH" ] ; then
    echo "Destination found $TOOL_PATH"
else
	echo "Script not installed in destination $TOOL_PATH" >&2
	exit 1
fi

 INSTALL_VERSION=$(sed -n "s/# Build \([0-9]\)/\1/p" "$TOOL_PATH")
 CURRENT_VERSION=$(sed -n "s/# Build \([0-9]\)/\1/p" "$TOOL_SRCPATH")

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

# echo "$INSTALL_VERSION"

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