# 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.14.2
pkgver=${_base_ver}${_pre}
pkgrel=1
pkgdesc='Cross-platform IDE (mingw-w64)'
url='https://www.qt.io/'
install=qt-creator-${CARCH}.install
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
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.xz
        https://download.qt.io/official_releases/qtcreator/${pkgver%.*}/${pkgver}/${_pkgfqn}.tar.xz
        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-4.3.0-qbs-CONFIG-add-qbs_enable_project_file_updates.patch
        qt-creator-4.6.2-fix-clang-detect.patch)
noextract=(${_pkgfqn}.tar.xz)
sha256sums=('bd2bb62474f07466d8b59b0f751f29064deea3ed56173bce82548cb80e2d1a29'
            'b4eba129997fef75b811d0ba3ef573db23ba13e43f9dbdb0c27164ee551ba08d'
            '96c14f54577bf6cadf5c12018745666a9e99cd8d6a876c29a28b13599a8cb368'
            '3939880a0d3a092ca18cf7a4bdf8873f6bf2a4602940297381002f1dc9d83999'
            '879cc9448612f09a36272cb8e76c3b67c06154568721f5ab73dee7b7bebddd9b')

prepare() {
  [[ -d ${srcdir}/${_pkgfqn} ]] && rm -rf ${srcdir}/${_pkgfqn}
  tar -xf ${srcdir}/${_pkgfqn}.tar.xz -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-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 -p ${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
# 
