#!/usr/bin/env bash

#
# install-qt-macos
#
# Copyright (C) 2022 by RStudio, PBC
#
# Unless you have received this program directly from RStudio pursuant
# to the terms of a commercial license agreement with RStudio, then
# this program is licensed to you under the terms of version 3 of the
# GNU Affero General Public License. This program is distributed WITHOUT
# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
#
#

# Install minimal Qt bits needed to build via command-line.
# For developer environments, better to install using Qt's online installer.

set -e

if [ -z "$QT_VERSION" ]; then
    QT_VERSION=5.12.8
fi
if [ -z "$QT_SDK_DIR" ]; then

   # Qt install root if path not supplied
   QT_SDK_DIR=~/Qt${QT_VERSION}

   # other locations checked for existing installation
   QT_SDK_DIR2=~/Qt/Qt${QT_VERSION}
   QT_SDK_DIR3=~/Qt/${QT_VERSION}
fi

if [ ! -e "$QT_SDK_DIR" ] && [ ! -e "$QT_SDK_DIR2" ] && [ ! -e "$QT_SDK_DIR3" ]
then
   command -v 7z >/dev/null 2>&1 || { echo >&2 "7z command required to install Qt but not found (brew install p7zip)."; exit 1; }
   echo ----------------------------------------------------------------------
   echo "                            *** Important ***"
   echo ----------------------------------------------------------------------
   echo " The minimal Qt installer should only be used on a build machine. For"
   echo " a developer machine use the Qt online installer from qt.io or"
   echo " MaintenanceTool.app from existing Qt folder to install $QT_VERSION."
   echo ----------------------------------------------------------------------
   echo " Pausing for 15 seconds... (Ctrl+C to interrupt)"
   echo ----------------------------------------------------------------------
   sleep 15
   echo "Installing minimal Qt $QT_VERSION SDK"
   ../common/install-qt.sh --version $QT_VERSION --directory ${QT_SDK_DIR} \
      qtbase \
      qtwebengine \
      qtwebchannel \
      qtquick \
      qtquickcontrols2 \
      qtdeclarative \
      qtsensors \
      qtlocation \
      qtsvg \
      qtxmlpatterns \
      qttools \
      qttranslations \
      qtwebsockets
else
   echo "Qt $QT_VERSION SDK already installed"
fi
