#!/bin/zsh

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

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

# Copy integration script to application support
cp "$TOOL_SRCPATH" "$TOOL_PATH" || { echo "Copy to '$TOOL_PATH' failed"; exit 1; }

# append configuration to .lldbinit
LLDBINIT_PATH="$HOME/.lldbinit"

if [ ! -r "$LLDBINIT_PATH" ] ; then
  touch "$LLDBINIT_PATH"
fi

## remove our integration if there
zsh "$SCRIPT_DIR/uninstall_lldb"

# we use zsh's :A functionality here to resolve links, because the previously used readlink -f is not available on Big Sur (see #1476 and #1173)
LLDBINIT_PATH=${LLDBINIT_PATH:A}

## integrate

# use echo instead of cat here, because in the sandbox cat fails (see #1715)
echo command script import \""$TOOL_PATH"\" >> "$LLDBINIT_PATH"
