# Maintainer ncihnegn <ningchen@outlook.com>
# Maintainer: Alethea Rose <alethea@alethearose.com>

_realname=ghc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.6.5
pkgrel=2
pkgdesc="The Glorious Glasgow Haskell Compilation System (mingw-w64)"
arch=('any')
mingw_arch=()
group="${MINGW_PACKAGE_PREFIX}-haskell-platform"
url='https://www.haskell.org/ghc/'
license=('The Glasgow Haskell Compiler License')
depends=("${MINGW_PACKAGE_PREFIX}-cc"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-isl"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-mpc"
         "${MINGW_PACKAGE_PREFIX}-mpfr")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             'perl')
provides=("${MINGW_PACKAGE_PREFIX}-haskell-array=0.5.3.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-base=4.12.0.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-binary=0.8.6.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-bytestring=0.10.8.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-containers=0.6.0.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-deepseq=1.4.4.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-directory=1.3.3.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-filepath=1.4.2.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-boot=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-boot-th=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-compact=0.1.0.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghci=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-heap=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-prim=0.5.3.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-haskeline=0.7.4.3"
          "${MINGW_PACKAGE_PREFIX}-haskell-hpc=0.6.0.3"
          "${MINGW_PACKAGE_PREFIX}-haskell-integer-gmp=1.0.2.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-integer-simple=0.1.1.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-libiserv=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-haskell-mtl=2.2.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-parsec=3.1.13.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-pretty=1.1.3.6"
          "${MINGW_PACKAGE_PREFIX}-haskell-process=1.6.5.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-stm=2.5.0.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-template-haskell=2.14.0.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-terminfo=0.4.1.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-text=1.2.3.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-time=1.8.0.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-transformers=0.5.6.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-win32=2.7.2.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-xhtml=3000.2.2.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-cabal=2.4.0.1")
replaces=("${MINGW_PACKAGE_PREFIX}-haskell-array"
          "${MINGW_PACKAGE_PREFIX}-haskell-base"
          "${MINGW_PACKAGE_PREFIX}-haskell-binary"
          "${MINGW_PACKAGE_PREFIX}-haskell-bytestring"
          "${MINGW_PACKAGE_PREFIX}-haskell-containers"
          "${MINGW_PACKAGE_PREFIX}-haskell-deepseq"
          "${MINGW_PACKAGE_PREFIX}-haskell-directory"
          "${MINGW_PACKAGE_PREFIX}-haskell-filepath"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-boot"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-boot-th"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-compact"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghci"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-heap"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-prim"
          "${MINGW_PACKAGE_PREFIX}-haskell-haskeline"
          "${MINGW_PACKAGE_PREFIX}-haskell-hpc"
          "${MINGW_PACKAGE_PREFIX}-haskell-integer-gmp"
          "${MINGW_PACKAGE_PREFIX}-haskell-integer-simple"
          "${MINGW_PACKAGE_PREFIX}-haskell-libiserv"
          "${MINGW_PACKAGE_PREFIX}-haskell-mtl"
          "${MINGW_PACKAGE_PREFIX}-haskell-parsec"
          "${MINGW_PACKAGE_PREFIX}-haskell-pretty"
          "${MINGW_PACKAGE_PREFIX}-haskell-process"
          "${MINGW_PACKAGE_PREFIX}-haskell-stm"
          "${MINGW_PACKAGE_PREFIX}-haskell-template-haskell"
          "${MINGW_PACKAGE_PREFIX}-haskell-terminfo"
          "${MINGW_PACKAGE_PREFIX}-haskell-text"
          "${MINGW_PACKAGE_PREFIX}-haskell-time"
          "${MINGW_PACKAGE_PREFIX}-haskell-transformers"
          "${MINGW_PACKAGE_PREFIX}-haskell-win32"
          "${MINGW_PACKAGE_PREFIX}-haskell-xhtml"
          "${MINGW_PACKAGE_PREFIX}-haskell-cabal")
optdepends=("${MINGW_PACKAGE_PREFIX}-llvm: use LLVM for an optimized backend")
install="ghc-${MSYSTEM}.install"

_hsarch=$CARCH
if [[ ${_hsarch} == "i686" ]]; then
  _hsarch="i386"
fi
_hooks=('ghc-register.hook' 'ghc-unregister.hook' 'ghc-rebuild-doc-index.hook')

