#!/bin/bash

# utile pour l'output de l'erreur
rouge='\e[0;31m'
neutre='\e[0;m'
vert='\e[0;32m'

# fonction affichant un output d'aide au cas de mauvaise utilisation de la commande
usage() { 
    echo -e "\nUsage: ${vert}$0 [ -x PYTHONPATH ] {create,init,env,run,version,help} \n${neutre}" 1>&2;
    echo -e "create ...: create a new project in a new directory specified\n" 1>&2;
    echo -e "init: create a new project in current dir\n" 1>&2;
    echo -e "version: show the current version\n" 1>&2;
    echo -e "env: generate only a .env file\n" 1>&2;
    echo -e "run [--dev]: run the server, autoreload if --dev is specified\n" 1>&2;
    echo -e "help: show this current help\n" 1>&2;
    exit 1;
}

# Analyse des parametres entrée avec la commande
while getopts ":x:" option; do
    case "${option}" in
        x)
            python=${OPTARG}
            ;;
    esac
done
shift $((OPTIND-1))

# verification du parametre du path

if [ -z "${python}" ] ; then
    which python3 > /dev/null
    if [ ! $? -eq 0 ]; then
        echo -e "~\033[31m Wait, what? no python3 in path !! \033[0m\nTest python"
        which python > /dev/null
        if [ ! $? -eq 0 ]; then
            echo -e "~\033[31m Oh, Give up !! \033[0m\nNo python/python3 in path"
            exit 1
        else
            python="python"
        fi
    else
        python="python3"
    fi
fi

if [ "$1" == "version" ]; then
    echo `$python -c "import ampalibe;print(ampalibe.__version__)"`

elif [ "$1" == "env" ]; then
    if ! [ -f "core.py" ]; then
        >&2 echo -e "~ \033[31mERROR !! \033[0m | core.py not found\n~\033[36m TIPS 👌\033[0m ~\033[0m Please, go to your dir project.";
        exit 1
    fi
    `$python -c "import ampalibe.source;print(ampalibe.source.env)" > .env`;
    echo -e "~\033[32m 👌 \033[0m | Env file created";

elif [ "$1" == "create" ]; then
    if [ $# -eq 2 ]; then
        if [ -d "$2" ]; then
            >&2 echo -e "~\033[31m ERROR !!\033[0m ~ A folder $2 already exists"
            exit 1
        fi
        mkdir $2;
            echo -e "~\033[32m 👌 \033[0m | Creating $2 ...";
            echo -e ".env\n.env.bat\n__pycache__/\nngrok\nngrok.exe" > $2/.gitignore;
        `$python -c "import ampalibe.source;print(ampalibe.source.env)" > $2/.env`;
            echo -e "~\033[32m 👌 \033[0m | Env file created";
        `$python -c "import ampalibe.source;print(ampalibe.source.conf)" > $2/conf.py`;
            echo -e "~\033[32m 👌 \033[0m | Config file created";
        `$python -c "import ampalibe.source;print(ampalibe.source.core)" > $2/core.py`;
            echo -e "~\033[32m 👌 \033[0m | Core file created";
        mkdir -p $2/assets/{public,private}
            echo -e "~\033[32m 👌 \033[0m | Project Ampalibe created. \033[32mYoupii !!! 😎 \033[0m";
            echo -e "~\033[36m TIPS\033[0m |\033[0m Fill in .env file."
            echo -e "~\033[36m TIPS\033[0m |\033[36m cd $2 && ampalibe run\033[0m for lauching project."
    else
        echo -e "~\033[31m ERROR !!\033[0m | Incorrect number of args for create"
        usage
        exit 1
    fi
elif [ "$1" == "init" ]; then
    if [ $# -eq 1 ]; then
        echo -e ".env\n.env.bat\n__pycache__/\nngrok\nngrok.exe" >> .gitignore;
        `$python -c "import ampalibe.source;print(ampalibe.source.env)" > .env`;
            echo -e "~\033[32m 👌 \033[0m | Env file created";
        `$python -c "import ampalibe.source;print(ampalibe.source.conf)" > conf.py`;
            echo -e "~\033[32m 👌 \033[0m | Config file created";
        `$python -c "import ampalibe.source;print(ampalibe.source.core)" > core.py`;
            echo -e "~\033[32m 👌 \033[0m | Core file created";
        mkdir -p assets/{public,private}
            echo -e "~\033[32m 👌 \033[0m | Project Ampalibe initiated. \033[32mYoupii !!! 😎 \033[0m";
            echo -e "~\033[36m TIPS\033[0m |\033[36m ampalibe run\033[0m for lauching project."
    else
        >&2 echo -e "~\033[31m ERROR :(\033[0m | Incorrect number of args for init"
        exit 1
        
    fi
elif [ "$1" == "run" ]; then
    if ! [ -f "core.py" ]; then
        >&2 echo -e "~ \033[31mERROR !! \033[0m | core.py not found\n~\033[36m TIPS 👌\033[0m ~\033[0m Please, go to your dir project.";
        exit 1
    fi
    source .env;
        echo -e """\033[36m
                                                         0o
                                                          Oo
                                                         coooool
                                                        looooooool
                                                       loooooooooool
     _    __  __ ____   _    _     ___ ____  _____     looooooooooool
    / \  |  \/  |  _ \ / \  | |   |_ _| __ )| ____|    looooooooooool
   / _ \ | |\/| | |_) / _ \ | |    | ||  _ \|  _|       loooooooooool
  / ___ \| |  | |  __/ ___ \| |___ | || |_) | |___        looooooool
 /_/   \_\_|  |_|_| /_/   \_\_____|___|____/|_____|         oooooo \033[0m
 
"""
        echo -e "~\033[32m 👌\033[0m | Env Loaded";
        echo -e "~\033[32m 👌\033[0m | Ampalibe running...";

    if [ "$2" = "--dev" ]; then
        if [ $python = "python3" ]; then
            watchmedo auto-restart --patterns="*.py;.env" --recursive -- python3 -c 'import core;core.ampalibe.init.run(core.Configuration())'
        else
            watchmedo auto-restart --patterns="*.py;.env" --recursive -- python -c 'import core;core.ampalibe.init.run(core.Configuration())'
        fi

        exit
    fi

    if [ $python = "python3" ]; then
        python3 -c 'import core;core.ampalibe.init.run(core.Configuration())'
    else
        python -c 'import core;core.ampalibe.init.run(core.Configuration())'
    fi

else
    >&2 echo -e "~\033[31m ERROR !! \033[0m | Missing knowing argument"
    usage;
fi
