#!/usr/bin/env bash

## Sync your local environment with fresh data and code from Pantheon development environment.
##
## Usage: fin sync

# Abort if anything fails
set -e

start=$SECONDS

# SETTINGS
# See docksal.env 

# HELPERS

# Console colors
red=`tput setaf 1`
green=`tput setaf 2`
green_bg=`tput setaf 1; tput setab 2`
yellow=`tput setaf 3; tput setab 234`
NC=`tput sgr0`

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}"; }

# Windows check
is_windows ()
{
  local res=$(uname | grep 'CYGWIN_NT')
  if [[ "$res" != "" ]]; then
    return 0
  else
    return 1
  fi
}

# Execute
if [[ "${PROJECT_ROOT}" == "" ]]; then
  echo-red "\$PROJECT_ROOT is not set!"
  exit 1
fi

cd ${PROJECT_ROOT}

# Check for a clean repository
if ! [ -z "$(git status --untracked-files=no --porcelain)" ]; then
  # Uncommitted changes in tracked files
  echo -e
  echo -e  "${red}WARNING: You have uncommitted changes in your branch.${NC}"
  echo
  git status
  echo -e
  read -p "Continue (y/n)?" choice
    case "$choice" in 
      y|Y ) echo "Fair enough, let's do this!";;
      n|N ) echo "Exiting"; exit;;
      * ) echo "invalid";;
    esac
fi
# Project Files - Pull from Github
echo -e  "${green_bg} Get latest code ${NC}${yellow} Pulling ${REPO_BRANCH} branch from Github...${NC}"
git checkout ${REPO_BRANCH}
git pull 

# Fin Restart
echo -e  "${green_bg} Fin Restart ${NC}${yellow} Restart fin to get latest container updates...${NC}"
fin restart

# Composer Install
echo -e  "${green_bg} Composer Install ${NC}${yellow} to install latest updates from composer.json...${NC}"
fin composer install

if [ "$1" == 'pull' ]; then
  start2=$SECONDS
  echo -en "${green_bg} Fin Pull ${NC}${yellow} Starting Pantheon DB Pull... ${NC}"
  fin pull db -y
  ## Calculate Process time
  duration2=$(( SECONDS - start2 ))
fi

# Start database sync
if [ -n "$1" ] &&  [ "$1" != "pull" ]; then
  FILE="$1"
  echo -e "${green_bg} fin db import ${NC}${yellow} from "${FILE}" ${NC}"
  if [ -x "$(command -v pv)" ]; then
    pv ${FILE} | gunzip | fin db import
  else
    gunzip ${FILE} | fin db import
  fi
elif [ "$1" != "pull" ]; then
  echo -e "${green_bg} No DB Import ${NC}${yellow} (add file name to end of command)${NC}"
fi

# Cache Rebuild
echo -e "${green_bg} Drush cr ${NC}${yellow} Cache Rebuild...${NC}"
fin drush cr

# Config Import
echo -e "${green_bg} Drush cim -y ${NC}${yellow} Import config...${NC}"
fin drush cim -y

# Config Import
echo -e "${green_bg} Drush cim -y ${NC}${yellow} Import config again to be sure...${NC}"
fin drush cim -y

## Calculate Process time
duration=$(( SECONDS - start ))
## Echo Process time
echo -e
if [ "$1" == 'pull' ]; then
  echo -e "${green_bg} Pull runtime ${NC}${yellow} ${duration2} seconds ${NC}"
fi
echo -e "${green_bg} Sync runtime ${NC}${yellow} ${duration} seconds ${NC}"
echo -e
echo -e "${green_bg} SYNC COMPLETE! ${NC} "
echo -e "Open ${yellow}http://${VIRTUAL_HOST}${NC} in your browser to verify the setup.${NC}"
echo -e
echo -e "${green_bg} fin drush uli! ${NC} "
fin drush uli

exit

sleep 2
