#!/bin/bash

CLI_VERSION="0.9.6"

BIN_DIR=$(dirname $0)

if [ -f $BIN_DIR/spry.php ]
then
   SPRY_DIR=$(dirname $BIN_DIR)
else
   SPRY_DIR=$(dirname $BIN_DIR)/ggedde/spry-cli
fi

SPRY_DIR_EXISTS=""

for arg in "$@"
do
    if [ "$arg" == "version" ] || [ "$arg" == "v" ] || [ "$arg" == "-v" ] || [ "$arg" == "--version" ] || [ "$arg" == "-h" ] || [ "$arg" == "--help" ]
    then
        echo "Spry-Cli -v $CLI_VERSION"
    fi

    if [ "$1" == "new" ] || [ "$1" == "n" ]
    then

        if [ $2 ] && [ "$2" != "" ]
        then

            if [ -d $2 ]
            then

                printf "\e[31;4mERROR:\e[0m directory '$2' already exists."
                echo ''
                exit

            fi

            mkdir $2

            if [ ! -d $2 ]
            then

                printf "\e[31;4mERROR:\e[0m Error creating directory '$2'."
                echo ''
                exit

            fi

            cd $2

            $BIN_DIR/spry init public

            exit

        else

            printf "\e[31;4mERROR:\e[0m Missing project name."
            echo ''
            exit

        fi

    fi

    if [ "$1" == "init" ] || [ "$1" == "i" ]
    then

        if [ -d 'spry' ]
        then
            SPRY_DIR_EXISTS="true"
            printf "The folder 'spry' already exists \e[91mignoring\e[0m."
            echo ''

            if [ ! $2 ] || [ "$2" == "" ]
            then

                exit
                
            fi

        else
            cp -r $SPRY_DIR/example_project spry
        fi

        if [ ! -f 'composer.json' ]
        then
            echo 'Downloading Composer Packages...'
            composer require ggedde/spry
        fi


        if [ $2 ] && [ "$2" != "" ]
        then

            if [ "$SPRY_DIR_EXISTS" == "" ]
            then
                php $SPRY_DIR/src/SpryCli.php "$@"
            fi

            if [ -d $2 ]
            then
                printf "The folder '$2' already exists \e[91mignoring\e[0m."
                echo ''
            else
                mkdir $2
                echo "<?php

include dirname(__DIR__).'/vendor/autoload.php';
include dirname(__DIR__).'/spry/init.php';

" >> "$2/index.php";

                echo "Folder '$2' created."
                echo "File '$2/index.php' created."

            fi

            echo ''
            exit

        fi
    fi

    if [ "$1" == "up" ] || [ "$1" == "u" ]
    then

        PORT=8000

        if [ $2 ] && [ "$2" != "" ]
        then
            PORT=$2
        fi

        if [ $3 ] && [ "$3" != "" ] && [ -d $3 ]
        then
            echo ''
            php $SPRY_DIR/src/SpryCli.php "$@"
            php -S localhost:$PORT -t $3 &>/dev/null
            echo ''
            echo 'Spry Server Closed.'
            exit
        fi

        if [ ! $3 ] || [ $3 == "" ]
        then
            if [ -f "$PWD/vendor/autoload.php" ] && [ -f "$PWD/spry/init.php" ]
            then
                echo ''
                php $SPRY_DIR/src/SpryCli.php "$@"
                php -S localhost:$PORT $SPRY_DIR/server/up.php &>/dev/null
                echo ''
                echo 'Spry Server Closed.'
                exit
            else
                printf "\e[31;4mERROR:\e[0m Spry Server Could not find vendor/autoload.php or spry/init.php\nMake sure you run 'spry up' in your root Project directory that contains both vendor/autoload.php and spry/init.php\nOr run it with your endpoint folder 'spry up {public_dir}' that contains your index.php file."
                echo ''
                exit
            fi
        fi
    fi
done

php $SPRY_DIR/src/SpryCli.php "$@"
echo ''

exit
