#!/bin/bash

# Stop when error occurred
set -e

# Check if file is executed from a symbolic link or directly
if [ -L ${BASH_SOURCE[0]} ] ; then
    echo "Please run this command directly, not as a symbolic link."
    exit 1
fi

CMD_NAME="flyboat"
CODE_DIR=$(dirname "${BASH_SOURCE[0]}")
DEFAULT_CODE_DIR="/usr/local/lib/flyboat"
CMD_DIR="/usr/local/bin/$CMD_NAME"

# Check if actually installed
if ! type "$CMD_NAME" > /dev/null; then
    echo "Command '$CMD_NAME' does not seem to be installed. Please check"
    exit 1
fi

if [ "$CODE_DIR" != "$DEFAULT_CODE_DIR" ]; then
    echo "It looks like this code is not installed in the default directory."
    echo " - Current directory: $CODE_DIR"
    echo " - Default directory: $DEFAULT_CODE_DIR"
    echo "Uninstall is getting canceled to prevent deleting files accidentally."
    echo "If you still want to uninstall it, run '$DEFAULT_CODE_DIR/uninstall.sh'."
    exit 1
fi

echo "The following file/directories will be removed:"
echo " - $CMD_DIR"
echo " - $DEFAULT_CODE_DIR"
read -p "Continue (y/n)?" choice
case "$choice" in
  y|Y ) echo "Removing files";;
  n|N ) echo "Nothing removed"; exit 0;;
  * ) echo "Nothing removed"; exit 0;;
esac

# Print command being executed
set -x

# Remove executable link
sudo rm "$CMD_DIR"
# Remove all code
sudo rm -r "$DEFAULT_CODE_DIR"

echo "Your environments have not been removed."
echo "To remove them run 'rm -r ~/.flyboat'"
