#!/usr/bin/env bash

## Set up colors
red=`tput setaf 1`
green=`tput setaf 2`
orange=`tput setaf 3`
reset=`tput sgr0`

## Init functions
showhelp()
{
    echo " "
    echo "${green}Hesto Laravel Setup: Install Packages for ${orange}Laravel 5.3"
    echo "${reset}________________________________________________________"
    echo " "
    echo "${orange}Usage:"
    echo "${reset} laravel-setup {guard} {framework} [arguments]"
    echo " "
    echo "${orange}Example:"
    echo "${reset} laravel-setup admin"
    echo "${orange}Options:"
    echo "${green}  -h, --help${reset}                 Show brief help"
    echo "${green}  -d, --dev${reset}                  Use Composer to install on the develop branch"
    quit
}

abort()
{
    echo >&2 '
***********************************************
*** There was an error running this script. ***
***********************************************
'
    echo "An error occurred. Exiting..." >&2
    exit 1
}

quit()
{
    trap : 0
    exit 0
}

## Set up error handling
trap 'abort' 0

## Start script

PROJECTNAME=$1
FRAMEWORK=$2
PROJECTPATH="."
MESSAGE="Initial commit."
DEVELOP=false

### Check for project name and framework arguments; if none passed, show help
if [ $# -eq 1 ]
    then
        showhelp
fi

### Handle arguments
while [[ $# -gt 0 ]]
do
    key="$1"

    case $key in
        -h|--help)
            showhelp
            ;;
        -d|--develop|--dev)
            DEVELOP=true
            shift
            ;;
        *)
            ;;
    esac

    shift
done

echo "
***********************************************
Setup Laravel Application ${green}$PROJECTNAME${reset}
***********************************************
"
php artisan basis:env -f
php artisan basis:dependencies -f --framework=$FRAMEWORK
composer update
php artisan basis:register -f --framework=$FRAMEWORK
php artisan vendor:publish

php artisan make:auth
php artisan multi-auth:install $PROJECTNAME -f
sleep 2
php artisan basis:install $PROJECTNAME -f --framework=$FRAMEWORK
php artisan rbac:install

npm install

# This must execute later because migrations order is important
php artisan rbac:migration $PROJECTNAME -f

gulp

git init
git add .
git commit -m "$MESSAGE"

php artisan migrate

quit

echo "

***********************************************

You're ready to go!"

# End script; disable error handling
trap : 0
