#!/bin/bash
set -e

#
# Install dependencies and upgrade to latest preset
#
# Run in docker
#


# ------------ functions ----------

# set variable:
#   branch
function get_git_branch() {
  if [[ -n $CI_COMMIT_REF_NAME ]]; then
    branch=$CI_COMMIT_REF_NAME
  else
    branch=`git symbolic-ref --short HEAD`
  fi
}

# input variable:
#   latest
function update_preset() {
  echo
  echo "Update preset to $latest"
  echo
  sed -i "s/\"@henson\/mrm-preset\".*\"/\"@henson\/mrm-preset\": \"$latest\"/" package.json
}

function apply_preset() {
  echo
  echo Applying Henson node.js preset...
  mrm all --preset @henson/mrm-preset
}

# ----------- main -------------

latest=`npm info @henson/mrm-preset version`
current=`grep '"@henson/mrm-preset"' package.json | cut -d\" -f4`
if [[ -n $current ]]; then
  if [[ $latest == $current ]]; then
    echo "Preset $current is up-to-date"
    yarn install
    apply_preset
  else
    echo "Preset $current is outdated, latest version is $latest"
    status="preset_outdated"

    get_git_branch
    if [[ $branch == "master" ]]; then
      # only upgrade preset in master branch

      # detect running environment
      namespace=`echo $CI_PROJECT_PATH | cut -d/ -f1`

      if [[ -n $namespace ]]; then
        # running in GitLab pipeline

        if [[ $namespace != "henson" ]]; then
          # GitLab personal pipeline
          echo "Error: Latest preset is not applied. Run 'yarn setup' in local dev environment to update."
          exit 2
        fi

        if [[ -n $CI_JOB_NAME ]] && [[ $CI_JOB_NAME == "scheduled_test" ]]; then
          # it's a scheduled pipeline
          update_preset
          status="preset_upgraded"
        fi
      else
        # running in local
        update_preset
        status="preset_upgraded"
      fi
    fi

    yarn install --check-files
    apply_preset

    if [[ $status == "preset_upgraded" ]]; then
      # clean node_modules folder to make sure new version of dependencies will be installed
      rm -rf node_modules/*
      yarn install --check-files
    fi
  fi
else
  status="no_preset"
  yarn install
fi


case "$status" in
  no_preset)
    echo
    echo -------------------- WARNING----------------------------------
    echo Preset is not installed.
    echo 'Please install it by running `yarn add --dev @henson/mrm-preset`'
    echo --------------------------------------------------------------
    ;;
  preset_outdated)
    echo
    echo -------------------- WARNING----------------------------------
    echo Latest preset is not applied.
    echo "If you need to upgrade to latest preset, change @henson/mrm-preset to $latest in package.json then run 'yarn setup'."
    echo --------------------------------------------------------------
    ;;
  preset_upgraded)
    echo
    echo -------------------- NOTICE ----------------------------------
    echo New preset $latest has been applied just now.
    echo
    echo Please commit all changes and new files,
    echo And record a line into CHANGELOG.md:
    echo "  * Apply preset $latest"
    echo --------------------------------------------------------------
    ;;
  *)
    echo "Setup done."
esac
