PortSystem      1.0

name            jnethack
set ver_main    3.4.3
set ver_patch   0.11
version         ${ver_main}-${ver_patch}
revision        1
categories      games japanese
maintainers     nomaintainer
description     Classic dungeon adventure game, translated in Japanese.
long_description \
                ${description}
platforms       darwin
license         nethack
homepage        http://jnethack.sourceforge.jp/
master_sites    sourceforge_jp:${name}/9091/:nh \
                sourceforge_jp:${name}/58545/:jnh
set nethacksrc      "nethack-[string map {"." ""} ${ver_main}]-src.tgz"
set jnethackpatch   "${name}-${version}.diff.gz"
distfiles       ${nethacksrc}:nh \
                ${jnethackpatch}:jnh
patchfiles      patch-sys_unix_Makefile.doc.diff \
                patch-sys_unix_Makefile.src.diff \
                patch-sys_unix_Makefile.top.diff \
                patch-sys_unix_Makefile.utl.diff \
                patch-sys_unix_nethack.sh.diff \
                patch-win_tty_termcap.c.diff \
                patch-src_options.c.diff
checksums       ${nethacksrc} sha1 c26537093c38152bc0fbcec20468d975b35f59fd \
                ${jnethackpatch}  sha1 ee138602035c0f5587a24b2567135c836ad65395

depends_lib     port:ncurses \
                port:xorg-libX11
depends_build   port:bison \
                port:flex
depends_run     port:cocot

## extract ##

extract.only    ${nethacksrc}
worksrcdir      nethack-${ver_main}
post-extract {
    system "cd ${worksrcpath} && gunzip -dc ${distpath}/${jnethackpatch} | patch -p 1"
}

## patch ##

pre-patch {
    if {[variant_isset x11]} {
        patchfiles-append x11/patch-include_config.h.diff \
                          x11/patch-sys_unix_Makefile.top.diff \
                          x11/patch-sys_unix_Makefile.src.diff \
                          x11/patch-win_X11_JNetHack.ad.diff
    }
}

post-patch {
    set jn_cflags "${configure.cflags} ${configure.cppflags}"
    set jn_ldflags ${configure.ldflags}

    if {[variant_exists universal] && [variant_isset universal]} {
        set jn_cflags "${jn_cflags} ${configure.universal_cflags}"
    }

    foreach f {sys/unix/Makefile.doc
               sys/unix/Makefile.src
               sys/unix/Makefile.top
               sys/unix/Makefile.utl
               sys/unix/nethack.sh} {
        reinplace "s|__PREFIX__|${prefix}|" ${worksrcpath}/${f}
        reinplace "s|__CFLAGS__|${jn_cflags}|" ${worksrcpath}/${f}
        reinplace "s|__LDFLAGS__|${jn_ldflags}|" ${worksrcpath}/${f}
    }
}

## configure ##

configure.dir       ${worksrcpath}/sys/unix
configure.cmd       /bin/sh
configure.pre_args  setup.sh

## build ##

use_parallel_build  no
build.env-append    CC=${configure.cc}

## destroot ##

pre-destroot {
    addgroup games
    adduser games gid=[existsgroup games]
}

destroot.target     install manpages
destroot.keepdirs   "${destroot}${prefix}/share/jnethackdir/save/"

post-destroot {
    reinplace "s|${destroot}||" "${destroot}${prefix}/bin/jnethack"
    if {[variant_isset x11]} {
        copy ${worksrcpath}/win/X11/JNetHack.ad ${destroot}${prefix}/share/jnethackdir/
    }
}

## install ##

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

## variants ##

variant x11 {
    depends_lib-append  port:xorg-libXaw \
                        port:xorg-libXmu \
                        port:xorg-libXext \
                        port:xorg-libXt \
                        port:xpm
#                       port:xorg-libX11
}

variant universal {}

livecheck.type  regex
livecheck.url   ${homepage}
livecheck.regex "\">((?:\\d+(?:\\.\\d+)*)-(?:\\d+(?:\\.\\d+)*)) "
