#/bin/bash

set -exou

mkdir qt-build
pushd qt-build

if [[ $(uname) == "Linux" ]]; then
   USED_BUILD_PREFIX=${BUILD_PREFIX:-${PREFIX}}
   echo USED_BUILD_PREFIX=${BUILD_PREFIX}

   ln -s ${GXX} g++ || true
   ln -s ${GCC} gcc || true
   ln -s ${USED_BUILD_PREFIX}/bin/${HOST}-gcc-ar gcc-ar || true

   export LD=${GXX}
   export CC=${GCC}
   export CXX=${GXX}
   export PKG_CONFIG_EXECUTABLE=$(basename $(which pkg-config))
   export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/lib64/pkgconfig/"

   chmod +x g++ gcc gcc-ar
   export PATH=${PWD}:${PATH}

   # Copy XCB headers to PREFIX
   #cp -r /usr/include/xcb $PREFIX/include
   NPROC=$(nproc)

  ../configure -prefix ${PREFIX} \
             -libdir ${PREFIX}/lib \
             -bindir ${PREFIX}/bin \
             -headerdir ${PREFIX}/include/qt \
             -archdatadir ${PREFIX} \
             -datadir ${PREFIX} \
             -I ${PREFIX}/include \
             -L ${PREFIX}/lib \
             -L ${BUILD_PREFIX}/${HOST}/sysroot/usr/lib64 \
             QMAKE_LFLAGS+="-Wl,-rpath,$PREFIX/lib -rpath-link,$PREFIX/lib -L$PREFIX/lib" \
             -opensource \
             -nomake examples \
             -nomake tests \
             -gstreamer 1.0 \
             -confirm-license \
             -system-libjpeg \
             -system-libpng \
             -system-zlib \
             -xcb \
             -xcb-xlib \
             -bundled-xcb-xinput
fi

if [[ $(uname) == "Darwin" ]]; then

  export PATH=${BUILD_PREFIX}/bin:${PATH}

  #export CROSS_COMPILE=$HOST
  #export AR=$(basename ${AR})
  #export RANLIB=$(basename ${RANLIB})
  #export STRIP=$(basename ${STRIP})
  export OBJDUMP=$(basename ${OBJDUMP})
  #export CC=$(basename ${CC})
  #export CXX=$(basename ${CXX})

  # Let Qt set its own flags and vars
  for x in OSX_ARCH CFLAGS CXXFLAGS LDFLAGS
  do
      echo $x # unset $x
   done

    #cp "${RECIPE_DIR}"/xcrun .
    #cp "${RECIPE_DIR}"/xcodebuild .
    # Some test runs 'clang -v', but I do not want to add it as a requirement just for that.
    #ln -s "${CXX}" ${HOST}-clang || true
    # For ltcg we cannot use libtool (or at least not the macOS 10.9 system one) due to lack of LLVM bitcode support.
    #ln -s "${LIBTOOL}" libtool || true
    # Just in-case our strip is better than the system one.
    #ln -s "${STRIP}" strip || true
    #chmod +x ${HOST}-clang libtool strip xcrun xcodebuild
    # Qt passes clang flags to LD (e.g. -stdlib=c++)
    #export LD=${CXX}

  NPROC=$CPU_COUNT

  PLATFORM=""
  if [[ $(arch) == "arm64" ]]; then
    PLATFORM="-device-option QMAKE_APPLE_DEVICE_ARCHS=arm64 -device-option QMAKE_MACOSX_DEPLOYMENT_TARGET=11.1"
    # temporary fix for apple
    # PLATFORM="${PLATFORM} -skip qtwebengine -no-assimp"
  fi
  export WEBENGINE_OSX_SDK_PRODUCT_VERSION="10.15.0"

  ../configure -prefix ${PREFIX} \
             -device-option CROSS_COMPILE=${BUILD_PREFIX}/bin/${HOST}- \
             -device-option CLANG_VERSION=12.0.0 \
             -device-option BUILD_PREFIX=${BUILD_PREFIX} \
             -device-option QMAKE_XCODE_VERSION="10.0.0" \
             -device-option WEBENGINE_OSX_SDK_PRODUCT_VERSION="10.15.0" \
             -libdir ${PREFIX}/lib \
             -bindir ${PREFIX}/bin \
             -headerdir ${PREFIX}/include/qt \
             -archdatadir ${PREFIX} \
             -datadir ${PREFIX} \
             $PLATFORM \
             -I ${PREFIX}/include \
             -L ${BUILD_PREFIX}/lib \
             -R ${BUILD_PREFIX}/lib \
             -L ${PREFIX}/lib \
             -R $PREFIX/lib \
             -release -opensource -confirm-license -shared \
             -nomake examples \
             -nomake tests \
             -verbose \
             -system-libjpeg \
             -system-libpng \
             -system-zlib \
             -system-sqlite -plugin-sql-sqlite \
             -qt-freetype \
             -no-framework \
             -qt-pcre -dbus-runtime \
             -optimize-size \
             -skip qtwebengine \
             -sdk macosx11.1

  #sed -i.bat -E "s:/usr/local/Cellar/llvm/10.0.0_3:${BUILD_PREFIX}:" ./qt-build/qttools/src/qdoc/qtqdoc-config.pri
fi

# 7/23/2021 PJY: Specifying the CPATH and LD_LIBRARY_PATH seem to be important for
# finding additional headers and libs (e.g. event.h).
CPATH=$PREFIX/include LD_LIBRARY_PATH=$PREFIX/lib make -j${CPU_COUNT} || exit 1

make install

# Remove XCB headers
rm -rf $PREFIX/include/xcb

echo "copy configuration file qt.conf ..."
# Add qt.conf file to the package to make it fully relocatable
cp -f "${RECIPE_DIR}"/qt.conf "${PREFIX}"/bin/
if [[ ${HOST} =~ .*darwin.* ]]; then
  pushd ${PREFIX}
  sed -i '' "s|${BUILD_PREFIX}|\$\${BUILD_PREFIX}|g" mkspecs/modules/*.pri || true
  sed -i '' "s|${PREFIX}|\$\${PREFIX}|g" mkspecs/modules/*.pri || true
  sed -i '' "s|${BUILD_PREFIX}|\$\${BUILD_PREFIX}|g" mkspecs/*.pri || true
  sed -i '' "s|${PREFIX}|\$\${PREFIX}|g" mkspecs/*.pri || true
  popd
fi

