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

github.setup        rime squirrel 0.16.2
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            8
name                squirrel-ime
categories          aqua
license             GPL-3
maintainers         {l2dy @l2dy} openmaintainer
description         Rime Input Method for macOS
long_description    ${description}
homepage            https://rime.im/

fetch.type          git

post-fetch {
    system -W ${worksrcpath} "git submodule update --init"
}

post-patch {
    # https://trac.macports.org/ticket/57137
    reinplace "s|xcodebuild -project Squirrel.xcodeproj|& -scheme Squirrel -derivedDataPath ./DerivedData|g" ${worksrcpath}/Makefile
    # Override BUILD_DIR in DerivedData
    reinplace "/xcodebuild/ s| build| BUILD_DIR=${worksrcpath}/build&|" ${worksrcpath}/Makefile
    # Fix librime path
    reinplace "s|path = lib/librime.1.dylib;|path = ${prefix}/lib/librime.1.dylib;|g" ${worksrcpath}/Squirrel.xcodeproj/project.pbxproj
    # Fix opencc data path
    reinplace "s|path = data/opencc;|path = ${prefix}/share/opencc;|g" ${worksrcpath}/Squirrel.xcodeproj/project.pbxproj
    # Create librime bin symlinks
    ln -s ${prefix}/bin/rime_dict_manager ${worksrcpath}/bin/
    ln -s ${prefix}/bin/rime_deployer ${worksrcpath}/bin/
}

if {${os.platform} eq "darwin" && ${os.major} >= 22} {
    # Fix arclite file not found
    post-patch {
        reinplace "s|MACOSX_DEPLOYMENT_TARGET = 10.9;|MACOSX_DEPLOYMENT_TARGET = 10.13;|g" ${worksrcpath}/Squirrel.xcodeproj/project.pbxproj
    }
}

depends_lib         port:libiconv \
                    path:lib/librime.dylib:librime-devel \
                    port:capnproto \
                    port:gflags \
                    port:google-glog \
                    port:leveldb \
                    port:marisa-trie \
                    port:opencc \
                    port:yaml-cpp

patchfiles          patch-Makefile.diff

use_configure       no
use_parallel_build  no
use_xcode           yes

build.target        deps all

destroot.violate_mtree yes
destroot {
    set imedir "/Library/Input Methods"
    xinstall -d "${destroot}${imedir}"
    copy ${worksrcpath}/build/Release/Squirrel.app "${destroot}${imedir}"
    # Fix opencc data
    delete "${destroot}${imedir}/Squirrel.app/Contents/SharedSupport/opencc"
    ln -s ${prefix}/share/opencc "${destroot}${imedir}/Squirrel.app/Contents/SharedSupport/opencc"
    # Overwrite librime bins
    ln -sf ${prefix}/bin/rime_dict_manager "${destroot}${imedir}/Squirrel.app/Contents/MacOS/"
    ln -sf ${prefix}/bin/rime_deployer "${destroot}${imedir}/Squirrel.app/Contents/MacOS/"
}
