# $Id: Portfile 90705 2012-03-12 22:51:34Z jeremyhu@macports.org $

PortSystem      1.0

name            canna
version         3.7p3
set dicversion  0.95c
revision        3
categories      textproc japanese
maintainers     takanori openmaintainer
description     Kana-Kanji conversion system
long_description \
                ${description}
platforms       darwin
license         Restrictive/Distributable BSD-old GPL-2+ MIT
homepage        http://canna.sourceforge.jp/
master_sites    sourceforge_jp:${name}/9565/:canna \
                http://cannadic.oucrc.org/:cannadic
distname        "Canna[string map {"." ""} ${version}]"
use_bzip2       yes
distfiles       ${distname}${extract.suffix}:canna \
                cannadic-${dicversion}.tar.gz:cannadic
patchfiles      patch-Canna.conf.diff \
                patch-canna_widedef.h.diff \
                patch-doc_man_guide_tex_Imakefile.diff \
                patch-lib_canna_lisp.c.diff \
                patch-server_util.c.diff \
                patch-cmd_catdic_Imakefile.diff \
                patch-lib_RK_ncache.c.diff \
                patch-server_comm.c.diff \
                \
                patch-dic_ideo_words_dics.dir.diff \
                patch-misc_default.ca.diff
checksums       ${distname}${extract.suffix} md5 0b8c241f63ab4cd3c0b9be569456dc33 \
                                             sha1 e39eece7c70c669dd46dd74b26121a60a2496fde \
                                             rmd160 33e5eb9e2dc144835db838612897e652acbc60ed \
                cannadic-${dicversion}.tar.gz md5 2a7fc1be5a50e0ca5dd6e5721be4371a \
                                              sha1 a8e452a509ea38643197b21ec6c10739c8dd9e4b \
                                              rmd160 d0920e718d35d038fff930ac50ebe049a322174d

extract.only    ${distname}${extract.suffix}
post-extract {
    system "cd ${workpath} && gzip -dc ${distpath}/cannadic-${dicversion}.tar.gz | tar --no-same-owner -xf -"
}

post-patch {
    reinplace -locale C "s|__PREFIX__|${prefix}|" ${worksrcpath}/Canna.conf
}

use_xmkmf       yes

# https://trac.macports.org/ticket/31504
if {${configure.compiler} == "clang"} { 
    configure.compiler llvm-gcc-4.2 
}
build.env-append IMAKECPP="${configure.cpp}"

pre-build {
    build.target    canna
    if {[variant_isset universal]} {
        build.post_args CDEBUGFLAGS=\"${configure.cflags} ${configure.universal_cflags}\" SHLIBGLOBALSFLAGS=\"${configure.universal_cflags}\"
    } else {
        build.post_args CDEBUGFLAGS=\"${configure.cflags}\"
    }
}
use_parallel_build  no
post-build {
    system "cd ${workpath}/cannadic-${dicversion} && ${worksrcpath}/cmd/crxdic/crxdic -D ${worksrcpath}/dic/ideo/grammar/cnj.bits -m -o gcanna.cbd gcanna.ctd"
    system "cd ${workpath}/cannadic-${dicversion} && ${worksrcpath}/cmd/crfreq/crfreq -div 512 gcanna.cbd gcanna.mwd"
}

destroot.target install install.man
destroot.keepdirs ${destroot}${prefix}/var/db/canna/dic/user \
                  ${destroot}${prefix}/var/db/canna/dic/group
pre-destroot {
    addgroup canna
    adduser canna gid=[existsgroup canna]
}
post-destroot {
    foreach f {gcanna.cbd gcanna.cld gcannaf.ctd} {
        xinstall -m 664 -o canna -g canna ${workpath}/cannadic-${dicversion}/${f} ${destroot}${prefix}/var/db/canna/dic/canna
    }
    xinstall -m 775 -o canna -g canna -d ${destroot}${prefix}/var/db/canna/dic/user
    xinstall -m 775 -o canna -g canna -d ${destroot}${prefix}/var/db/canna/dic/group
    xinstall -m 755 ${worksrcpath}/misc/rc.canna ${destroot}${prefix}/etc
}
post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    foreach f {COPYRIGHT ChangeLog INSTALL README WHATIS *.jp} {
        eval xinstall -m 644 [glob ${worksrcpath}/${f}] ${destroot}${prefix}/share/doc/${name}
    }
}

pre-install {
    addgroup canna
    adduser canna gid=[existsgroup canna]
}

startupitem.create      yes
startupitem.name        canna
startupitem.start       "${prefix}/etc/rc.canna start"
startupitem.stop        "${prefix}/etc/rc.canna stop"

variant universal {}

variant inet description {Accept TCP 5680 incoming connections from clients. (Possible security risk.)} {
    post-patch {
        reinplace "s|-syslog|-syslog -inet|" ${worksrcpath}/misc/rc.canna
    }
}

variant pdfdoc description {Create PDF documents using pTeX.} {
    depends_build-append port:pTeX port:ghostscript port:ghostscript-fonts-hiragino
    post-build {
        system "cd ${worksrcpath}/doc/man/guide/tex && xmkmf && make canna.pdf"

        # These two files are written in LaTeX 2.09!
        system "cd ${worksrcpath}/doc/intern && platex -kanji=euc proto12.tex && platex -kanji=euc proto12.tex && dvipdfmx proto12.dvi"
        system "cd ${worksrcpath}/doc/intern && platex -kanji=euc protocol.tex && platex -kanji=euc protocol.tex && dvipdfmx protocol.dvi"

        system "cd ${worksrcpath}/doc/lisp && platex -kanji=euc canlisp.tex && platex -kanji=euc canlisp.tex && dvipdfmx canlisp.dvi"
    }
    post-destroot {
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/guide
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/intern
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/lisp

        xinstall -m 644 ${worksrcpath}/doc/man/guide/tex/canna.pdf ${destroot}${prefix}/share/doc/${name}/guide
        xinstall -m 644 ${worksrcpath}/doc/intern/proto12.pdf ${destroot}${prefix}/share/doc/${name}/intern
        xinstall -m 644 ${worksrcpath}/doc/intern/protocol.pdf ${destroot}${prefix}/share/doc/${name}/intern
        xinstall -m 644 ${worksrcpath}/doc/lisp/canlisp.pdf ${destroot}${prefix}/share/doc/${name}/lisp
    }
}

livecheck.type  regex
livecheck.regex Canna (\[0-9.a-z\]+)
