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

PortSystem          1.0

name                simutrans
version             112.0
set branch          [join [lrange [split ${version} .] 0 1] .]
set my_version      [strsed ${version} {g/\./-/}]
set my_branch       [strsed ${branch} {g/\./-/}]
categories          games
license             Artistic-1 MIT
platforms           darwin
maintainers         nomaintainer
description         Simutrans is a freeware transport simulation game.
long_description    Simutrans Transport Simulator (short: Simutrans) is a \
                    freeware simulation game for Windows, BeOS, Mac OS X and \
                    Linux that focuses on the transporting of goods, people, \
                    mail and power. It was originally written by Hansjörg \
                    Malthaner, and is now maintained and developed by a \
                    small team.

homepage            http://www.simutrans.com/
use_zip             yes

extract.mkdir       yes

use_configure       no

set data_dir        ${prefix}/share/${name}

if {${name} eq ${subport}} {
    PortGroup           app 1.0
    
    revision            1
    
    master_sites        sourceforge:project/simutrans/simutrans/${my_version}
    
    distname            ${name}-src-${my_version}
    
    checksums           rmd160  82352f6509e4f9d8402cefb31b7fa7e4dbc228b7 \
                        sha256  1f8c067290c7a286ddddbae41ba3cc4997a0e51d1d0dcbe57120a715d904791e
    
    depends_build       port:pkgconfig
    
    depends_lib         port:libsdl \
                        port:zlib
    
    post-extract {
        copy -force ${worksrcpath}/config.template ${worksrcpath}/config.default
        copy -force ${filespath}/simutrans.in ${worksrcpath}/simutrans-wrapper
    }
    
    patchfiles          patch-config.default.diff \
                        patch-Makefile.diff \
                        patch-simtypes.h.diff
    
    post-patch {
        reinplace "s|__PREFIX__|${prefix}|g" ${worksrcpath}/Makefile
        reinplace "s|__DATA_DIR__|${data_dir}|g" ${worksrcpath}/simutrans-wrapper
    }
    
    build.args          CC="${configure.cc} [get_canonical_archflags cc]" \
                        CXX="${configure.cxx} [get_canonical_archflags cxx]"
    
    destroot {
        # Install the simutrans data.
        copy ${worksrcpath}/${name} ${destroot}${data_dir}
        
        # Install the simutrans binary. It has to be in the same directory
        # as the data.
        xinstall ${worksrcpath}/build/default/sim ${destroot}${data_dir}
    
        # Install the wrapper script.
        xinstall ${worksrcpath}/simutrans-wrapper ${destroot}${prefix}/bin/simutrans
    }
    
    app.icon            simutrans.ico
    
    # Legacy compatibility variant added 2012-04-22
    variant enable_music requires music description {legacy compatibility variant} {}
    variant music description {allow listening to simutrans music while playing (may reduce speed)} {
        depends_lib-append  port:libsdl_mixer
        post-patch {
            reinplace "s|#BACKEND = mixer_sdl|BACKEND = mixer_sdl|g" \
                ${worksrcpath}/config.default
        }
    }
    
    variant pak64 description {Install the 64x64 graphics pak} {
        depends_run-append port:simutrans-pak64
        notes-append "
${name} is installed with the default pak64 image set.\
If you want to use another pak, download it (for example from\
http://www.simutrans.com/paksets.htm) and put it into\
${data_dir}.
"
    }
    
    if {![variant_isset pak64]} {
        default_variants +pak64
    }
    
    platform powerpc {
        patchfiles-append patch-powerpc.diff
    }
}

subport simutrans-pak64 {
    version             ${branch}
    revision            0
    supported_archs     noarch
    
    description         Standard 64x64 graphics for simutrans
    
    long_description    ${description}
    
    master_sites        sourceforge:project/simutrans/pak64/${my_branch}
    
    distname            simupak64-${my_branch}
    
    checksums           rmd160  ec268f1367d3b07f93b59a31e34f4b6be531f63e \
                        sha256  a58052627e4f4257a8b0abd1622507aa5893ab13ebf38407bf43533b3b228719
    
    post-extract {
        fs-traverse item ${worksrcpath} {
            if {[file isdirectory ${item}]} {
                file attributes ${item} -permissions 0755
            } elseif {[file isfile ${item}]} {
                file attributes ${item} -permissions 0644
            }
        }
    }
    
    build {}
    
    destroot {
        copy ${worksrcpath}/${name} ${destroot}${data_dir}
    }
    
    # Deactivate old simutrans port that had an integrated pak64 that would conflict.
    # Legacy port deactivation hack added 2012-04-22.
    pre-activate {
        if {[file exists ${prefix}/bin/simutrans]
            && ![catch {set vers [lindex [registry_active simutrans] 0]}]
            && [vercmp [lindex $vers 1] 111.2.2] < 0} {
            registry_deactivate_composite simutrans "" [list ports_nodepcheck 1]
        }
    }
}

livecheck.version       ${my_version}
livecheck.regex         /${name}-src-(\[0-9.-\]+)${extract.suffix}