#!/bin/bash -ex
#
# This script is run on travis for the install stage of mac builds.
#

function version_ge() { test "$(printf "%s\n%s" "$1" "$2" | sort -rV | head -n 1)" == "$1"; }

#debug failed install
function debug() {
  cat "${CACHEDIR}/qt-${QT_VERSION}.env"
  find "${CACHEDIR}" -maxdepth 3 -ls
  cat "${CACHEDIR}/Qt/InstallationLog.txt"
  cat "${CACHEDIR}/Qt/components.xml"
  echo "$1" >&2
  exit 1
}

# validate install
function validate() {
  (
    set +e
    # shellcheck source=/dev/null
    source "${CACHEDIR}/qt-${QT_VERSION}.env"
    if [ "$(qmake -query QT_INSTALL_BINS)" != "${QTDIR}/bin" ]; then
      debug "ERROR: unexpected Qt location."
    fi
    if [ "$(qmake -query QT_VERSION)" != "${QT_VERSION}" ]; then
      debug "ERROR: wrong Qt version."
    fi
  )
}

QT_VERSION=${1:-6.2.4}
METHOD=${2:-aqt}

# our expectation is that install-qt creates $QTDIR, $QTDIR/bin.
CACHEDIR=${HOME}/Cache
if [ "$METHOD" = "aqt" ]; then
  if version_ge "${QT_VERSION}" 6.1.2; then
    QTDIR=${CACHEDIR}/Qt/${QT_VERSION}/macos
  else
    QTDIR=${CACHEDIR}/Qt/${QT_VERSION}/clang_64
  fi
else
  QTDIR=${CACHEDIR}/Qt/${QT_VERSION}/clang_64
fi

if [ -d "${QTDIR}/bin" ]; then
  echo "Using cached Qt."
  echo "If you need to clear the cache see"
  echo "https://docs.travis-ci.com/user/caching/#Fetching-and-storing-caches."
  if [ "${TRAVIS_EVENT_TYPE}" = "cron" ]; then
    # the cache is being used.  modify it to reset expiration date.
    date > "${CACHEDIR}/timestamp"
  fi
  if [ -f "${CACHEDIR}/timestamp" ]; then
    echo -n "Cache timestamp: "
    cat "${CACHEDIR}/timestamp"
  fi
else
  rm -fr "${CACHEDIR}"
  mkdir -p "${CACHEDIR}"
  pushd "${CACHEDIR}"

  if [ "$METHOD" = "artifactory" ]; then
    (
      # Do not leak keys
      set +x
      if [ -z "${ARTIFACTORY_API_KEY}" ]; then
        echo "An untrusted build cannot load cache from artifactory."
        echo "A PR from a forked repo will be an untrusted build."
        echo "The cache can be loaded from a trusted build of the default branch."
        echo "A PR from the original repo will be trusted, but has it's own cache."
        echo "However, when that PR is merged it will build cache for the default branch."
        echo "Also, the cron job should rebuild the cache for the default branch, if necessary,"
        echo "once that flavor of build in .travis.yml makes it into the default branch."
        exit 1
      else
        archive=qt-${QT_VERSION}-release-macos.tar.xz
        curl -u "${ARTIFACTORY_USER}:${ARTIFACTORY_API_KEY}" "${ARTIFACTORY_BASE_URL}/${archive}" -o "/tmp/${archive}"
        tar -x -J -f "/tmp/${archive}"
        echo "export PATH=${QTDIR}/bin:\$PATH" > "${CACHEDIR}/qt-${QT_VERSION}.env"
        rm -f "/tmp/${archive}"
      fi
     )
  elif [ "$METHOD" = "aqt" ]; then
    pip3 install aqtinstall>=2.0.0
    "${TRAVIS_BUILD_DIR}/tools/ci_install_qt.sh" mac "${QT_VERSION}" clang_64 "${CACHEDIR}/Qt"
    echo "export PATH=${QTDIR}/bin:\$PATH" > "${CACHEDIR}/qt-${QT_VERSION}.env"
  else
    # install-qt creates the install at $PWD/Qt.
    QT_VERSION_SHORT=${QT_VERSION//./}
    QT_CI_PACKAGES=qt.qt5.${QT_VERSION_SHORT}.clang_64,qt.qt5.${QT_VERSION_SHORT}.qtwebengine QT_CI_DOWNLOADER="wget -nv -c" PATH=${TRAVIS_BUILD_DIR}/tools/qtci:${PATH} install-qt "${QT_VERSION}"
    rm "${CACHEDIR}"/qt-opensource*.dmg
  fi
  popd
  validate
fi
