#!/usr/bin/env bash
## Initialize stack and site (full reset)
##
## Usage: fin init

# Abort if anything fails
set -e

#-------------------------- Helper functions --------------------------------

# Console colors
red='\033[0;31m'
green='\033[0;32m'
green_bg='\033[42m'
yellow='\033[1;33m'
NC='\033[0m'

echo-red () { echo -e "${red}$1${NC}"; }
echo-green () { echo -e "${green}$1${NC}"; }
echo-green-bg () { echo -e "${green_bg}$1${NC}"; }
echo-yellow () { echo -e "${yellow}$1${NC}"; }

#-------------------------- Execution --------------------------------

# Copy env file
ENV_FILE="${PROJECT_ROOT}/.env"
if [ ! -f ${ENV_FILE} ]; then
  cp ${PROJECT_ROOT}/.env.dist ${ENV_FILE}
fi

# Stack initialization
echo -e "${green_bg}${NC}${green} Initializing stack...${NC}"
fin up

# Build site
echo -e "${green_bg}${NC}${green} Building site...${NC}"
fin build-site

# Install site
echo -e "${green_bg}${NC}${green} Installing dependencies...${NC}"
fin install

echo -en "${green_bg} DONE! ${NC} "
echo -e "Open ${yellow}http://${VIRTUAL_HOST}${NC} in your browser to verify the setup."

#-------------------------- END: Execution --------------------------------
