# Maintainer: Maciej Suminski <maciej.suminski@cern.ch>
# Contributor: Unai Martinez-Corral <unai.martinezcorral@ehu.eus>
#
# Based on Arch ngspice PKGBUILD:
# https://git.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/ngspice
#   Contributor: Kyle Keen <keenerd@gmail.com>
#   Contributor: Abhishek Dasgupta <abhidg@gmail.com>
#   Contributor: Jason Taylor <jftaylor21@gmail.com>
#   Contributor: Luis Henrique <lmello.009@gmail.com>

_realname=ngspice
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=34
pkgrel=2
pkgdesc="Mixed-level/Mixed-signal circuit simulator based on Spice3f5, Cider1b1, and Xspice (mingw-w64)"
url='https://ngspice.sourceforge.io/'
license=('BSD')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-gcc"
  "${MINGW_PACKAGE_PREFIX}-pkg-config"
)
source=(
  "https://downloads.sourceforge.net/project/${_realname}/ng-spice-rework/${pkgver}/${_realname}-${pkgver}.tar.gz"
  "https://downloads.sourceforge.net/project/${_realname}/ng-spice-rework/${pkgver}/${_realname}-doc-${pkgver}.tar.gz"
  "no-explicit-lstdc++.patch"
)
sha256sums=(
  '2263fffc6694754972af7072ef01cfe62ac790800dad651bc290bfcae79bd7b5'
  'cbe7a42c4e3730ce86f2bdcc5c2eb95192e7daadf99e1c039b02dceaf7a12743'
  '26c34504e11ee753855da3b6d5ff44f0247dbcedb9eb6fe5efcf6f50f2927c97'
)

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/no-explicit-lstdc++.patch"
  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-shared-${CARCH}" ]] && rm -rf "${srcdir}/build-shared-${CARCH}"
  mkdir -p "${srcdir}/build-shared-${CARCH}" && cd "${srcdir}/build-shared-${CARCH}"

  # FS#45230, create so lib
  # shared lib sets flags and modifies headers, needs dedicated pass
  # adding --with-readline disables libngspice-0.dll
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    LDFLAGS+=" -lomp"
  fi
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-debug \
    --enable-openmp \
    --enable-xspice \
    --enable-cider \
    --with-ngshared
  make

  cd "${srcdir}/${_realname}-${pkgver}"
  [[ -d "${srcdir}/build-static-${CARCH}" ]] && rm -rf "${srcdir}/build-static-${CARCH}"
  mkdir -p "${srcdir}/build-static-${CARCH}" && cd "${srcdir}/build-static-${CARCH}"

  # gcc is being weird
  LDFLAGS+=" -lgdi32"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-debug \
    --enable-xspice \
    --enable-cider \
    --enable-openmp \
    --with-wingui
  make
}

package() {
  cd "${srcdir}/build-static-${CARCH}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # FIX: config.h should not be installed. This should be upstreamed/patched in the makefiles.
  rm "${pkgdir}${MINGW_PREFIX}"/include/config.h

  # pkg-conf file has to be copied manually.  Generated pkg-config file is the same for
  # static and shared builds.
  install -D -m644 "${srcdir}/build-shared-${CARCH}/ngspice.pc" "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/ngspice.pc"

  # additional files from the shared build have to be copied manually
  cd "${srcdir}/build-shared-${CARCH}"
  install -Dm755 src/.libs/libngspice-0.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install -Dm755 src/.libs/libngspice.dll.a "${pkgdir}${MINGW_PREFIX}/lib"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/src/include/ngspice/sharedspice.h "${pkgdir}${MINGW_PREFIX}"/include/ngspice/sharedspice.h
}
