# -*- 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.1
categories         games
platforms          darwin
license            Copyleft
maintainers        @Nax
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       sourceforge \
                   ${homepage}download/${version}/
distname           ${name}-[string map {{.} {}} ${version}]-src
extract.suffix     .tgz
checksums          sha256 4b8cbf1cc3ad9f6b9bae892d44a9c63106d44782a210906162a7c3be65040ab6 \
                   rmd160 6f7a0fbcf5754e8df8d3257c711d72d4abb5f673 \
                   size 4640769

depends_lib        port:ncurses
worksrcdir         ${name}-${version}

patch.pre_args     -p1
patchfiles         patch-gamestate-dir.diff \
                   patch-manpage-dir.diff
post-patch {
    if {${subport} eq "nethack"} {
        reinplace "s|__PREFIX__|${prefix}|" \
            "${worksrcpath}/sys/unix/Makefile.doc" \
            "${worksrcpath}/sys/unix/hints/macosx10.10"
    }
}

configure.dir      ${worksrcpath}/sys/unix
configure.cmd      ./setup.sh
configure.pre_args hints/macosx10.10

use_parallel_build no
build.args-append  CC=${configure.cc} \
                   CXX=${configure.cxx} \
                   CPP=${configure.cpp}

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/"

post-destroot {
    if {${subport} eq "nethack"} {
        reinplace "s|${destroot}||" "${destroot}${prefix}/bin/nethack"

        # Prevent existing preferences files being overwritten.
        foreach f { logfile record sysconf } {
            file rename ${destroot}${prefix}/var/games/nethack/${f} \
                ${destroot}${prefix}/var/games/nethack/${f}.dist
        }
    }
}

post-activate {
    if {${subport} eq "nethack"} {
        # Make initially-missing preferences files from earlier versions.
        foreach f { logfile record sysconf } {
            if {![file exists ${prefix}/var/games/nethack/${f}]} {
                file copy ${prefix}/var/games/nethack/${f}.dist \
                    ${prefix}/var/games/nethack/${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
    description         Classic dungeon adventure game (previous version).
    platforms           darwin freebsd

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

    worksrcdir          nethack-${version}

    patchfiles          patch-doc__nethack.6.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

    post-patch {
        reinplace "s|__PREFIX__|${prefix}|" \
            "${worksrcpath}/sys/unix/Makefile.doc" \
            "${worksrcpath}/sys/unix/Makefile.top"
    }

    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/"

    post-destroot {
        reinplace "s|${destroot}||" "${destroot}${prefix}/bin/nethack343"

        # Don't overwrite existing preference files
        foreach f { logfile record } {
            file rename ${destroot}${prefix}/share/nethackdir/${f} \
                ${destroot}${prefix}/share/nethackdir/${f}.dist
        }
    }

    post-activate {
        # Make sure initial preference files exist
        foreach f { logfile record } {
            if {![file exists ${prefix}/share/nethackdir/${f}]} {
                file copy ${prefix}/share/nethackdir/${f}.dist \
                    ${prefix}/share/nethackdir/${f}
            }
        }
    }

    variant autopickup_exceptions description \
        { Automatically pick up things onto which you move } {
        patchfiles-append patch-include__config.h.diff
    }

    variant menucolors description \
        { Allows the user to define in what color menus are shown } {
        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
    #}
}
