# -*- 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

github.setup        geneweb geneweb 7.1-beta v
revision            3
categories          genealogy
maintainers         {pguyot @pguyot} openmaintainer
license             GPL-2

description         Sophisticated Genealogy System
long_description    GeneWeb is sophisticated genealogy software with a \
                    web interface.  It provides efficient relationship and consanguinity \
                    computing, support for very large database (i.e., hundreds of thousands \
                    of entries), and more.

homepage            https://geneweb.tuxfamily.org/wiki/GeneWeb

github.tarball_from archive
checksums           rmd160  7fa38db901155a79369b50368450db71db5b48f1 \
                    sha256  bfb2d45e3e1bd2b702ed5a46c8c1d1319bcf23d75d1c1ce771bfb489a5589073 \
                    size    14465662

depends_lib         port:ocaml \
                    port:ocaml-dune \
                    port:ocaml-calendars \
                    port:camlp5 \
                    port:ocaml-camlp-streams \
                    port:ocaml-cppo \
                    port:ocaml-fmt \
                    port:ocaml-jingoo \
                    port:ocaml-markup \
                    port:ocaml-not-ocamlfind \
                    port:ocaml-num \
                    port:ocaml-ocamlformat \
                    port:ocaml-ounit \
                    port:ocaml-ppx_blob \
                    port:ocaml-ppx_deriving \
                    port:ocaml-ppx_import \
                    port:ocaml-stdlib-shims \
                    port:ocaml-syslog \
                    port:ocaml-unidecode \
                    port:ocaml-uri \
                    port:ocaml-uucp \
                    port:ocaml-uunf \
                    port:ocaml-uutf \
                    port:ocaml-zarith

use_parallel_build  no

set gwuser          ${name}
set gwgroup         ${name}
set logdir          ${prefix}/var/log
set langdir         ${prefix}/share/${name}/gw
set dbdir           ${prefix}/var/db/${name}

add_users ${gwuser} group=${gwgroup} \
                    home=${dbdir} \
                    realname=Geneweb

startupitem.create  yes
startupitem.user    geneweb
startupitem.executable \
                    ${prefix}/share/${name}/gw/gwd \
                    -bd ${dbdir} \
                    -hd ${langdir} \
                    -lang en \
                    -log ${logdir}/${name}.log \
                    -daemon
startupitem.pidfile none

# Include PR1806 to fix compatibility with camlp5 8.03.0.
# https://github.com/geneweb/geneweb/pull/1806/files
patchfiles          patch-bin-gwrepl-mk_data.ml.diff \
                    patch-Makefile-PR1806.diff
post-patch {
    reinplace       "s|__MACPORTS_PREFIX__|${prefix}|g" \
                    ${worksrcpath}/bin/gwrepl/mk_data.ml
}

configure.cmd ocaml
configure.pre_args
configure.args ./configure.ml

build.target        distrib

destroot.keepdirs   ${destroot}${dbdir}

set distrib ${worksrcpath}/distribution

destroot {
    xinstall -m 0755 -o ${gwuser} -g ${gwgroup} -d ${destroot}${logdir}
    xinstall -m 0755 -o ${gwuser} -g ${gwgroup} -d ${destroot}${dbdir}

    system "touch ${destroot}${logdir}/${name}.log"
    system "chown ${gwuser}:${gwgroup} ${destroot}${logdir}/${name}.log"
    system "chown -R ${gwuser}:${gwgroup} ${destroot}${dbdir}"

    xinstall -m 0755 -o ${gwuser} -g ${gwgroup} -d ${destroot}${prefix}/share/${name}
    xinstall -m 0755 -o ${gwuser} -g ${gwgroup} -d ${destroot}${prefix}/share/doc/${name}

    foreach x {LICENSE.txt README.txt START.htm README.txt LISEZMOI.txt CHANGES.txt gw geneweb.sh gwd.sh gwsetup.sh} {
        file copy -force ${distrib}/${x} ${destroot}${prefix}/share/${name}/${x}
    }

    system "chmod a+rx ${destroot}${prefix}/share/${name}/gwsetup.sh"
    system "chmod a+rx ${destroot}${prefix}/share/${name}/gwd.sh"

    # Make the directory structure generally writeable so the gwsetup and other stuff will work easily.
    system "chown -R ${gwuser}:${gwgroup} ${destroot}${prefix}/share/${name}"
    system "chmod -R a+r ${destroot}${prefix}/share/${name}/gw"

    # Move a few utilities where the user can get to them easily
    file copy -force ${destroot}${prefix}/share/${name}/gw/consang ${destroot}${prefix}/bin
    file copy -force ${destroot}${prefix}/share/${name}/gw/ged2gwb ${destroot}${prefix}/bin
    file copy -force ${destroot}${prefix}/share/${name}/gw/gwb2ged ${destroot}${prefix}/bin
    file copy -force ${filespath}/gwsetup.macports ${destroot}${prefix}/bin/gwsetup

    # Set correct path in gwsetup
    reinplace "s|%PREFIX%|${prefix}|" ${destroot}${prefix}/bin/gwsetup
    system "chmod a+rx ${destroot}${prefix}/bin/gwsetup"
}

post-activate {
    system "chown -R ${gwuser}:${gwgroup} ${prefix}/var/db/${name}"
    system "chmod -R a+rw ${prefix}/var/db/${name}"
}

notes "
Geneweb will be listening on localhost, port 2317.

To get started, you will need to load an initial database.\
Run the program 'gwsetup' to get started. After that point,\
simply point your browser to http://localhost:2317 to review\
the data.
"
