# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=f2c
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=20200425
pkgrel=1
pkgdesc='Fortran to C code translator (mingw-w64)'
url='https://www.netlib.org/f2c/index.html'
license=('custom')
makedepends=('unzip')
options=('strip' 'staticlibs')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
options=('!buildflags' 'staticlibs')
source=(f2c-src.tgz::"https://www.netlib.org/f2c/src.tgz"
        libf2c-${pkgver}.zip::"https://www.netlib.org/f2c/libf2c.zip"
        20051004-add-ofiles-dep.patch
        20090407-cygwin-build.patch
        libf2c-20110801-64bit-long.patch
        libf2c-1.patch)
noextract=("libf2c-${pkgver}.zip")
sha256sums=('ae76b86c5ce887ad508a761509a762adf5ccb8d9ea0c40901264aa1b994878db'
            'ca404070e9ce0a9aaa6a71fc7d5489d014ade952c5d6de7efb88de8e24f2e8e0'
            '3f688297b45515c1e386bb7f94ad36bf0f6a3497a0baff759b54d943cbec7d8f'
            '7fdf61868985bd116307e0039967666845c74c09dc3a3293974ff34d374ac9c6'
            '919698643f12b2fc4b9f4ebac395a8b2c9c01e83028907621b95dee8db4d5653'
            '0fc51325773b0e74c89aafd3b0331b76ad9890b060ac2d0fa2fc6aa72b93a249')

prepare() {
  cd "${srcdir}"
  #mv src f2c

  mkdir libf2c
  unzip -q libf2c-${pkgver}.zip -d libf2c

  # mxe patch
  pushd libf2c
    patch -p1 -i "${srcdir}"/20051004-add-ofiles-dep.patch
    patch -p1 -i "${srcdir}"/20090407-cygwin-build.patch
    patch -p1 -i "${srcdir}"/libf2c-20110801-64bit-long.patch
    patch -p1 -i "${srcdir}"/libf2c-1.patch
  popd
}

build() {
  cd "${srcdir}"
  pushd libf2c
    local _cc="CC=${MINGW_PREFIX}/bin/gcc"
    local _ar="AR=${MINGW_PREFIX}/bin/ar"
    local _ld="LD=${MINGW_PREFIX}/bin/ld"
    local _ranlib="RANLIB=${MINGW_PREFIX}/bin/ranlib"
    make -f makefile.u ${_ar} ${_cc} ${_ld} ${_ranlib} \
        CFLAGS='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4 -DUSE_CLOCK'
  popd
  
  #cd ${srcdir}/f2c
  #make -f makefile.u ${_cc} CFLAGS="${CFLAGS} -DNO_MKDTEMP=1"
}

package() {
  cd "${srcdir}/libf2c"
  install -Dm644 f2c.h "${pkgdir}"${MINGW_PREFIX}/include/f2c.h
  install -Dm644 libf2c.a "${pkgdir}"${MINGW_PREFIX}/lib/libf2c.a
  # ${_arch}-strip --strip-unneeded "${pkgdir}"${MINGW_PREFIX}/bin/*.dll
  # ${_arch}-strip -g "${pkgdir}"${MINGW_PREFIX}/lib/*.a

  #install -Dm755 ${srcdir}/f2c/f2c "${pkgdir}"${MINGW_PREFIX}/bin
}