source=("https://downloads.haskell.org/~ghc/${pkgver}/ghc-${pkgver}-src.tar.xz"{,.sig}
        "https://downloads.haskell.org/~ghc/${pkgver}/ghc-${pkgver}-testsuite.tar.xz"{,.sig}
        "https://downloads.haskell.org/~ghc/${pkgver}/ghc-${pkgver}-${_hsarch}-unknown-mingw32.tar.xz"{,.sig}
        'build.mk'
        '0002-Switch-to-LLVM-version-7.0.patch'
        'ghc-register.hook'
        'ghc-unregister.hook'
        'ghc-rebuild-doc-index.hook')
noextract=("ghc-${pkgver}-${_hsarch}-unknown-mingw32.tar.xz")

sha256sums=('4d4aa1e96f4001b934ac6193ab09af5d6172f41f5a5d39d8e43393b9aafee361'
            'SKIP'
            '21391cb63a8a6b327f6c9519217a3dad39493e72c48967008ae35af142ca895f'
            'SKIP'
            '2a8fb73080ed4335f7a172fe6cf9da1a2faa51fdb72817c50088292f497fc57a'
            'SKIP'
            '7001599139d0e81bcb098158eede2a3910428f69d65dbcacb9372633d0fa6076'
            '6904893a6f97b2818abe89b952765752d6894e611c4838f7ebff5a6a577c18d5'
            '633b6a02efa3a6f30c4c40062f24bf0ffa9689c21d0e16da09ed8b699ba6050e'
            '33bde1f07e6317017e9c7fc7b2e7ad2d63ed3dcde407a23401d0a03861301cea'
            '31f3b191bcfe7ad09a886e90f6d71c3be5bee02304a81bffe61ac1f5dcdae703')
validpgpkeys=('FFEB7CE81E16A36B3E2DED6F2DE04D4E97DB64AD') # Benjamin Gamari <ben@well-typed.com>

prepare() {
  cd "${srcdir}"

  mkdir -p ${MSYSTEM}/host
  rm -rf ${MSYSTEM}/ghc-${pkgver}
  mv {,$CARCH/}ghc-${pkgver}

  msg "Extracting binaries: ghc-${pkgver}-${_hsarch}-unknown-mingw32.tar.xz..."
  bsdtar -xf ghc-${pkgver}-${_hsarch}-unknown-mingw32.tar.xz \
    -C ${MSYSTEM}/host --strip-components 1

  for _hook in "${_hooks[@]}"; do
    echo $_hook
    sed -e "s|MINGW_PREFIX|$(basename ${MINGW_PREFIX})|" \
      ${_hook} > ${MSYSTEM}/${_hook}
  done

  export PATH="${srcdir}/${MSYSTEM}/host/bin:$PATH"

  cp -v "${srcdir}/build.mk" "${srcdir}/${MSYSTEM}/ghc-${pkgver}/mk/build.mk"

  cd "${srcdir}/${MSYSTEM}/ghc-${pkgver}"
  patch -Np1 -i "${srcdir}/0002-Switch-to-LLVM-version-7.0.patch"
}

build() {
  export PATH="${srcdir}/${MSYSTEM}/host/bin:${PATH}"
  cd "${srcdir}/${MSYSTEM}/ghc-${pkgver}"

  ./boot
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-system-libffi \
    --enable-distro-toolchain \
    --with-ffi-includes="$(cygpath -u $(pkg-config --variable=includedir libffi))"

  make
  make html
}

check() {
  cd "${srcdir}/${MSYSTEM}/ghc-${pkgver}"

  make test
}

package() {
  cd "${srcdir}/${MSYSTEM}/ghc-${pkgver}"

  make prefix="${pkgdir}${MINGW_PREFIX}" \
    docdir="${pkgdir}${MINGW_PREFIX}/share/docs/ghc" \
    install

  for _hook in "${_hooks[@]}"; do
    install -Dm644 "${srcdir}/${MSYSTEM}/${_hook}" \
      "${pkgdir}/usr/share/libalpm/hooks/${MINGW_PACKAGE_PREFIX}-${_hook}"
  done

  mkdir -vp "${pkgdir}${MINGW_PREFIX}/share/haskell/"{register,unregister}

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/ghc/LICENSE"
}
