#!/bin/bash

CLI_VERSION="1.0.0"

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

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 [ "$arg" == "init" ] || [ "$arg" == "i" ]
    then
        if [ -d 'spry' ]
        then
            echo 'The folder "spry" already exists.'
            exit
        fi
        cp -r $SPRY_DIR/example_project spry
    fi

    if [ "$1" == "up" ] || [ "$1" == "u" ]
    then
        echo ''
        php $SPRY_DIR/src/SpryCli.php "$@"
        php -S localhost:8000 -t $2 &>/dev/null
        echo ''
        echo 'Spry Server Closed.'
        exit
    fi
done

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

exit
