#!/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
# Command used to be installed
CMD_NAME="flyboat"
# Set destination folder and executable
LIB_DIR="/usr/local/lib/flyboat"
CMD_DIR="/usr/local/bin"
CMD_FILE="$CMD_DIR/$CMD_NAME"

RELEASE_BUILD_DIR="target/release"
ENVIRONMENTS_DIR="$HOME/.flyboat/env/"

# Check if already installed
if type "$CMD_NAME" &> /dev/null; then
    # Already installed
    echo -e "\033[33m$CMD_NAME is already installed in location:\033[0m"
    (set -x; whereis "$CMD_NAME")
    (set -x; "$CMD_NAME" --version)

    echo -e "\033[33mPlease uninstall it before installing a new version.\033[0m"
    echo -e "\033[33mYou can do this using the '$LIB_DIR/uninstall.sh' script.\033[0m"
    exit 1
fi

# Make sure the script is executed from the right folder
SOURCE_DIR=$(dirname "${BASH_SOURCE[0]}")
if [ "$(pwd)" != "$SOURCE_DIR" ]; then
    (set -x; cd "$SOURCE_DIR")
fi

# Check if Rust is installed
if ! type "cargo" &> /dev/null; then
    echo -e "\033[33mRust must be installed to compile this project.\033[0m"
    echo -e "\033[33mTo install go to: https://rust-lang.org/tools/install/ \033[0m"
fi

# Build project
cargo build --release

# Copy Folders
echo -e "\033[33mSudo permissions are needed for:\033[0m"
echo -e "\033[33m - Copy files into $LIB_DIR\033[0m"
echo -e "\033[33m - Copy executable to $CMD_FILE\033[0m"

# Use `set -x` to print commands to terminal for transparency
(set -x;
sudo mkdir -p "$LIB_DIR"
# Copy all files over (and preserver owner)
sudo cp -p "$(pwd)/$RELEASE_BUILD_DIR/$CMD_NAME" "$LIB_DIR/$CMD_NAME"
sudo cp -p "$(pwd)/$RELEASE_BUILD_DIR/$CMD_NAME.bash" "$LIB_DIR/$CMD_NAME.bash"
sudo cp -p "$(pwd)/$RELEASE_BUILD_DIR/_$CMD_NAME" "$LIB_DIR/$CMD_NAME.zsh"
sudo cp -p "$(pwd)/install.sh" "$LIB_DIR/"
sudo cp -p "$(pwd)/uninstall.sh" "$LIB_DIR/"
sudo cp -p "$(pwd)/README.md" "$LIB_DIR/"

# Create Symbolic links for the executables.
sudo ln -sf "$LIB_DIR/$CMD_NAME" "$CMD_FILE"
sudo chmod u+x "$LIB_DIR/$CMD_NAME"
# Creating environments folder
mkdir -p "$ENVIRONMENTS_DIR"
)

# Checking if correctly installed
INSTALLED_CHECK=$("$CMD_FILE" --help)
GET_VERSION=$("$CMD_FILE" --version)
if [[ $INSTALLED_CHECK == *"connect"* ]]; then
    echo -e "\033[1;32mCorrectly installed: $GET_VERSION\033[0m"
else
    echo -e "\033[1;31mCommand not correctly installed. Check if $CMD_DIR is part of '\$PATH'.\033[0m"
    if [[ $PATH == *"$CMD_DIR"* ]]; then
        echo "$CMD_DIR is part of \$PATH, so not sure what is wrong here. Please report this."
    else
        echo "$CMD_DIR is NOT part of \$PATH, adding to \$PATH"
        (set -x; export PATH="$PATH:$CMD_DIR")
        echo "You will have to reopen the terminal for the changes to take effect."
    fi
fi

echo -e "\033[33mIf you want to add a Environments you can add them in:\033[0m"
echo -e "\033[34m  \x1b]8;;file://$ENVIRONMENTS_DIR\x1b\\$ENVIRONMENTS_DIR\x1b]8;;\x1b\\ \033[0m"
