# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0

name                ocaml-findlib
github.setup        ocaml ocamlfind 1.9.6 findlib-
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            1
distname            findlib-${version}
categories          ocaml devel
maintainers         {pmetzger @pmetzger} openmaintainer
license             MIT

description         Objective Caml library management system
long_description    The findlib library provides a scheme to manage reusable \
                    software components (packages), and includes tools that \
                    support this scheme. Packages are collections of OCaml \
                    modules for which metainformation can be stored. \
                    The packages are kept in the filesystem \
                    hierarchy, but with strict directory structure. \
                    The library contains functions to look the directory up \
                    that stores a package, to query metainformation about \
                    a package, and to retrieve dependency information \
                    about multiple packages. There is also a tool that \
                    allows the user to enter queries on the command-line. \
                    In order to simplify compilation and linkage, there \
                    are new frontends of the various OCaml compilers that \
                    can directly deal with packages.

checksums           rmd160  1f9ccf5b1320db9e7da71f19ab5cdcb904ad2cbf \
                    sha256  decd1b18e38a5c5c6f555a2ba792f23e3660cc38763d181e28291eb467e90256 \
                    size    166771

depends_lib         port:ocaml

configure.args      -bindir ${prefix}/bin \
                    -mandir ${prefix}/share/man \
                    -sitelib ${prefix}/lib/ocaml/site-lib \
                    -no-camlp4

configure.pre_args

use_parallel_build  no

if {${build_arch} eq "ppc" || ${build_arch} eq "ppc64"} {
    build.target    all
    } else {
    build.target    all opt
}

universal_variant   no

# `ocamlfind printconf destdir` returns nothing if the dir isn't in place
# but we know it's the same as our site-lib dir
set ocamlfind_destdir ${prefix}/lib/ocaml/site-lib/stublibs
destroot.keepdirs   ${destroot}${ocamlfind_destdir}

destroot.env-append OCAMLFIND_DESTDIR=${destroot}${prefix}/lib/ocaml/site-lib
destroot.destdir    prefix=${destroot}

post-activate {
        # Add our stublibs dir path to ld.conf
        set ldconfpath [exec ${prefix}/bin/ocamlfind printconf ldconf]
        # ld.conf should exist since ocaml is installed, but lets just be safe anyway
        if {[file exists $ldconfpath]} then {
          # make sure ld.conf doesn't already have this line, so if the user installs,
          # uninstalls, then installs again, we won't duplicate it
          set has_line 0
          set ldconf [open $ldconfpath r+]
          while {[gets $ldconf line] != -1 && !$has_line} {
            if {[string equal $line $ocamlfind_destdir]} then {
              set has_line 1
            }
          }
          if {!$has_line} {
            if {${registry.format} eq "receipt_flat"} {
                ui_msg "## Adding line `${ocamlfind_destdir}' to file ${ldconfpath}"
                ui_msg "## Be sure to remove this line if you uninstall ${name}"
            }
            puts $ldconf $ocamlfind_destdir
          }
          close $ldconf
        } else {
          ui_warn "File ${ldconfpath} doesn't seem to be installed."
          ui_warn "Is ocaml installed properly?"
        }
}

pre-deactivate {
        # remove our stublibs dir path from ld.conf
        set ocamlfind_destdir [file normalize ${prefix}/lib/ocaml/site-lib/stublibs]
        set ldconfpath [exec ${prefix}/bin/ocamlfind printconf ldconf]
        set ldconfoutpath [file dirname $ldconfpath]/.[file tail $ldconfpath]~
        if {[file exists $ldconfpath]} then {
          set ldconf [open $ldconfpath r]
          set ldconfout [open $ldconfoutpath w]
          while {[gets $ldconf line] != -1} {
            if {![string equal $line $ocamlfind_destdir]} {
              puts $ldconfout $line
            }
          }
          close $ldconfout
          close $ldconf
          file rename -force $ldconfoutpath $ldconfpath
        }
}

variant toolbox description {build the toolbox wizard, requires labltk} {
        configure.args-append -with-toolbox
        depends_lib-append port:ocaml-labltk
}
