# Maintainer: Jeremy Drake <github@jdrake.com>

_realname=fritzing
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.9
pkgrel=7
pkgdesc="Electronic Design Automation software with a low entry barrier, suited for the needs of makers and hobbyists (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/fritzing/fritzing-app"
license=('spdx:GPL-3.0-or-later' 'CCPL:cc-by-sa-3.0')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libgit2"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qt5-serialport"
         "${MINGW_PACKAGE_PREFIX}-zlib")
# Fritzing updates its parts repo online from the master branch, so the latest
# master commit should be fine here
_partscommit='640fa25650211afccd369f960375ade8ec3e8653'
_appcommit='f0af53a9077f7cdecef31d231b85d8307de415d4'
# Fritzing seems not to be tagging its releases anymore.  https://aur.archlinux.org/packages/fritzing#comment-822991
source=("${_realname}"::"git+https://github.com/fritzing/fritzing-app.git#commit=$_appcommit"
        "${_realname}-parts"::"git+https://github.com/fritzing/fritzing-parts.git#commit=$_partscommit"
        0001-Fix-casts-from-pointer-to-long-to-size_t-instead.patch
        0002-qmake-fixes-for-mingw.patch
        0003-Look-in-.-share-fritzing-from-bin-dir-on-mingw.patch
        system-libgit2.patch)
sha256sums=('fc5336bc097456eebc4a6f98b92e48fe8836ed3c358c37433986c10d932692ba'
            '1d3f01786980d24e0b110bc4cdf5bd5c12e1b25ee7323d6db87f9f6c0f0defbc'
            '6d1a0d3a4daf775bff3f592e548430fccad8ac90f6573970bb4bf299ec344763'
            '3f101f91cdaf3ce40e74a38a48d94535a7dc1126126374ce043b946039b8f056'
            'b367dc68cc4dd4468650fe122117ae93b5d21b3301018c0920ce13219dd6f522'
            'b9fcdd41207a5d8fd67d44c5d4b3348e25464815431a91b61111b1fd91f1495f')

prepare() {
  cd "${srcdir}"/${_realname}

  git apply "${srcdir}"/0001-Fix-casts-from-pointer-to-long-to-size_t-instead.patch
  git apply "${srcdir}"/0002-qmake-fixes-for-mingw.patch
  git apply "${srcdir}"/0003-Look-in-.-share-fritzing-from-bin-dir-on-mingw.patch
  patch -Np1 -i "${srcdir}"/system-libgit2.patch
}

build() {
  cd "${srcdir}"/${_realname}

  lrelease phoenix.pro

  local qmake_config
  if check_option "debug" "y"; then
    qmake_config=debug
  else
    qmake_config=release
  fi

  MSYS2_ARG_CONV_EXCL="PREFIX=" \
  qmake \
    PREFIX=${MINGW_PREFIX} \
    CONFIG+=${qmake_config} \
    DEFINES+=QUAZIP_LIB \
    QMAKE_CFLAGS="-Wno-incompatible-pointer-types"

  make
}

package() {
  cd "${srcdir}"/${_realname}

  make INSTALL_ROOT="${pkgdir}" install -j1

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  install -Dm644 "${srcdir}"/${_realname}/LICENSE.* "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/

  rm -rf "${pkgdir}${MINGW_PREFIX}/share/fritzing/parts"
  git clone --single-branch -b makepkg --no-local --dissociate "${srcdir}"/${_realname}-parts "${pkgdir}${MINGW_PREFIX}/share/fritzing/parts"
  cd "${pkgdir}${MINGW_PREFIX}/share/fritzing/parts"
  git branch -M master
  git remote set-branches origin master
  git update-ref --no-deref refs/remotes/origin/master refs/remotes/origin/makepkg
  git branch --set-upstream-to origin/master
  git update-ref -d refs/remotes/origin/makepkg
  git remote set-head origin master
  git remote set-url origin https://github.com/fritzing/fritzing-parts.git
  "${pkgdir}${MINGW_PREFIX}/bin/Fritzing" -db "${pkgdir}${MINGW_PREFIX}/share/fritzing/parts/parts.db" -pp "${pkgdir}${MINGW_PREFIX}/share/fritzing/parts" -f "${pkgdir}${MINGW_PREFIX}/share/fritzing"
}
