# Maintainer: Junjie Mao <eternal.n08@gmail.com>

_realname=ocaml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" 
         "${MINGW_PACKAGE_PREFIX}-${_realname}-compiler-libs")
pkgver=4.11.1
pkgrel=2
pkgdesc="An industrial strength programming language supporting functional, imperative and object-oriented styles (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://ocaml.org/'
license=('Q Public Licence 1.0' 'GPL2')
makedepends=("${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-crt-git")
optdepends=("${MINGW_PACKAGE_PREFIX}-ncurses: advanced ncurses features"
            "${MINGW_PACKAGE_PREFIX}-tk: advanced tk features")
source=("https://caml.inria.fr/distrib/ocaml-${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        "https://github.com/alainfrisch/flexdll/archive/0.39.tar.gz"
        "0001-fix-misc-h.patch")
sha256sums=('ea4a9187035019f219a50458b7c09ab74239272e5b6ea3ab2a0c840b5810854c'
            '51a6ef2e67ff475c33a76b3dc86401a0f286c9a3339ee8145053ea02d2fb5974'
            '24d20cc5fc9c0f70e5c54c2378f0be612ae474425dd5918b321469fb887eb358')
options=('!makeflags' '!emptydirs' 'staticlibs')
install=ocaml-${CARCH}.install

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-fix-misc-h.patch"
}

build() {  
  cd "${srcdir}"

  ln -sf ${MINGW_PREFIX}/bin/windres.exe ${CARCH}-w64-mingw32-windres.exe
  ln -sf ${MINGW_PREFIX}/bin/as.exe ${CARCH}-w64-mingw32-as.exe

  export PATH=${PWD}:${PATH}

  cd "${srcdir}/${_realname}-${pkgver}"

  cp -r "${srcdir}/flexdll-0.39/"* flexdll

  export BFD_LIB_DIR="${MINGW_PREFIX}/lib/binutils/"
  export BFD_INCLUDE_DIR="${MINGW_PREFIX}/include/binutils/"
  export LDFLAGS="-L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/"
  export CFLAGS="-I${srcdir}/${_realname}-${pkgver}/flexdll/"

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --mandir=${MINGW_PREFIX}/share/man \
    --disable-force-safe-string

  #Necessary for ocaml to find ws2_32
  export SEDTEXT="s:  BOOT_FLEXLINK_CMD = FLEXLINK_CMD=\"../boot/ocamlrun ../flexdll/flexlink.exe\":  BOOT_FLEXLINK_CMD = FLEXLINK_CMD=\"../boot/ocamlrun ../flexdll/flexlink.exe -L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/\":g"
  sed -i "${SEDTEXT}" Makefile

  SEDTEXT="s:CAMLOPT=\$(CAMLRUN) ./ocamlopt -g -nostdlib -I stdlib -I otherlibs/dynlink:CAMLOPT=\$(CAMLRUN) ./ocamlopt -g -nostdlib -I stdlib -I otherlibs/dynlink -cclib \"-L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/\":g"
  sed -i "${SEDTEXT}" Makefile

  SEDTEXT="s:BYTECCLIBS=-lws2_32 -lversion:BYTECCLIBS=-L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/ -lws2_32 -lversion:g"
  sed -i "${SEDTEXT}" Makefile.config

  SEDTEXT="s:CAMLOPT_CMD = \$(FLEXLINK_ENV) \$(CAMLOPT):CAMLOPT_CMD = \$(FLEXLINK_ENV) \$(CAMLOPT) -cclib \"-L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/\":g"
  sed -i "${SEDTEXT}" Makefile.common

  SEDTEXT="s:WIN32_LIBS=\$(call SYSLIB,ws2_32) \$(call SYSLIB,advapi32):WIN32_LIBS=\$(call SYSLIB,ws2_32) \$(call SYSLIB,advapi32) -L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/:g"
  sed -i "${SEDTEXT}" otherlibs/win32unix/Makefile

  SEDTEXT="s:MKLIB=\$(CAMLRUN) \$(ROOTDIR)/tools/ocamlmklib:MKLIB=\$(CAMLRUN) \$(ROOTDIR)/tools/ocamlmklib -L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/:g"
  sed -i "${SEDTEXT}" otherlibs/systhreads/Makefile

  SEDTEXT="s:LDOPTS ?=:LDOPTS = -L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/:g"
  sed -i "${SEDTEXT}" otherlibs/Makefile.otherlibs.common

  SEDTEXT="s:OC_CPPFLAGS= -DCAML_NAME_SPACE -DUNICODE -D_UNICODE -DWINDOWS_UNICODE=\$(WINDOWS_UNICODE):OC_CPPFLAGS= -DCAML_NAME_SPACE -DUNICODE -D_UNICODE -DWINDOWS_UNICODE=\$(WINDOWS_UNICODE) -I${srcdir}/${_realname}-${pkgver}/flexdll/:g"
  sed -i "${SEDTEXT}" Makefile.config

  SEDTEXT="s:PTHREAD_LINK=:PTHREAD_LINK=-L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/:g"
  sed -i "${SEDTEXT}" Makefile.config

  SEDTEXT="s:-safe-string -strict-sequence -strict-formats -bin-annot -principal:-safe-string -strict-sequence -strict-formats -bin-annot -principal -cclib \"-L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/\":g"
  sed -i "${SEDTEXT}" ocamldoc/Makefile

  make flexdll
  make world.opt

  SEDTEXT="s:LINKFLAGS = -cclib \"-link \$(RES)\":LINKFLAGS = -cclib \"-link \$(RES) -L${MINGW_PREFIX}/${MINGW_CHOST}/lib/ -L${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/10.2.0/\":g"
  sed -i "${SEDTEXT}" flexdll/Makefile

  make flexlink.opt
}

package_ocaml() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make DESTDIR="${pkgdir}" install

  # install license
  install -m755 -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -m644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"

  # remove compiler and flex libs
  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/ocaml/compiler-libs"
  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/ocaml/flexdll"
  rm -rf "${pkgdir}${MINGW_PREFIX}/bin/flexlink.exe"
}

package_ocaml-compiler-libs() {
pkgdesc="Several modules used internally by the OCaml compiler (mingw-w64)"
license=('Q Public Licence 1.0')
depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")
optdepends=()

  cd "${srcdir}/${_realname}-${pkgver}"
  make DESTDIR="${pkgdir}" install
  # Remove non-compiler-libs
  rm -rf   "${pkgdir}${MINGW_PREFIX}/bin"  "${pkgdir}${MINGW_PREFIX}/lib/ocaml/caml" \
     "${pkgdir}${MINGW_PREFIX}/lib/ocaml/ocamldoc" "${pkgdir}${MINGW_PREFIX}/lib/ocaml/stublibs" \
     "${pkgdir}${MINGW_PREFIX}/lib/ocaml/threads" "${pkgdir}${MINGW_PREFIX}/share"
  find "${pkgdir}${MINGW_PREFIX}/lib/ocaml/" -maxdepth 1 -type f -delete

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-compiler-libs"
  install -m644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-compiler-libs/"
  
  # remove flex libs
  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/ocaml/flexdll"
  rm -rf "${pkgdir}${MINGW_PREFIX}/bin/flexlink.exe"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
