# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=qt-creator
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
__pre=
_base_ver=4.8.0
pkgver=${_base_ver}${_pre}
pkgrel=1
pkgdesc='Cross-platform IDE (mingw-w64)'
url='https://www.qt.io/'
install=qt-creator-${CARCH}.install
arch=('any')
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-gcc"
         "${MINGW_PACKAGE_PREFIX}-make"
         "${MINGW_PACKAGE_PREFIX}-qbs")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('docs' 'staticlibs') # 'debug' '!strip')
optdepends=("${MINGW_PACKAGE_PREFIX}-gdb: for the debugger"
            "${MINGW_PACKAGE_PREFIX}-cmake: for cmake project support"
            "${MINGW_PACKAGE_PREFIX}-clang: for clang code model"
            #'openssh-askpass: for ssh support'
            #'git: for git support'
            #'mercurial: for mercurial support'
            #'bzr: for bazaar support'
            )
_pkgfqn="${_realname}-opensource-src-${_base_ver}${__pre}"
source=(#https://download.qt.io/development_releases/qtcreator/${_base_ver%.*}/${_base_ver}${__pre}/${_pkgfqn}.tar.gz
        https://download.qt.io/official_releases/qtcreator/${pkgver%.*}/${pkgver}/${_pkgfqn}.tar.gz
        qt-creator-3.2.0-Allow-static-clang-via-LLVM_STATIC-env-var.patch
        qt-creator-3.2.0-Allow-iOS-plugin-on-any-platform.patch
        qt-creator-3.2.0-Define-QBS_INSTALL_DIR-as-relative-path.patch
        qt-creator-3.2.0-MSYS2-autotools-run-shell-scripts-via-sh.patch
        qt-creator-3.2.0-autotools-allow-non-Automake-Makefile-in-projects.patch
        qt-creator-3.5.0-shellquote-declspec-dllexport-for-unix-shell.patch
        qt-creator-3.5.0-Hacky-fix-for-__GNUC_PREREQ-usage.patch
        qt-creator-3.5.1-Use-systemEnvironment-For-Start-and-Debug-External-Application.patch
        qt-creator-4.3.0-qbs-CONFIG-add-qbs_enable_project_file_updates.patch
        qt-creator-4.6.2-fix-clang-detect.patch)
noextract=(${_pkgfqn}.tar.gz)
sha256sums=('4c4813454637141a45aa8f18be5733e4ba993335d95940aadf12fda66cf6f849'
            'b4eba129997fef75b811d0ba3ef573db23ba13e43f9dbdb0c27164ee551ba08d'
            '96c14f54577bf6cadf5c12018745666a9e99cd8d6a876c29a28b13599a8cb368'
            '24f93d9d888027915ce3eb387d2d52123fd625ae1b3cd31adb6500952bc6e192'
            'c818e1b43a351e8e274255af5cda05ee3a5191766a916ada9cf3bc3091de059e'
            'eb5be20983a75c2aa8329981e9c45daf90db92cee463688ab383636dff27c62a'
            'e95ab2c936ff3e4fd09e044ff119829d72037a40de4bc6c93e90c52eb14a75fc'
            '5989243f07e4f72f33bf66ab67534b21e2e9d96993fc1814b96b2c6c80afbacf'
            'bf6472c3b6a65e6fd0e0e822d25b5f4e849262ab3586f8ac16e7767d2bd641eb'
            '3939880a0d3a092ca18cf7a4bdf8873f6bf2a4602940297381002f1dc9d83999'
            '879cc9448612f09a36272cb8e76c3b67c06154568721f5ab73dee7b7bebddd9b')

