#! /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"
fi

# Change the current directory to the home directory
cd $INFAMOUSDIR
echo "EXECUTED FROM $PWD"

# Check if the number of arguments is correct
if (($# != 2))
then
    echo " "
    echo "CANCELED"
    echo "  Wrong number of arguments : $# were provided but 2 are expected (project_name, team)"
    echo "  Syntax : ./tools/init_project.sh project_name team"
    echo "  Example:"
    echo "      To create a project under projects-fr :"
    echo "      ./tools/test.sh project_name fr"
    exit
else
    PROJECTDIR=$PWD/projects-$2/$1-$2 
fi

# Check if the project directory already exists
if [[ -d "$PROJECTDIR" ]]
then

    echo "$PROJECTDIR already exists"
    read -p "Are you deploying the project and want to set up the external directories? (Y/n): " answer
    if [[ "$answer" =~ ^[Yy]$ || -z "$answer" ]]; then
        mode="deploy"
    else 
        echo "CANCELED"
        echo "  Looks like you are trying to init an existing project."
        exit    
    fi

else # create the project directory
    echo " "
    echo "CREATE PROJECT DIRECTORY : $PROJECTDIR"
    mkdir -p $PROJECTDIR
    echo " CREATION OK"
    mode="init"
fi

if [[ $mode == "init" ]]
then 

    # Change the current directory to the project directory
    cd $PROJECTDIR

    # Create the README file
    echo "CREATE README FILE : $PROJECTDIR/README.md"
    touch ./README.md
    echo " CREATION OK"

    # Create the project sub-directories
    echo " "
    echo "CREATE PROJECT SUB-DIRECTORIES"
    echo "  notebooks : $PROJECTDIR/notebooks"
    mkdir ./notebooks 
    touch ./notebooks/.gitkeep
    echo "   CREATION OK"

    echo "  sql : $PROJECTDIR/sql"
    mkdir -p ./sql/_create
    touch ./sql/_create/.gitkeep
    echo "   CREATION OK"

    echo "  sh : $PROJECTDIR/sh"
    mkdir ./sh
    touch ./sh/.gitkeep

    cp $INFAMOUSDIR/tools/admin/run_template.sh ./sh/run.sh
    chmod +x ./sh/run.sh
    echo "   CREATION OK"

    echo "  modules : $PROJECTDIR/src"
    mkdir ./src
    touch ./src/.gitkeep
    echo "   CREATION OK"

    # Create the requirements file
    echo " "
    echo "CREATE REQUIREMENTS FILE : $PROJECTDIR/requirements.txt"
    echo " $PROJECTDIR/requirements.txt "
    touch ./requirements.txt
    echo "# The path in the find-links below is relative to the cea-infamous repository" >> ./requirements.txt
    echo "# It will be resolved correctly if the venv is created with the tools/create_env.sh script" >> ./requirements.txt
    echo "# Otherwise you should amend it with the correct path to the folder containing the djagitit dist file" >> ./requirements.txt
    echo "--find-links ./packages/dist" >> ./requirements.txt
    pkg_version=$(grep "version=" $INFAMOUSDIR/packages/djagitit/setup.py | sed "s/.*version='\([^']*\)'.*/\1/")
    echo "djagitit==$pkg_version" >> ./requirements.txt

fi

# Change the current directory to the home directory
cd $INFAMOUSDIR

# Create the external sub-directories
echo " "
echo "CREATE EXTERNAL LOCAL SUB-DIRECTORIES"

check_env_var() {

    local var_name="$1"

    while [[ -z "${!var_name}" ]]; do
        echo " "
        echo "  Environment variable $var_name is not set in the .env file"
        echo "  Please set the $var_name in $ENVFILEDIR/.env"
        read -p "Do you want to retry? (Y/n): " answer
        if [[ "$answer" =~ ^[Yy]$ || -z "$answer" ]]; then
            set -a
            source "$ENVFILEDIR/.env"
            set +a
        else
            return 1
        fi
    done
    return 0
}


check_and_create_dir() {

    local DIR="$1"

    if [[ -d "$DIR" ]]; then
        echo "  Directory $DIR already exists"
        return 1
    else 
        echo "  Directory $DIR not found"
        read -p "  Do you want to create this directory? (Y/n): " answer
        if [[ "$answer" =~ ^[Yy]$ || -z "$answer" ]]; then
            mkdir -p "$DIR"
            echo "  Directory $DIR created"
            echo "  "
            return 0
        else
            echo "  Directory $DIR not created"
            echo "  "
            return 1
        fi
    fi
}

check_env_var "LOGDIR"
if [[ $? -eq 0 ]]
then
    check_and_create_dir "$LOGDIR/$1-$2"
else 
    echo "  Log directory $LOGDIR/$1-$2 not created" 
fi

check_env_var "DATADIR"
if [[ $? -eq 0 ]]
then
    check_and_create_dir "$DATADIR/$1-$2"
    if [[ $? -eq 0 ]]
    then
        mkdir "$DATADIR/$1-$2/input"
        mkdir "$DATADIR/$1-$2/processed"
        mkdir "$DATADIR/$1-$2/errors"
        mkdir "$DATADIR/$1-$2/output"
    fi
else 
    echo "  Data directory $DATADIR/$1-$2 not created" 
fi

# Create the virtual environment
echo "CREATE PYTHON VIRTUAL ENVIRONMENT"
read -p "Do you want to create a python environment? (Y/n): " answer
if [[ "$answer" =~ ^[Yy]$ || -z "$answer" ]]
then
    ./tools/create_env.sh $1-$2
else 
    echo "  Virtual environment not created (but it might already exist)" 
fi
