# -*- 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           gitlab 1.0
PortGroup           meson 1.0

name                shared-mime-info

set ver_latest      yes
platform darwin {
    # Version 2.2 is last version not requiring std::filesystem
    # Fallback to it, for 10.5 and earlier
    if { ${os.major} >= 10 } {
        PortGroup   legacysupport 1.1

        # Needed for std::filesystem
        legacysupport.newest_darwin_requires_legacy 18
        legacysupport.use_mp_libcxx                 yes
    } else {
        set ver_latest no
    }
}

if { ${ver_latest} } {
    version         2.4
    revision        0

    checksums       rmd160  e51249bfe1fe3cb936eaa346f1643affb9808b89 \
                    sha256  32dc32ae39ff1c1bf8434dd3b36770b48538a1772bc0298509d034f057005992 \
                    size    7096347
} else {
    version         2.2
    revision        9

    checksums       rmd160  c8efc1e91c23b82c37d9f78f6d9b1ffbf7be8a11 \
                    sha256  418c480019d9865f67f922dfb88de00e9f38bf971205d55cdffab50432919e61 \
                    size    6428871
}

gitlab.instance     https://gitlab.freedesktop.org
gitlab.setup        xdg ${name} ${version}

platforms           darwin linux
categories          devel
license             GPL-2+
maintainers         {mascguy @mascguy} openmaintainer

description         Database of common types.
long_description    The core database of common types and the \
                    update-mime-database command used to extend it. \n\
                    Installs and starts a watcher daemon that keeps the \
                    cache up-to-date when other ports add or remove mime definitions.
homepage            https://www.freedesktop.org/wiki/Software/shared-mime-info/

installs_libs       no

# Disable unexpected download of subprojects
meson.wrap_mode     nodownload

set script_orig     update-mime-database
set script_real     update-mime-database-real

set startupitem_file \
                    org.macports.shared-mime-info-updater.plist

post-extract {
    copy ${filespath}/${startupitem_file} ${worksrcpath}
    reinplace -locale C "s|@PREFIX@|${prefix}|g" \
        ${worksrcpath}/${startupitem_file}
}

if { ${ver_latest} } {
    # Replace use of fdatasync(), with fsync()
    patchfiles-append \
                    patch-fdatasync-to-fsync.diff
}

depends_build-append \
                    port:gettext \
                    port:itstool \
                    port:pkgconfig \
                    port:xmlto

# Note: gettext only used at build time; lib dep for gettext-runtime not needed
depends_lib-append \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:libxml2

if {${os.platform} eq "darwin"} {
    # bash and coreutils needed for stub script
    depends_run-append \
                    path:bin/bash:bash \
                    path:libexec/coreutils/libstdbuf.so:coreutils
}

patchfiles-append   libxml2-2.12.patch

configure.args-append \
                    -Dbuild-tools=true \
                    -Dwarning_level=0 \
                    -Dupdate-mimedb=false

if { ${ver_latest} } {
    compiler.cxx_standard 2017

    if { [string match *clang* ${configure.compiler}] } {
        configure.cxxflags-append \
                    -Wno-reserved-user-defined-literal \
                    -Wno-error=unknown-warning-option \
                    -Wno-unknown-warning-option
    }

    configure.args-append \
                    -Dbuild-tests=false \
                    -Dbuild-translations=true
}

# add an entry for Objective C++
# (https://bugs.freedesktop.org/show_bug.cgi?id=98823)
# upstream bug #98823 mentioned here is still not resolved
# now moved to current freedesktop.org Gitlab repo as shared-mime-info issue #52
# (https://gitlab.freedesktop.org/xdg/shared-mime-info/-/issues/52)
# patchfiles-append   patch-add-objc++-def.diff

# post-destroot {
#     xinstall -m 644 ${filespath}/ObjCpp.xml \
#         ${destroot}${prefix}/share/mime/packages
# }

post-destroot {
    set bin_dir ${destroot}${prefix}/bin

    # Rename real updater script, to avoid inadvertent use
    move ${bin_dir}/${script_orig} ${bin_dir}/${script_real}

    if {${os.platform} eq "darwin"} {
        # Copy in stub script, which initiates update by touching mime area
        copy ${filespath}/${script_orig} ${bin_dir}

        reinplace "s|@PREFIX@|${prefix}|g" \
            ${bin_dir}/${script_orig}
        reinplace "s|@SCRIPT_REAL@|${script_real}|g" \
            ${bin_dir}/${script_orig}
    } else {
        # For non-Darwin, symlink stub to real script
        ln -s ${prefix}/bin/${script_real} ${destroot}${prefix}/bin/${script_orig}
    }
}

post-activate {
    if {${os.platform} ne "darwin"} {
        ui_msg "Updating MIME database..."
        system "env XDG_DATA_DIRS=${prefix}/share ${prefix}/bin/${script_real} -V ${prefix}/share/mime"
    }
}

post-deactivate {
    # cleanup
    foreach f [glob -nocomplain ${prefix}/share/mime/*] {
        if {${f} ne "${prefix}/share/mime/packages"} {
            file delete -force ${f}
        }
    }
}

startupitem.autostart    yes
startupitem.custom_file  ${worksrcpath}/${startupitem_file}
startupitem.name         ${name}-updater
startupitem.type         launchd

pre-pkg {
    xinstall -m 0755     ${filespath}/postinstall ${package.scripts}/
    reinplace -locale C  "s|@PREFIX@|${prefix}|g" ${package.scripts}/postinstall
    long_description-append \
                         "Install prefix: ${prefix}"
}
