#!/usr/bin/env bash
## Copyright (c) 2020 Aleksej Komarov
## SPDX-License-Identifier: MIT

set -e
shopt -s globstar
shopt -s expand_aliases

## Initial set-up
## --------------------------------------------------------

## Returns an absolute path to file
alias tgui-realpath="readlink -f"

## Fallbacks for GNU readlink
## Detecting GNU coreutils http://stackoverflow.com/a/8748344/319952
if ! readlink --version >/dev/null 2>&1; then
  if hash greadlink 2>/dev/null; then
    alias tgui-realpath="greadlink -f"
  else
    alias tgui-realpath="perl -MCwd -le 'print Cwd::abs_path(shift)'"
  fi
fi

## Find a canonical path to project root
base_dir="$(dirname "$(tgui-realpath "${0}")")/.."
base_dir="$(tgui-realpath "${base_dir}")"

## Make use of nvm if it exists
if [[ -e "${HOME}/.nvm/nvm.sh" ]]; then
  source "${HOME}/.nvm/nvm.sh"
fi

## Fall back to running Yarn from the repo
if ! hash yarn 2>/dev/null; then
  yarn_releases=("${base_dir}"/.yarn/releases/yarn-*.cjs)
  yarn_release="${yarn_releases[0]}"
  yarn() {
    node "${yarn_release}" "${@}"
  }
fi


## Functions
## --------------------------------------------------------

## Installs node modules
task-install() {
  cd "${base_dir}"
  yarn install
}

## Runs webpack
task-webpack() {
  cd "${base_dir}"
  yarn run webpack-cli "${@}"
}

## Runs a development server
task-dev-server() {
  cd "${base_dir}"
  yarn node --experimental-modules packages/tgui-dev-server/index.js "${@}"
}

## Run a linter through all packages
task-lint() {
  cd "${base_dir}"
  yarn run tsc
  echo "tgui: type check passed"
  yarn run eslint packages --ext .js,.cjs,.ts,.tsx "${@}"
  echo "tgui: eslint check passed"
}

task-test() {
  cd "${base_dir}"
  yarn run jest
}

## Mr. Proper
task-clean() {
  cd "${base_dir}"
  ## Build artifacts
  rm -rf public/.tmp
  rm -f public/*.map
  rm -f public/*.chunk.*
  rm -f public/*.bundle.*
  rm -f public/*.hot-update.*
  ## Yarn artifacts
  rm -rf .yarn/cache
  rm -rf .yarn/unplugged
  rm -rf .yarn/webpack
  rm -f .yarn/build-state.yml
  rm -f .yarn/install-state.gz
  rm -f .yarn/install-target
  rm -f .pnp.*
  ## NPM artifacts
  rm -rf **/node_modules
  rm -f **/package-lock.json
}

## Installs merge drivers and git hooks
task-install-git-hooks() {
  cd "${base_dir}"
  local git_root
  local git_base_dir
  git_root="$(git rev-parse --show-toplevel)"
  git_base_dir="${base_dir/${git_root}/.}"
  git config --replace-all merge.tgui-merge-bundle.driver \
    "${git_base_dir}/bin/tgui --merge=bundle %O %A %B %L"
  echo "tgui: Merge drivers have been successfully installed!"
}

## Bundle merge driver
task-merge-bundle() {
  local file_ancestor="${1}"
  local file_current="${2}"
  local file_other="${3}"
  local conflict_marker_size="${4}"
  echo "tgui: Discarding a local tgui build"
  ## Do nothing (file_current will be merged and is what we want to keep).
  exit 0
}


## Main
## --------------------------------------------------------

if [[ ${1} == "--merge"* ]]; then
  if [[ ${1} == "--merge=bundle" ]]; then
    shift 1
    task-merge-bundle "${@}"
  fi
  echo "Unknown merge strategy: ${1}"
  exit 1
fi

if [[ ${1} == "--install-git-hooks" ]]; then
  shift 1
  task-install-git-hooks
  exit 0
fi

if [[ ${1} == "--clean" ]]; then
  task-clean
  exit 0
fi

if [[ ${1} == "--dev" ]]; then
  shift
  task-install
  task-dev-server "${@}"
  exit 0
fi

if [[ ${1} == '--lint' ]]; then
  shift 1
  task-install
  task-lint "${@}"
  exit 0
fi

if [[ ${1} == '--lint-harder' ]]; then
  shift 1
  task-install
  task-lint -c .eslintrc-harder.yml "${@}"
  exit 0
fi

if [[ ${1} == '--fix' ]]; then
  shift 1
  task-install
  task-lint --fix "${@}"
  exit 0
fi

if [[ ${1} == '--test' ]]; then
  shift 1
  task-install
  task-test "${@}"
  exit 0
fi

## Analyze the bundle
if [[ ${1} == '--analyze' ]]; then
  task-install
  task-webpack --mode=production --analyze
  exit 0
fi

## Make a production webpack build
if [[ ${1} == '--build' ]]; then
  task-install
  task-webpack --mode=production
  exit 0
fi

## Make a production webpack build + Run eslint
if [[ -z ${1} ]]; then
  task-install
  task-lint --fix
  task-webpack --mode=production
  exit 0
fi

## Run webpack with custom flags
task-install
task-webpack "${@}"
