# -*- 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                warzone2100
set pretty_name     "Warzone 2100"
categories          games
license             GPL-2+
maintainers         @jasonliu--

homepage            https://wz2100.net
if {$subport eq ${name}} {
    description     3-D real-time strategy game
}
subport ${name}-music  {
    description     music soundtrack files for $pretty_name
}
subport ${name}-videos {
    description     in-game cinematic sequences for $pretty_name
}
long_description    In $pretty_name, you take on the role of a member \
                    of a group named \"The Project\", in a battle to \
                    rebuild the world after mankind has nearly been \
                    destroyed by nuclear missiles. \
                    \
                    \n\nThe game offers campaign, tutorial, and \
                    single- and multi-player skirmish modes. An \
                    extensive tech tree with over 400 different \
                    technologies, combined with the unit design \
                    system, allows for a wide variety of possible \
                    units and tactics.

if {$subport eq ${name} || $subport eq "${name}-music"} {
    PortGroup           github 1.0

}
if {$subport eq ${name}} {
    PortGroup           cmake 1.1
    PortGroup           legacysupport 1.1

    github.setup        Warzone2100 ${name} 4.3.0
    revision            7
    github.tarball_from releases
    distname            ${name}_src
    use_xz              yes
    dist_subdir         ${name}/${version}

    checksums           rmd160  ead5dccf8860d2096e506d94d41e14eba0ae6fdb \
                        sha256  417665f4cc678e0cbf2f27905a8fbf0bc51771a0138385abec54d31254582cb3 \
                        size    320501020

    set port_libfmt     libfmt9
    cmake.module_path-append \
        ${prefix}/lib/${port_libfmt}/cmake

    depends_build-append    path:bin/pkg-config:pkgconfig \
                            port:gettext \
                            port:asciidoctor
    depends_lib-append      port:libzip \
                            port:miniupnpc \
                            port:gettext-runtime \
                            port:curl \
                            port:sqlite3 \
                            port:sqlitecpp \
                            port:re2 \
                            port:${port_libfmt} \
                            port:discord-rpc \
                            port:libpng \
                            port:libsdl2 \
                            port:openal-soft \
                            port:libogg \
                            port:libvorbis \
                            port:libtheora \
                            port:libopus \
                            port:libsodium \
                            port:libutf8proc \
                            port:fribidi \
                            port:physfs \
                            port:freetype \
                            port:glm \
                            path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                            port:zlib
    depends_run-append      port:${name}-music \
                            port:${name}-videos

    # This (sub)port is getting flagged with a '"warzone2100" is not
    # distributable because its license "gpl" conflicts with license
    # "OpenSSL" of dependency "openssl11"' on the buildbot builders.
    # However, warzone2100 is in fact NOT dependent on openssl11; the
    # curl port has already been transitioned to using openssl3, and the
    # port that is still dependent on openssl11 is asciidoctor, one of
    # the tools that warzone2100 is using during the build process.
    license_noconflict      asciidoctor

    worksrcdir              ${name}

    # re2 pulls in abseil, which needs C++17 now.
    compiler.cxx_standard   2017
    compiler.thread_local_storage yes

    patchfiles-append       patch-cxx17.diff \
                            patch-context.h.diff \
                            patch-std-apply.diff

    # Fix compile errors on macOS <= 10.10 due to files included from
    # /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1
    legacysupport.newest_darwin_requires_legacy 14
    legacysupport.use_mp_libcxx yes

    post-patch {
        # The source code resets the CMAKE_MODULE_PATH, instead of
        # adding to the list. This causes CMake to not be able to
        # find any packages that are available through MacPorts Cmake.
        reinplace {s/^set.\(CMAKE_MODULE_PATH.*PROJECT_SOURCE_DIR.*cmake\)/list(PREPEND \1/} \
            ${worksrcpath}/CMakeLists.txt

        # Prevent the upstream source from trying to compile these as
        # internal libraries, since we have made them available as
        # dependencies through MacPorts.
        foreach dep [list utf8proc fmt discord-rpc SQLiteCpp] {
            reinplace -E \
                "/add_subdirectory.$dep/,/set_property.*$dep/d" \
                ${worksrcpath}/3rdparty/CMakeLists.txt
        }
        reinplace "/target_link_lib.*fmt/i\\
            target_link_libraries(${name} utf8proc)\\
            find_package(fmt REQUIRED)\\
        " \
            ${worksrcpath}/src/CMakeLists.txt

        reinplace -E {s|(#include.*)utf8proc/utf8proc|\1utf8proc|} \
            ${worksrcpath}/lib/framework/wzstring.cpp

        reinplace \
            {/SET.RE2_BUILD_TESTING/,/set_property.*re2.*XCODE_ATTRIBUTE_WARNING_CFLAGS/d} \
            ${worksrcpath}/3rdparty/CMakeLists.txt
        reinplace {s/re2::re2/re2/} \
            ${worksrcpath}/src/CMakeLists.txt \
            ${worksrcpath}/lib/netplay/CMakeLists.txt

        reinplace "/if.ENABLE_DISCORD/a\\
            \\        find_package(DiscordRPC)\\
        " \
            ${worksrcpath}/3rdparty/CMakeLists.txt \
            ${worksrcpath}/src/CMakeLists.txt \
            ${worksrcpath}/src/integrations/CMakeLists.txt
        reinplace {s/\(TARGET discord-rpc\)/DiscordRPC_FOUND OR \1/} \
            ${worksrcpath}/src/CMakeLists.txt \
            ${worksrcpath}/src/integrations/CMakeLists.txt
        foreach re [list \
            {/target_include_directories.*3rdparty/s/".*"/"${DiscordRPC_INCLUDE_DIR}"/} \
            {/target_link_libraries.*discord-rpc/s/discord-rpc/"${DiscordRPC_LIBRARY}"/} \
        ] {
            reinplace $re ${worksrcpath}/src/integrations/CMakeLists.txt
        }

        reinplace {/include_directories.*glm/d} \
            ${worksrcpath}/CMakeLists.txt

        # The following method to create a conditional to test for an
        # Apple Clang version number was taken from the mpiutil 1.0
        # PortGroup.
        if {${configure.compiler} eq "clang"} {
            set compiler_version \
                [compiler.command_line_tools_version \
                 ${configure.compiler}]
            if {[vercmp $compiler_version 900.0.37] < 0} {
                # The '-Wunguarded-availability' flag was introduced in
                # Xcode 9, so if we are compiling using an older Xcode,
                # we need to remove this #pragma line.
                reinplace {/Wunguarded-availability/d} \
                    ${worksrcpath}/lib/framework/cocoa_wrapper.mm
            }
        }
        if {${os.platform} eq "darwin" && ${os.major} <= 13} {
            reinplace {/pragma.*push/,/pragma.*pop/d} \
                ${worksrcpath}/lib/framework/cocoa_wrapper.mm
        }

        # Compile Objective-C files with
        # Automatic Reference Counting turned on
        reinplace "/cocoa_wrapper.mm/i\\
            \\        add_compile_options(\"-fobjc-arc\")\\
        " \
            ${worksrcpath}/lib/framework/CMakeLists.txt
        reinplace "/cocoa_wz_menus.mm/i\\
            \\        add_compile_options(\"-fobjc-arc\")\\
        " \
            ${worksrcpath}/lib/sdl/CMakeLists.txt

        # Remove the "Check for a newer version" button
        reinplace {/Check for a newer version/d} \
            ${worksrcpath}/src/frontend.cpp
    }

    configure.args-append   -DWZ_DISTRIBUTOR=MacPorts \
                            -DWZ_ENABLE_WARNINGS=ON \
                            -DWZ_ENABLE_WARNINGS_AS_ERRORS=OFF \
                            -DWZ_ENABLE_BACKEND_VULKAN=OFF
    #                                                 ^
    # Unfortunately, we have to disable compiling with Vulkan support,
    # because currently the vulkan ports are only at version 1.1.112,
    # and warzone requires one that is newer.

    post-build {
        if {![catch {exec grep MACOSX_BUNDLE_BUNDLE_NAME \
            ${worksrcpath}/src/CMakeLists.txt} result]} \
        {
            regexp {MACOSX_BUNDLE_BUNDLE_NAME "(.*)"} $result match \
                executable_name
        }
        if {![catch {exec grep MACOSX_BUNDLE_GUI_IDENTIFIER \
            ${worksrcpath}/src/CMakeLists.txt} result]} \
        {
            regexp {MACOSX_BUNDLE_GUI_IDENTIFIER "(.*)"} $result match \
                bundle_identifier
        }
        foreach re [list \
            "s/\$(EXECUTABLE_NAME)/$executable_name/" \
            "s/\$(PRODUCT_BUNDLE_IDENTIFIER)/$bundle_identifier/" \
            "s/\$(MACOSX_DEPLOYMENT_TARGET)/${macosx_deployment_target}/" \
        ] {
            reinplace $re ${build.dir}/src/Warzone-Info.plist
        }
    }

    post-destroot {
        move "${destroot}${prefix}/$pretty_name.app" \
             ${destroot}${applications_dir}/
        set app_contents "${applications_dir}/$pretty_name.app/Contents"
        delete -force ${destroot}$app_contents/Resources/data
        ln -s ${prefix}/share/${name} \
              ${destroot}$app_contents/Resources/data
        delete -force ${destroot}$app_contents/Resources/docs
        ln -s ${prefix}/share/doc/${name} \
              ${destroot}$app_contents/Resources/docs
        ln -s "$app_contents/MacOS/$pretty_name" \
              ${destroot}${prefix}/bin/${name}
        delete -force ${destroot}${prefix}/share/${name}/music

        # For whatever unknown reason, the upstream source code doesn't
        # overwrite the CMake-generated Info.plist file, after going to
        # the trouble of generating one themselves. So we do it manually
        # here.
        xinstall -m 644 ${build.dir}/src/Warzone-Info.plist \
            ${destroot}$app_contents/Info.plist
    }
}

if {$subport eq "${name}-music"} {
    set subport_version 0.0.0
    revision            1
    platforms           any
    supported_archs     noarch

    long_description-append \
                        \n\nThis package ($subport) contains the music \
                        soundtrack files.

    variant standard conflicts high \
        description {Install standard-quality music soundtrack} \
    {
        github.setup    Warzone2100 data-music-opus \
                        81ae966cc68c242a1452ad1f168fb02bab82acea
        version         $subport_version

        checksums       rmd160  95f18b1b31f640842a1d085d22195f3354c6873e \
                        sha256  1a86e9b08999b3a13c484a81a3c2dee7922a71bd243ef1e2607a66859c27189d \
                        size    87292224
    }
    variant high conflicts standard \
        description {Install high-quality music soundtrack} \
    {
        github.setup    Warzone2100 data-music-opus-hq \
                        41dc5aac2afe5de04b2de2dc8cf699e8cbbae131
        version         $subport_version

        checksums       rmd160  7601e86b019e0895fd3a07cf5475b63ea239d0e8 \
                        sha256  2afd7c59fa02749ecad718da26cb0f99c5d9103eb6c73bb0f37917d985c5ab46 \
                        size    184129091
    }

    default_variants    +high

    pre-fetch {
        if {![variant_isset high] && ![variant_isset standard]} {
            ui_error "You must install this port with one of the audio quality variants selected!"
            return -code error "No audio quality variant selected"
        }
    }
    use_configure no
    build {}
    destroot {
        set music_dir ${prefix}/share/${name}/music
        xinstall -d ${destroot}$music_dir
        xinstall -m 644 ${worksrcpath}/menu.opus ${destroot}$music_dir/
        copy ${worksrcpath}/albums ${destroot}$music_dir/
    }
}

if {$subport eq "${name}-videos"} {
    version             0.0.0
    revision            0
    platforms           any
    supported_archs     noarch

    long_description-append \
                        \n\nThis package ($subport) contains the \
                        in-game video sequences. All videos were made \
                        from the original 320x160 source material. \
                        \n* The high quality videos are just upscaled \
                        and filtered. Do not expect miracles. \
                        \n* The standard quality videos are pretty \
                        close to the original videos, plus some \
                        filtering. \
                        \n* The low quality videos use a much lower \
                        bitrate.

    distname            sequences
    # Note: A .wz file is just a .zip with a custom file extension
    extract.suffix      .wz

    set sf_vids_subdir  project/${name}/${name}/Videos
    variant high conflicts standard low \
        description {Install high quality videos} \
    {
        set vidquality  high-quality
        set vidlang     en
        master_sites    sourceforge:$sf_vids_subdir/$vidquality-$vidlang
        dist_subdir     ${name}/$vidquality-$vidlang
    }
    variant standard conflicts high low \
        description {Install standard quality videos} \
    {
        set vidquality  standard-quality
        if {[variant_isset de]} {
            set vidlang de
        } else {
            set vidlang en
        }
        master_sites    sourceforge:$sf_vids_subdir/$vidquality-$vidlang
        dist_subdir     ${name}/$vidquality-$vidlang
    }
    variant low conflicts high standard \
        description {Install low quality videos} \
    {
        set vidquality  low-quality
        set vidlang     en
        master_sites    sourceforge:$sf_vids_subdir/$vidquality-$vidlang
        dist_subdir     ${name}/$vidquality-$vidlang
    }
    variant de requires standard conflicts high low \
        description {Installieren Sie die Videosequenzen mit den deutschsprachigen Audiodaten} {}

    default_variants    +high

    if {[variant_isset high]} {
        checksums       rmd160  ac26f1e00849b84cc5b761a3e5471e733d8d3ef1 \
                        sha256  90ff552ca4a70e2537e027e22c5098ea4ed1bc11bb7fc94138c6c941a73d29fa \
                        size    964260204
    } elseif {[variant_isset standard]} {
        if {[variant_isset de]} {
            checksums   rmd160  1a5e557507b057dde28859da628cffe8317b6768 \
                        sha256  d285c85ffa57a5663ac11073ef954afb97097089306877860e8e9d9119116fd8 \
                        size    499187492
        } else {
            checksums   rmd160  7350fa47b0dc0db631ad22763039e29bf420a847 \
                        sha256  142ae905be288cca33357a49f42b884c190e828fc0b1b1773ded5dff774f41a3 \
                        size    571937134
        }
    } elseif {[variant_isset low]} {
        checksums       rmd160  88ea130925e4a886f0063dc923c794aef068519a \
                        sha256  4a1939a1e401f3b0f68736ff19689abed5e1e9d3dfe4fc07b04953c0dfaf7ad6 \
                        size    169607304
    }

    pre-fetch {
        if {![variant_isset high] && ![variant_isset standard] && \
            ![variant_isset low]} \
        {
            ui_error "You must install this port with one of the video quality variants selected!"
            return -code error "No video quality variant selected"
        }
    }
    extract {}
    use_configure no
    build {}
    destroot {
        xinstall -d ${destroot}${prefix}/share/${name}
        xinstall -m 644 ${distpath}/${distfiles} \
            ${destroot}${prefix}/share/${name}/
    }
}