# $Id: Portfile 92478 2012-04-29 23:29:03Z ryandesign@macports.org $

PortSystem          1.0

name                simutrans
version             111.2.2
set branch          [join [lrange [split ${version} .] 0 1] .]
set my_version      [strsed ${version} {g/\./-/}]
set my_branch       [strsed ${branch} {g/\./-/}]
categories          games
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} == ${subport}} {
    PortGroup           app 1.0
    
    revision            1
    
    master_sites        sourceforge:project/simutrans/simutrans/${my_version}
    
    distname            ${name}-src-${my_version}
    
    checksums           rmd160  306f5b618dc5190f3a8b31462c44e6db8c1e5ea6 \
                        sha256  e97279ab7149cef430bb53c6206786ff039915dfa98a1b597e61e3f192840732
    
    depends_build       port:pkgconfig
    
    depends_lib         port:libpng \
                        path:lib/pkgconfig/sdl.pc: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
    
    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}
    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  40b5c0e57427eda83ef357a1bbb8cdb5250a79ac \
                        sha256  5cda187d578e5263a1a77f8cd09268e2694157f6ca4c3ad4921b3fc8710fea38
    
    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 simutrans "" [list ports_nodepcheck 1]
        }
    }
}
