#!/usr/bin/env bash

set -o pipefail
set -o errexit
set -o nounset

# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)"

if [ -z "${CI:-}" ]; then
  echo "!! Running CI-style end-to-end tests but CI environment variable is not set"
  echo "It is generally ill-advised to run the full end-to-end suite on your local machine."
  echo "You are probably looking for this instead:"
  echo "  npm run test:endtoend:local"
  echo ""
  echo "Hit Ctrl+C to stop or Enter if you REALLY DO want to run the full thing."
  read
else
  echo travis_fold:start:endtoend_build_ci
fi

set -o xtrace

VERDACCIO_REGISTRY=http://localhost:4002
CURRENT_COMMIT="$(git rev-parse HEAD)"

cleanup() {
  rm -rf "${__root}/test/endtoend/node_modules"
  rm -rf "${__root}/test/endtoend/tmp"
  git reset
  git checkout $CURRENT_COMMIT

  if [ -n "${CI:-}" ]; then
    echo travis_fold:end:endtoend_build_ci
  fi
}
function on_exit() {
  # revert to public registry
  npm set registry https://registry.npmjs.org
  cleanup
}
trap on_exit EXIT

echo "Preparing for end to end test: copying static HTML and CSS, building JS"
rm -rf "${__root}/test/endtoend/node_modules"

# list of @uppy/* packages
PACKAGES="$(for pkg in packages/@uppy/*; do echo "${pkg#packages/}"; done)"

cleanup
# Initialise verdaccio storage path.
mkdir -p "${__root}/test/endtoend/tmp/verdaccio"

npm run build

# https://github.com/facebook/create-react-app/pull/4626
(cd && npm-auth-to-token -u user -p password -e user@example.com -r "$VERDACCIO_REGISTRY")

git checkout -b endtoend-test-build
# HACK this thing changes all the time for some reason on CI
# so I'll just ignore it…
git checkout -- package-lock.json

# Simulate a publish of everything, to the local registry,
# without changing things in git
ENDTOEND=1 lerna version prerelease --yes \
  --exact \
  --force-publish \
  --no-push
ENDTOEND=1 lerna publish from-git --yes \
  --registry="$VERDACCIO_REGISTRY" \
  --no-verify-access

# install all packages to the endtoend folder
# (Don't use the npm cache, don't generate a package-lock, don't save dependencies to any package.json)
pushd "${__root}/test/endtoend"
  PKG_VERSIONS="$(echo uppy $PACKAGES | sed -e 's/\S\+\b/&@latest/g')"
  echo '{}' > package.json # temp pkg.json to make sure npm installs deps in this folder
  npm install \
    --prefer-online \
    --registry "$VERDACCIO_REGISTRY" \
    --no-package-lock \
    --no-save \
    $PKG_VERSIONS
  rm package.json
popd

bash "${__dir}/endtoend-build-tests"

cleanup
