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

_realname=ocaml-findlib
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.7.1
pkgrel=1
pkgdesc="OCaml library manager (mingw-w64)"
arch=('any')
url='http://projects.camlcity.org/projects/findlib.html'
license=('custom')
install=ocaml-findlib-${CARCH}.install
#we require msys2-runtime for cygpath to setup findlib.conf.
depends=("${MINGW_PACKAGE_PREFIX}-ocaml" "msys2-runtime")
source=("http://download.camlcity.org/download/findlib-${pkgver}.tar.gz")
sha256sums=('5d4b9a79e9abf8be0b509f6b8cf5696221cbe14fa2fbb2bb352342755fd15eef')

prepare() {
  plain "skip"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf findlib-${pkgver} build-${MINGW_CHOST}

  unset FLEXDIR
  unset OCAMLDIR

  cd ${srcdir}/build-${MINGW_CHOST}
  PATH=${MINGW_PREFIX}/lib/ocaml/stublibs:$PATH \
  ./configure -system win32 \
              -sitelib ${MINGW_PREFIX}/lib/ocaml \
              -mandir ${MINGW_PREFIX}/share/man \
              -config ${MINGW_PREFIX}/etc/findlib.conf \
              -no-camlp4

  make all -j1
  make opt -j1
}

check() {
  # The package does not include a testsuite
  plain "skip"
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make install prefix=${pkgdir}
  [ -f LICENSE ] && install -Dm644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

#The config file requires a dos path in a format where \\ are doubled. 
#The problem is that make install does this on the build system and that path
#may be invalid on the package target system.  So we have to undo it here and then redo it
#on the target system itself.
  _varvalue="$(cygpath -w -l ${MINGW_PREFIX}/lib/ocaml)"
  _varvalue="$(echo "$_varvalue" | sed -e 's;\\;\\\\\\\\;g')"
  sed -e "s;${_varvalue};${MINGW_PREFIX}/lib/ocaml;g" -i ${pkgdir}${MINGW_PREFIX}/etc/findlib.conf
}
