#!/bin/bash

#define parameters which are passed in.
#COMPOSER_PATH="php /usr/local/bin/composer.phar"
COMPOSER_PATH="composer"
PROJECTNAME=$1
DEFAULTEXT="app"
LOCALEXT=${2:-${DEFAULTEXT}}
DBNAME="${PROJECTNAME//-/_}"

PASSWORD_SUFFIX=$(echo $PROJECTNAME| cut -c1-3)
DB_NAME_SUFFIX="_dev"

DBNAME="$DBNAME$DB_NAME_SUFFIX"

DB_USER="evoluted"
DB_PASS="f9rm8uev"
DB_HOST="database.cd.evoluted.net"

command -v mysql >/dev/null 2>&1 || { echo >&2 "It looks like you dont have mysql installed yet. Run brew install mysql and then try again."; exit 1; }

# Takes 1 argument. Aborts the script if there's a false negative.
function mysql_db_exists () {
  local DBNAME="$1"

  # Underscores are treated as wildcards by mysqlshow.
  # Replace them with '\\_'. One of the underscores is consumed by the shell to keep the one mysqlshow needs in tact.
  ESCAPED_DB_NAME="${DBNAME//_/\\\_}"
  RESULT="$(mysqlshow -u $DB_USER -h $DB_HOST -p$DB_PASS "$DBNAME" 2>&1)"; EXITCODE=$?
  #RESULT="$(mysqlshow -u$DB_USER --host=$DB_HOST "$ESCAPED_DB_NAME" 2>&1)"; EXITCODE=$?
  if echo "$RESULT" | grep -iq "Tables"; then
    true
  else
    false
  fi
}

# Run checks before starting
printf "Checking if database already exists..."
if mysql_db_exists "$DBNAME"; then
  echo "Installer has been aborted as the Database isn't unique"
  exit 0
else
  printf "ok\n"
fi

printf "Checking if folder exists..."
# Check if folder already exists
if [ -d "./$PROJECTNAME" ]; then
  printf "\nInstaller has been aborted. Folder already exists"
  exit 0
else
    printf "ok\n"
fi

printf "Checks are complete, starting install...\n"

####################
#   CONFIGURE DB   #
####################
printf "Creating database..."
mysql -u $DB_USER -h $DB_HOST -p$DB_PASS -e "CREATE DATABASE ${DBNAME} /*\!40100 DEFAULT CHARACTER SET utf8 */;"
if mysql_db_exists "$DBNAME"; then
  printf "ok\n"
else
  printf "failed\n"
fi

printf "Installing Pyro via composer..."
COMPOSER_OUTPUT="$($COMPOSER_PATH create-project pyrocms/pyrocms $PROJECTNAME 3.* 2>&1)"; EXITCODE=$?
#mkdir $PROJECTNAME
if [ $EXITCODE == 0 ]; then
    printf "done.\n"
else
    printf "failed.\n"
    echo $COMPOSER_OUTPUT
    exit 1
fi

printf "Setting up .env file..."

#define the template.
ENVFILE=$(cat <<-END
INSTALLED=false
APP_ENV=local
APP_KEY=zfesbnTkXvooWVcsKMw2r4SmPVNGbFoS
APP_DEBUG=true
DEBUG_BAR=true
DB_CONNECTION=mysql
DB_HOST=$DB_HOST
DB_PORT=3306
DB_DATABASE=$DBNAME
DB_USERNAME=$DB_USER
DB_PASSWORD=$DB_PASS
APPLICATION_NAME=$PROJECTNAME
APPLICATION_DOMAIN=http://$PROJECTNAME.$LOCALEXT
APPLICATION_REFERENCE=$PROJECTNAME
DEFAULT_LOCALE=en
APP_TIMEZONE=Europe/London
APP_URL=http://$PROJECTNAME.$LOCALEXT
LAZY_TRANSLATIONS=true
ADMIN_EMAIL=admin@evoluted.net
ADMIN_USERNAME=admin
ADMIN_PASSWORD=admin$PASSWORD_SUFFIX
END
)

destdir=./$PROJECTNAME/.env

if [ -d "./$PROJECTNAME" ]; then
echo "$ENVFILE" > "$destdir"
fi

printf "done\n"

cd ./$PROJECTNAME

php artisan key:generate

printf "Running Pyro install...\n"
php artisan install --ready

printf "All ready to go! The site is now available at http://$PROJECTNAME.$LOCALEXT \n"
printf "If you are using Laravel Valet you now need to link your project. From your project directory run valet link to make the above url accessible."
