#!/bin/bash

SCRIPT_NAME=`basename "$0"`
SCRIPT_DIR=`dirname "$0"`


# Mac OS checks
if [ $(uname) == "Darwin" ]; then
    # Install MySQL
    if ! brew ls --versions mysql; then
        # MySQL is not installed
        brew install mysql
    fi

    export CFLAGS="-I/usr/local/opt/mysql-client/include/mysql"
    export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
    export ORACLE_HOME="$HOME/Downloads/instantclient_12_1"
    export LD_LIBRARY_PATH="$HOME/Downloads/instantclient_12_1:$LD_LIBRARY_PATH"
    export PATH="$PATH:$HOME/Downloads/instantclient_12_1"
else
    # Linux checks
    export CFLAGS="-I/usr/include/mysql"
    export LDFLAGS="-L/usr/lib64/mysql"
    export ORACLE_HOME="/home/ssa/share/oracle/instantclient_12_1"
    export LD_LIBRARY_PATH="/home/ssa/share/oracle/instantclient_12_1:$LD_LIBRARY_PATH"
    export PATH="$PATH:/home/ssa/share/oracle/instantclient_12_1"
fi


# Check if we're in a virtualenv
if [ "$VIRTUAL_ENV" = "" ]; then
    # Make sure we have an appropriate venv available here: 
    if [ ! -d venv ]; then
	echo "No virtual environment detected, creating a basic one."
	python3.8 -m venv venv
	source ./venv/bin/activate
	pip install --upgrade pip
	pip install -r deployment/requirements.txt
    else
	# If we have one, activate it so we can use invoke: 
	. ./venv/bin/activate
    fi
else
    # If you're in a virtual environment, exit with a message
    echo "Deactivate your virtual environment, deployment requires"
    echo "its own venv in which to operate."
    exit -2
fi

# go
cd ${SCRIPT_DIR}/deployment
invoke $*
