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

_realname=fritzing
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.6
pkgrel=1
pkgdesc="Electronic Design Automation software with a low entry barrier, suited for the needs of makers and hobbyists (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/fritzing/fritzing-app"
license=('GPL' 'CCPL:cc-by-sa-3.0')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
makedepends=("${MINGW_PACKAGE_PREFIX}-boost"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-libgit2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
# Fritzing updates its parts repo online from the master branch, so the latest
# master commit should be fine here
_partscommit='e79a69765026f3fda8aab1b3e7a4952c28047a62'
source=("${_realname}"::"git+https://github.com/fritzing/fritzing-app.git#tag=${pkgver}"
        "${_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=('SKIP'
            'SKIP'
            'f06de3f2cd642df40a6239db3736a0fe4f1ed6bc87a4d99a1fc893ae7e71fd67'
            'ccbb2b66b8cb7a7fd1a19117062b430753c265587d7dbf0d2289627fcedc3fa2'
            'c7e9352acc9e12fcb056dda9d32c9d594aa096b83f61725007ebb2a2cc91b50b'
            '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 CONFIG+=${qmake_config} DEFINES+=QUAZIP_LIB PREFIX=${MINGW_PREFIX}
  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"
}
