#!/bin/sh

SCRIPT_DIR=$(dirname "$0")
FILENAME=$"lldb_ksdiff.py"
APP_SUPPORT=$"$HOME/Library/Application Support/Kaleidoscope"
TOOL_ENDPATH="$APP_SUPPORT/$FILENAME"
TOOL_PATH="$TOOL_ENDPATH"
TOOL_SRCPATH="$SCRIPT_DIR/$FILENAME"

LLDBINIT_PATH="$HOME/.lldbinit"

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

INSTALL_VERSION=$(grep -Eo 'v[0-9]+(\.[0-9]+)+' "$TOOL_PATH" | sed 's/^v//')
CURRENT_VERSION=$(grep -Eo 'v[0-9]+(\.[0-9]+)+' "$TOOL_SRCPATH" | sed 's/^v//')

# echo "installed: $INSTALL_VERSION, current: $CURRENT_VERSION"

if [ $CURRENT_VERSION \> $INSTALL_VERSION ] ; then
#  echo "Needs update"
  exit 210
else
  if [ -r "$LLDBINIT_PATH" ] ; then
#    echo "Could read .lldbinit in $LLDBINIT_PATH"
    grep "$TOOL_ENDPATH" "$LLDBINIT_PATH" >/dev/null && exit 0
  else
    echo "Could not read .lldbinit $LLDBINIT_PATH" >&2
  fi
fi

exit 1


