#!/usr/bin/env bash
#
# Setup Script
#
# Runs all the needed commands to set up a developer's system to run this app.
# This script should be run on your continuous integration server.
#
# Customize this as your app grows.

# Ensure Elixir is installed
command -v elixir >/dev/null 2>&1 || {
  echo "This app requires Elixir, but it was not found on your system."
  echo "Install it using the instructions at http://elixir-lang.org"
  exit 1
}

<%= if assigns[:assets] do %>
# Ensure Node.js is installed
command -v npm >/dev/null 2>&1 || {
  echo "This app requires Node.js to build assets, but it was not found on your system."
  echo "Install it using the instructions at http://nodejs.org"
  exit 1
}
<% end %>

echo "----------------------------------------------------------"
echo "Ensuring Hex is installed..."
echo "----------------------------------------------------------"
mix local.hex --force
mix local.rebar --force
echo "Done!"

echo "----------------------------------------------------------"
echo "Installing Mix dependencies..."
echo "----------------------------------------------------------"

mix deps.get || { echo "Mix dependencies could not be installed!"; exit 1; }
MIX_ENV=test mix deps.compile --warnings-as-errors || { echo "Dependencies could not compile for test environment!"; exit 1; }
MIX_ENV=dev mix deps.compile --warnings-as-errors || { echo "Dependencies could not compile for dev environment!"; exit 1; }

<%= if assigns[:assets] do %>
echo "----------------------------------------------------------"
echo "Installing Node.js dependencies (for assets)"
echo "----------------------------------------------------------"

cd apps/<%= @project_name %>_web/assets/ && {
  npm install || { echo "Node dependencies could not be installed!"; exit 1; };
  cd -;
}
<% end %>

<%= if assigns[:ecto] do %>
if [ ! $CI ]; then
  echo "----------------------------------------------------------"
  echo "Setting up database..."
  echo "----------------------------------------------------------"

  cd apps/<%= @project_name %>/ && {
    mix ecto.setup || { echo "Database could not be set up!"; exit 1; }
    cd -;
  }
fi
<% end %>

echo "----------------------------------------------------------"
echo "Running tests to verify setup is complete..."
echo "----------------------------------------------------------"
bin/test || { exit 1; }

echo "----------------------------------------------------------"
echo "Setup complete!"
echo "----------------------------------------------------------"
