#! /bin/bash

# Change the current directory to the script directory
BASEDIR=$(dirname "$0")
cd $BASEDIR
# Look for the .env file
while [[ "$PWD" != "$HOME" && ! -e .env ]]; do cd ..; done
if [[ -e .env ]]; then
  ENVFILEDIR=$PWD
  set -a; source .env; set +a
  echo "Using .env file found in $PWD"
else
  echo "CANCELED"
  echo "  No .env file found in the directory tree."
  echo "  Please setup the .env file and retry"
  exit
fi

# Change the current directory to the home directory
cd $INFAMOUSDIR || { echo "wrong path for INFAMOUSDIR, correct your .env file before retrying"; exit; }
echo "EXECUTED FROM $PWD"
# Check if the correct number of arguments is provided
if (($# != 1))
then
    echo " "
    echo "CANCELED"
    echo "  Wrong number of arguments : $# were provided but 1 is expected (project_name)"
    echo "  Syntax : ./tools/create_env.sh project_name"
    echo "  Example:"
    echo "      To create an environment for the project 'my-awesome-project-cea' :"
    echo "      ./tools/create_env.sh my-awesome-project-cea"
    exit
fi
# Check if the ENVDIR variable is set
while [[ ! $ENVDIR ]]
    do
        echo " "
        echo "  environment directory path (ENVDIR) is not set in .env file"
        echo "  Please set the path in $ENVFILEDIR/.env"
        read -p "Do you want to retry? (Y/n): " answer
        if [ "$answer" = "Y" ]; 
        then
            set -a; source $ENVFILEDIR/.env; set +a
        else
            exit
        fi
    done
# Change the current directory to the virtual environments directory
mkdir -p $ENVDIR
cd $ENVDIR
# Check if the project environment already exists
if [[ -d "$1-env/" ]]
then
    echo " "
    echo "CANCELED"
    echo "  $1-env already exists in $ENVDIR"
    exit
fi
# Create the project environment
echo " "
echo "CREATE VENV $1-env IN $ENVDIR : "
# Create a new virtual environment for the project
if command -v python3.10 &> /dev/null
then
    pycommand="python3.10"
elif command -v python &> /dev/null
then
    pycommand="python"
    python_version=$(python --version)
    echo "Running with Python version: $python_version"
    echo " If Python version < 3.10, you might have troubles with some dependencies"
else
    echo "Python not found. Please install Python 3.10 or higher."
    exit
fi
$pycommand -m venv $1-env
echo "  CREATION OK"
# Activate the new virtual environment
echo " "
echo "ENV ACTIVATION : "
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "win32" ]]
then
    ostype="win"
    echo "Running on Windows"
    source $1-env/Scripts/activate
else
    ostype="unix"
    echo "Running on non-Windows OS"
    source $1-env/bin/activate
fi
# Upgrade pip in the new virtual environment
echo " "
echo "PIP UPGRADE : "
if [ $ostype == "win" ]
then    
    $pycommand -m pip install --upgrade pip
else
    pip install --upgrade pip
fi
# Change the current directory back to the base directory
cd $INFAMOUSDIR
 # The following line of code is using the 'find' command to search for all files named 'requirements.txt' in directories that start with "projects-" under the home directory.
echo " "
requirements_files=($(find $INFAMOUSDIR/projects-*/$1 -name requirements.txt 2>/dev/null))
echo "Found ${#requirements_files[@]} requirements files"
for i in "${!requirements_files[@]}"; 
    do
        echo "$((i+1)): ${requirements_files[$i]}"
    done
# Check if there are any requirements files
if [ ${#requirements_files[@]} -eq 0 ];
then
    file_num=0
# If there is only one requirements file ask the user if he wants to install it
elif [ ${#requirements_files[@]} -eq 1 ];
then
    read -p "Do you want to install the packages from this requirements file? (Y/n): " answer
    if [ "$answer" = "Y" ];
    then
        file_num=1
    else
        file_num=0
    fi
# If there are multiple requirements files ask the user which one he wants to use
else
    read -p "Enter the number of the requirements file you want to use or 0 to skip: " file_num
fi
# If no requirements files are found or the user wants to skip
if [ $file_num -eq 0 ];
then
  # Ask the user if he wants to install the latest version of djagitit
  read -p "Do you want to install the latest version of djagitit? (Y/n): " answer
  if [ "$answer" = "Y" ];
  then
    pip install djagitit --find-links $INFAMOUSDIR/packages/dist
  else
    echo "No installation performed in the environment"
  fi
# If the user wants to install from a requirements file, perform the installation  
else
    pip install -r ${requirements_files[$((file_num-1))]}
fi
# Deactivate the virtual environment
echo " "
echo "ENV DEACTIVATION : "
deactivate
echo "  $1-env deactivated"
