#!/usr/bin/env bash

set -euo pipefail

# Check arguments

readonly ARCH=$1

if [ -z "$ARCH" ]
then
  echo "Error: No architecture was specified. Please specify either 'i686' or 'x86_64', case sensitive, as the first argument to the script."
  exit 1
fi

if [[ "$ARCH" != "i686" ]] && [[ "$ARCH" != "x86_64" ]]
then
  echo "Error: Incorrect architecture was specified. Please specify either 'i686' or 'x86_64', case sensitive, as the first argument to the script."
  exit 1
fi

set -x

# Get packages

apt-get update
apt-get install -y --no-install-recommends \
                   build-essential \
                   ca-certificates \
                   libtool \
                   pkg-config \
                   wget

if [[ "$ARCH" == "i686" ]]
then
  apt-get install -y --no-install-recommends \
                    g++-mingw-w64-i686 \
                    gcc-mingw-w64-i686
elif [[ "$ARCH" == "x86_64" ]]
then
  apt-get install -y --no-install-recommends \
                    g++-mingw-w64-x86-64 \
                    gcc-mingw-w64-x86-64
fi

# Use all cores for building

MAKEFLAGS=j$(nproc)
export MAKEFLAGS

mkdir -p ./build/$ARCH
cd ./build/$ARCH

wget "https://download.qt.io/development_releases/qt/5.12/5.12.0-alpha/single/qt-everywhere-src-5.12.0-alpha.tar.xz"
tar -xf qt-everywhere-src-5.12.0-alpha.tar.xz
rm qt-everywhere-src-5.12.0-alpha.tar.xz
cd qt*

./configure -prefix "${PWD}/prefix" \
            -release \
            -shared \
            -device-option CROSS_COMPILE=$ARCH-w64-mingw32- \
            -xplatform win32-g++ \
            -opensource -confirm-license \
            -pch \
            -nomake examples \
            -nomake tools \
            -nomake tests \
            -skip 3d \
            -skip activeqt \
            -skip androidextras \
            -skip canvas3d \
            -skip charts \
            -skip connectivity \
            -skip datavis3d \
            -skip declarative \
            -skip doc \
            -skip gamepad \
            -skip graphicaleffects \
            -skip imageformats \
            -skip location \
            -skip macextras \
            -skip multimedia \
            -skip networkauth \
            -skip purchasing \
            -skip quickcontrols \
            -skip quickcontrols2 \
            -skip remoteobjects \
            -skip script \
            -skip scxml \
            -skip sensors \
            -skip serialbus \
            -skip serialport \
            -skip speech \
            -skip translations \
            -skip virtualkeyboard \
            -skip wayland \
            -skip webchannel \
            -skip webengine \
            -skip webglplugin \
            -skip websockets \
            -skip webview \
            -skip x11extras \
            -skip xmlpatterns \
            -no-dbus \
            -no-icu \
            -no-compile-examples \
            -qt-libjpeg \
            -qt-libpng \
            -qt-zlib \
            -qt-pcre \
            -opengl desktop

make
make install
