#!/bin/sh 

# Message our PPID to the parent so we can wait for it.
echo "PID: $$"

KS_BIN_DIR="$1"
INSTALL_DIR="$2"

if [ ! -e "$KS_BIN_DIR/ksdiff" ]; then
	echo "The ksdiff tool could not be located inside Kaleidoscope"
	exit 1
fi

# Remove KS1 KSDiff files if they exist.
rm -f $INSTALL_DIR/ksdiff-svnwrapper
rm -f $INSTALL_DIR/ksdiff-wrapper


# Looking for an installed ksdiff binary
if [ -e "$INSTALL_DIR/ksdiff" ] ; then
	echo "$INSTALL_DIR/ksdiff exists, removing..."
	rm -f "$INSTALL_DIR/ksdiff"
fi

# Looking for an installed ksdiff symlink (in this case it was a broken symlink, because the test with -e returned false)
if [ -L "$INSTALL_DIR/ksdiff" ] ; then
	echo "$INSTALL_DIR/ksdiff symlink exists, removing..."
	rm -f "$INSTALL_DIR/ksdiff"
fi

echo "Installing ksdiff in $INSTALL_DIR..."

# creates INSTALL_DIR if it doesn't exist, doesn't overwrite it if it does exist.
if (/bin/mkdir -p "$INSTALL_DIR" && /bin/ln -s "$KS_BIN_DIR/ksdiff" "$INSTALL_DIR/ksdiff"); then
	# Success, everything installed fine
	echo "ksdiff was successfully installed in $INSTALL_DIR"
	exit 0
fi

echo "ksdiff could not be installed"
exit 4
