#!/bin/bash
set -e

#==============================================================================
# Installer script for Python utilities
# Assumes Python 3 is already installed and on the path.
#==============================================================================
INSTALLER_DIR="$(cd $(dirname $0); pwd)"
ENV_DIR="${HOME}/py699-moma"

PRE_MOMA_ENV_DIR="${HOME}/py699"
PRE_MOMA_ACTIVATE="${PRE_MOMA_ENV_DIR}/bin/activate"

# This is a place for users to stick their own custom Python tools. This
# directory will be included in $PATH, but will not be clobbered by the
# 699util install process.
CUSTOM_SCRIPT_DIR="${HOME}/custom_699_utils"

#==============================================================================
# Eliminate Old Installation
#==============================================================================

if command -v deactivate 2>&1 > /dev/null; then
    echo "Deactivating old Python virtualenv." 1>&2
    deactivate
fi

if command -v uninstall_699util.sh 2>&1 > /dev/null; then
    echo "Uninstalling previous version of 699util."
    uninstall_699util.sh -y
fi

# This shouldn't happen after the uninstaller, but just in case
if [[ -d "${ENV_DIR}" ]]; then
    echo "Removing old Python tools installation." 1>&2
    rm -r "${ENV_DIR}"
fi

#==============================================================================
# Set up the Virtual env
#==============================================================================

echo "Creating new Python Tools virtualenv area" 1>&2
# The echo helps with some woes about the wildcard semantics with quotes

python3.9 -m venv "${ENV_DIR}"
ACTIVATE="${ENV_DIR}/bin/activate"

# Cleanup
cd "${INSTALLER_DIR}"

#==============================================================================
# Install Packages
#==============================================================================

echo "Installing Python packages" 1>&2
source "${ACTIVATE}"

pip install --no-index --find-links="${INSTALLER_DIR}/" -r requirements.txt
pip install --no-index --find-links="${INSTALLER_DIR}/" 699util

# Copy c699util and its dependencies
cp c699util.py  ${ENV_DIR}/lib
cp _c699util.so ${ENV_DIR}/lib
cp lib* ${ENV_DIR}/lib
if [[ "$(uname)" == "Darwin" ]]; then
    cp -R Qt*framework ${ENV_DIR}/lib
fi

deactivate

#==============================================================================
# Create custom script directory, if necessary
#==============================================================================

echo "Setting up directory for user's custom scripts" 1>&2
if [ ! -d "$CUSTOM_SCRIPT_DIR" ]; then
    mkdir "${CUSTOM_SCRIPT_DIR}"
    chmod 700 "${CUSTOM_SCRIPT_DIR}"
fi

#==============================================================================
# Configure Bash Profile
#==============================================================================

echo "Configuring Bash" 1>&2
if [[ -f "${HOME}/.bashrc" ]]; then
    CONFIG_FILE="${HOME}/.bashrc"
elif [[ -f "${HOME}/.bash_profile" ]]; then
    CONFIG_FILE="${HOME}/.bash_profile"
elif [[ -f "${HOME}/.profile" ]]; then
    CONFIG_FILE="${HOME}/.profile"
else
    #
    # MCP Note: fresh installs of OSX don't have a .bashrc, nor a
    #           .bash_profile, nor a .profile. So, just make a .bash_profile
    #           file if need be. (Note that OSX won't source a .bashrc file
    #           by default, but it *will* source a .bash_profile.)
    #
#    echo "Error: Could not find BASH configuration file." 1>&2
#    exit 1
    CONFIG_FILE="${HOME}/.bash_profile"
    touch "$CONFIG_FILE"
    chmod 775 "$CONFIG_FILE"
fi

if ! grep '__699ENV__' "${CONFIG_FILE}" > /dev/null; then
    echo >> "${CONFIG_FILE}"
    echo "# __699ENV__  BEGIN (added by install699util.sh)" >> "${CONFIG_FILE}"
    echo "export ENV699='${ENV_DIR}' # __699ENV__" >> "${CONFIG_FILE}"
    echo "export ENV699_CONFIG='${CONFIG_FILE}' # __699ENV__" >> "${CONFIG_FILE}"

    # Make it so that c699util can be found when a Python scripts imports it
    echo "export PYTHONPATH=\"\${PYTHONPATH}\"":${ENV_DIR}/lib"  # __699ENV__" >> "${CONFIG_FILE}"

    echo "export PATH=\"\${PATH}\"":${CUSTOM_SCRIPT_DIR}"  # __699ENV__" >> "${CONFIG_FILE}"
    echo "source '${ACTIVATE}'  # __699ENV__" >> "${CONFIG_FILE}"
    echo "alias gomoma='source ${ACTIVATE}' # __699ENV__" >> "${CONFIG_FILE}"
    echo "alias gosam='source ${PRE_MOMA_ACTIVATE}' # __699ENV__" >> "${CONFIG_FILE}"
    echo "alias goladee='source ${PRE_MOMA_ACTIVATE}' # __699ENV__" >> "${CONFIG_FILE}"
    echo "alias gomaven='source ${PRE_MOMA_ACTIVATE}' # __699ENV__" >> "${CONFIG_FILE}"
    echo "source bashconfig699.sh  # __699ENV__" >> "${CONFIG_FILE}"
    echo "# __699ENV__ END" >> "${CONFIG_FILE}"
fi


echo "699util python tools are now installed and configured." 1>&2
echo "Please restart your shell." 1>&2
