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

name               nethack
version            3.6.7
revision           1
categories         games
license            Copyleft
maintainers        {@jflude hotmail.com:justin_flude} \
                   @Nax \
                   openmaintainer
description        Classic dungeon adventure game.
long_description   NetHack is a single-player, display-oriented Dungeons & \
    Dragons(tm)-like game, in development since 1987. It runs on a wide \
    variety of computer systems and graphical interfaces, although the text \
    interface is the most popular. Your goal is to grab as much treasure as \
    you can, retrieve the Amulet of Yendor, and escape the Mazes of Menace \
    alive.

homepage           https://www.nethack.org/
master_sites       ${homepage}download/${version}/
livecheck.url      https://github.com/NetHack/NetHack/tags
livecheck.regex    "NetHack-(\[\\d.\]+)_Released"
distname           ${name}-[string map {{.} {}} ${version}]-src
extract.suffix     .tgz
checksums          sha256 98cf67df6debf9668a61745aa84c09bcab362e5d33f5b944ec5155d44d2aacb2 \
                   rmd160 d840125094bbd67e11fa2c6fb640fab97c679994 \
                   size 5577415

depends_lib        port:ncurses
depends_build-append  port:bison
worksrcdir         NetHack-${version}

patch.pre_args-replace  -p0 -p1
patchfiles         patch-gamestate-dir.diff \
                   patch-manpage-dir.diff \
                   patch-curses-graphics.diff

patchfiles-append  patch-nethack-warn-unused-result.diff

post-patch {
    if {${subport} eq "nethack"} {
        reinplace "s|__PREFIX__|${prefix}|" \
            "${worksrcpath}/sys/unix/Makefile.doc" \
            "${worksrcpath}/sys/unix/hints/macosx10.10" \
            "${worksrcpath}/sys/unix/hints/macosx10.14"
    } else {
        reinplace "s|__PREFIX__|${prefix}|" \
            "${worksrcpath}/sys/unix/Makefile.doc" \
            "${worksrcpath}/sys/unix/Makefile.top"
    }
}

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

platform darwin {
    if {${os.major} >= 18} {
        configure.pre_args  hints/macosx10.14
    } else {
        configure.pre_args  hints/macosx10.10
    }
}

use_parallel_build no
build.args-append  CC=${configure.cc} \
                   CXX=${configure.cxx} \
                   CPP=${configure.cpp} \
                   CFLAGS="-I../include -DCURSES_GRAPHICS -DNOTPARMDECL"

pre-destroot {
    xinstall -d "${destroot}${prefix}/share/man/man6"
    if {${subport} ne "nethack"} {
        xinstall -d "${destroot}${prefix}/share/nethackdir/save"
    }
}

destroot.target    all install manpages
destroot.keepdirs  "${destroot}${prefix}/var/games/nethack/save/"

# We want to avoid overwriting any existing game-state files.  So after
# destroot we rename the new versions to something safe and after
# activation only install them if old versions do not already exist.
if {${subport} eq "nethack"} {
    set state_files { logfile record sysconf }
    set state_dir ${prefix}/var/games/nethack
} else {
    set state_files { logfile record }
    set state_dir ${prefix}/share/nethackdir
}

post-destroot {
    reinplace "s|${destroot}||" "${destroot}${prefix}/bin/${subport}"
    foreach f ${state_files} {
        file rename ${destroot}${state_dir}/${f} ${destroot}${state_dir}/${f}.dist
    }
}

post-activate {
    foreach f ${state_files} {
        if {![file exists ${state_dir}/${f}]} {
            file copy ${state_dir}/${f}.dist ${state_dir}/${f}
        }
    }
}

variant x11 {
    if {${subport} eq "nethack"} {
        patchfiles-append  x11/patch-x11-interface.diff
    } else {
        patchfiles-append \
            x11/patch-include__config.h.diff \
            x11/patch-sys__unix__Makefile.src.diff \
            x11/patch-sys__unix__Makefile.top.diff
    }
    depends_lib-append port:xorg-libXaw
}

subport nethack343 {
    version             3.4.3
    revision            0
    description         Classic dungeon adventure game (previous version).
    platforms           darwin freebsd

    master_sites        sourceforge:nethack
    livecheck.type      none
    distname            nethack-343-src
    checksums           sha256 bb39c3d2a9ee2df4a0c8fdde708fbc63740853a7608d2f4c560b488124866fe4 \
                        rmd160 42f600d24715a0b7e631b357c135761410b3ca95 \
                        size 3497458

    worksrcdir          nethack-${version}

    patchfiles          patch-doc__nethack.6.diff \
                        patch-sys__share__ioctl.diff \
                        patch-sys__unix__Makefile.doc.diff \
                        patch-sys__unix__Makefile.src.diff \
                        patch-sys__unix__Makefile.top.diff \
                        patch-win__tty__termcap.c.diff

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

    build.args-append   CC=${configure.cc} \
                        CXX=${configure.cxx} \
                        CPP=${configure.cpp} \
                        CFLAGS="-O2 -I../include"

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

    variant autopickup_exceptions description \
        { Control which things to automatically pick up } {
        patchfiles-append patch-include__config.h.diff
    }

    variant menucolors description \
        { Allow customization of the color of menus } {
        patch_sites-append http://bilious.alt.org/~paxed/nethack
        patchfiles-append nh343-menucolor.diff
        checksums-append nh343-menucolor.diff md5 ade00f9cb51f1b0140557d329d56844c
        build.args-delete CFLAGS="-O2 -I../include"
        build.args-append CFLAGS="-O2 -I../include -DMENU_COLOR_REGEX_POSIX"
    }

    #variant paranoid {
    #        patch_sites-append http://www.netsonic.fi/~walker/nh/
    #        patchfiles-append paranoid-343.diff
    #        checksums-append paranoid-343.diff md5 ade00f9cb51f1b0140557d329d56844c
    #}

    #variant deathexplore {
    #        patch_sites-append http://www.netsonic.fi/~walker/nh/
    #        patchfiles-append dthexp-343.diff
    #        checksums-append dthexp-343.diff md5 ade00f9cb51f1b0140557d329d56844c
    #}

    #variant sortloot {
    #        patch_sites-append http://www.netsonic.fi/~walker/nh/
    #        patchfiles-append sortloot-343.diff
    #        checksums-append sortloot-343.diff md5 ade00f9cb51f1b0140557d329d56844c
    #}
}