prepare() {
  [[ -d ${srcdir}/${_pkgfqn} ]] && rm -rf ${srcdir}/${_pkgfqn}
  tar -xzf ${srcdir}/${_pkgfqn}.tar.gz -C $srcdir || true

  cd ${srcdir}/${_pkgfqn}

  #patch -p1 -i "${srcdir}"/qt-creator-3.2.0-Allow-static-clang-via-LLVM_STATIC-env-var.patch
  patch -p1 -i "${srcdir}"/qt-creator-3.2.0-Allow-iOS-plugin-on-any-platform.patch
  patch -p1 -i "${srcdir}"/qt-creator-3.2.0-Define-QBS_INSTALL_DIR-as-relative-path.patch

  #patch -p1 -i "${srcdir}"/qt-creator-3.2.0-MSYS2-autotools-run-shell-scripts-via-sh.patch
  #patch -p1 -i "${srcdir}"/qt-creator-3.2.0-autotools-allow-non-Automake-Makefile-in-projects.patch

  patch -p1 -i "${srcdir}"/qt-creator-3.5.0-shellquote-declspec-dllexport-for-unix-shell.patch
  # Arguably mingw-w64 headers should define __GNUC_PREREQ (_mingw.h defines __MINGW_GNUC_PREREQ)
  # .. otherwise perhaps GCC should define this always (on GNU/Linux, glibc's features.h defines it)
  #patch -p1 -i "${srcdir}"/qt-creator-3.5.0-Hacky-fix-for-__GNUC_PREREQ-usage.patch
  # Not applicable anymore
  # patch -p1 -i "${srcdir}"/qt-creator-3.5.1-Use-systemEnvironment-For-Start-and-Debug-External-Application.patch

  patch -p1 -i "${srcdir}"/qt-creator-4.3.0-qbs-CONFIG-add-qbs_enable_project_file_updates.patch

  patch -p1 -i "${srcdir}"/qt-creator-4.6.2-fix-clang-detect.patch

  # Use system installed qbs
  rm -rf src/shared/qbs
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}
  export LLVM_INSTALL_DIR=${MINGW_PREFIX}
  export QTC_FORCE_CLANG_LIBTOOLING=1
  export QBS_INSTALL_DIR=${MINGW_PREFIX}
  if check_option "debug" "y"; then
    _config_variant=debug
  else
    _config_variant=release
  fi
  ${MINGW_PREFIX}/bin/qmake.exe ../${_pkgfqn}/qtcreator.pro CONFIG+=${_config_variant}
  make ${_config_variant}
  make docs
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make INSTALL_ROOT="${pkgdir}${MINGW_PREFIX}" install
  make INSTALL_ROOT="${pkgdir}${MINGW_PREFIX}" install_docs

  install -Dm644 ${srcdir}/${_pkgfqn}/LICENSE.GPL3-EXCEPT ${pkgdir}${MINGW_PREFIX}/share/licenses/qtcreator/LICENSE.GPL3-EXCEPT
}

# Please leave this block here as I use
# it to quickly make and test changes.
# return 0
# Debugger ProjectExplorer 
# ALLGOOD="yes"
# PLUGINS="Debugger"
# for PLUGIN in $PLUGINS; do
#   PLUGINLC=$(echo ${PLUGIN} | tr 'A-Z' 'a-z')
#   pushd src/build-x86_64-w64-mingw32/src/plugins/${PLUGINLC}
#     PATH=/mingw64/bin:"$PATH" make || ALLGOOD="no"
#   popd
# done
# if [ "${ALLGOOD}" = "yes" ]; then
# (
#   QTCPID=$(ps --windows | grep creator | awk '{ print $1; }')
#   while [ -n "$QTCPID" ]; do
#     cmd.exe /c "taskkill /PID $QTCPID"
#     sleep 1
#     QTCPID=$(ps --windows | grep creator | awk '{ print $1; }')
#   done
#   for PLUGIN in $PLUGINS; do
#     PLUGINLC=$(echo ${PLUGIN} | tr 'A-Z' 'a-z')
#     pushd src/build-x86_64-w64-mingw32/src/plugins/${PLUGINLC}
#       PATH=/mingw64/bin:$PATH make
#       cp ../../../lib/qtcreator/plugins/${PLUGIN}d.dll /mingw64/lib/qtcreator/plugins/
#     popd
#   done
#   PATH=/mingw64/bin:$PATH /mingw64/bin/qtcreator &
#   )
# fi
# 
