# -*- 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
# $Id: Portfile 77365 2011-03-28 23:49:09Z ryandesign@macports.org $

PortSystem              1.0

name                    alienarena
# Keep version in sync between alienarena and alienarena-data.
version                 7.45-20100726
revision                3
set version_number      [lindex [split ${version} -] 0]
set version_date        [lindex [split ${version} -] 1]
categories              games
platforms               darwin
maintainers             ryandesign
license                 GPL-2+

homepage                http://red.planetarena.org/
master_sites            http://icculus.org/alienarena/Files/ \
                        gentoo

worksrcdir              alienarena[join [split ${version_number} .] _]
distname                ${worksrcdir}-Linux${version_date}

# Manually emulate "use_zip yes" because we want to extract specific directories only.
#use_zip                 yes
extract.cmd             [findBinary unzip ${portutil::autoconf::unzip_path}]
extract.suffix          .zip
extract.pre_args        -q
extract.post_args       -d ${extract.dir} ${worksrcdir}/source/* ${worksrcdir}/arena/* ${worksrcdir}/data1/*.cfg

checksums               sha1    9c982d097d885af0cbd06bfbe6c4185936c43c4d \
                        rmd160  d20e167c66dec8ec072ecbf5833a4a4b46458a17

depends_build           port:pkgconfig

depends_lib             port:curl \
                        port:jpeg \
                        port:libvorbis \
                        port:mesa \
                        port:xorg-libX11 \
                        port:xorg-libXext \
                        port:xorg-libXxf86dga \
                        port:xorg-libXxf86vm

depends_run             port:alienarena-data

platform darwin {
    pre-fetch {
        # 10.4.11:
        # ld: common symbols not allowed with MH_DYLIB output format with the -multi_module option
        # The usual advice is to use -fno-common (e.g. http://gcc.gnu.org/ml/gcc/2005-06/msg00378.html)
        # but this leads to several "multiple definitions of symbol" errors.
        # 10.5.8:
        # In file included from client/snd_openal.c:37:
        # client/qal.h:108: error: syntax error before 'pqalcCreateContext'
        if {${os.major} < 10} {
            ui_error "Alien Arena requires Mac OS X 10.6 or greater at the moment."
            return -code error "incompatible Mac OS X version"
        }
    }
}

patch.dir               ${worksrcpath}/source

post-extract {
    # DOS to UNIX line endings so we can patch properly.
    fs-traverse file ${patch.dir} {
        switch [file extension ${file}] {
            .c -
            .cpp -
            .h {
                reinplace "s|\r||g" ${file}
            }
        }
    }
}

patchfiles              patch-game-q_shared.h.diff \
                        patch-unix-q_shunix.c.diff

use_configure           no

# On Leopard and presumably Tiger:
# In file included from client/snd_openal.c:37:
# client/qal.h:34: error: syntax error before 'pqalEnable'
configure.cflags-append -DAL_NO_PROTOTYPES

platform darwin {
    patchfiles-append   patch-Makefile.diff \
                        patch-client-qal.h.diff \
                        patch-client-snd_openal.c.diff \
                        patch-qcommon-common.c.diff \
                        patch-ref_gl-r_main.c.diff \
                        patch-ref_gl-r_model.h.diff \
                        patch-unix-qal_unix.c.diff \
                        patch-unix-sys_unix.c.diff
    post-patch {
        reinplace "s|@PREFIX@|${prefix}|g" ${patch.dir}/ref_gl/r_main.c
    }
    configure.cflags-append -D__unix__
}

variant universal {}
if {[variant_isset universal]} {
    set archflags ${configure.universal_cflags}
} else {
    set archflags ${configure.cc_archflags}
}

build.dir               ${patch.dir}

build.target            build-release
destroot.dir            ${build.dir}/release

build.env               CC="${configure.cc}" \
                        CFLAGS="${configure.cflags} ${archflags}" \
                        LDFLAGS="${configure.ldflags} ${archflags}" \
                        PREFIX="${prefix}" \
                        LOCALBASE="${prefix}" \
                        X11BASE="${prefix}" \
                        WITH_DATADIR=yes \
                        WITH_LIBDIR=yes

destroot {
    xinstall -W ${destroot.dir} crx ${destroot}${prefix}/bin
    xinstall -W ${destroot.dir} crded ${destroot}${prefix}/sbin
    
    xinstall -d ${destroot}${prefix}/lib/${name}/data1
    xinstall -W ${destroot.dir} -m 644 game.dylib ${destroot}${prefix}/lib/${name}/data1
    
    xinstall -d ${destroot}${prefix}/share/${name}
    copy ${worksrcpath}/arena ${worksrcpath}/data1 ${destroot}${prefix}/share/${name}
}

notes "
To play Alien Arena, type:

crx
"

livecheck.version       ${version_number}
livecheck.type          regex
livecheck.url           ${homepage}aquire.html
livecheck.regex         {Alien Arena ([0-9.]+)}
