# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0
PortGroup           cmake 1.1
PortGroup           qt6 1.0

github.setup        raspberrypi rpi-imager 2.0.10 v
github.tarball_from releases
revision            0
categories          sysutil aqua
maintainers         {i0ntempest @i0ntempest} openmaintainer
license             Apache-2

description         the official Raspberry Pi Imager
long_description    ${name} is ${description} Utility, a user-friendly tool for creating bootable \
                    media for Raspberry Pi devices.

homepage            https://www.raspberrypi.com/software/

checksums           rmd160  6ae5c9442373d7ff3d9eb047d6404a0020e172f4 \
                    sha256  a78867bbe02ed0f567d747ba5b1a1459dcc7085475d0518af6c54098877c11b5 \
                    size    4581828

distname            ${github.project}_${version}
use_xz              yes

worksrcdir          ${github.project}/src
cmake.build_type    Release

qt6.blacklist       {< 6.9}
qt6.depends_build   qttools
qt6.depends_lib     qtdeclarative \
                    qtsvg


set python_branch   3.14
set python_version  [string map {. ""} ${python_branch}]
set local_json_script \
                    create_local_json.py

depends_build-append \
                    port:pkgconfig
depends_run-append  port:python${python_version}
depends_lib-append  port:libarchive \
                    port:libusb \
                    port:xz \
                    port:zlib \
                    port:zstd

# disable calling of macdeployqt and allow externally linked libs
# disable timezone and country list downloading
patchfiles-append   patch-no-macdeployqt.diff \
                    patch-use-system-libs.diff \
                    patch-bundled-timezones.diff
patch.args          -p2

post-patch {
    reinplace "s|0.0.0-unknown|${github.tag_prefix}${version}|" cmake/GenerateVersion.cmake
    reinplace "s|CMAKE_OSX_DEPLOYMENT_TARGET \"13\"|CMAKE_OSX_DEPLOYMENT_TARGET \"[lindex [split ${macosx_deployment_target} .] 0]\"|" CMakeLists.txt
    reinplace "s|security/Authorization.h|Security/Authorization.h|g" mac/macfile.cpp
    reinplace "s|#!/usr/bin/env python3|#!${prefix}/bin/python${python_branch}|" {*}[glob ${workpath}/${github.project}/doc/*/*.py]
}


configure.args-append \
                    -DQt6_ROOT=${qt6.dir} \
                    -DIMAGER_SIGNED_APP=ON \
                    -DIMAGER_SIGNING_IDENTITY=- \
                    -DVERBOSE_CONFIGURE=ON \
                    -DGENERATE_COUNTRIES_FROM_REGDB=OFF \
                    -DGENERATE_TIMEZONES_FROM_IANA=OFF \
                    -DIMAGER_BUNDLE_QT=OFF \
                    -DIMAGER_USE_SYSTEM_DEPS=ON

destroot {
    set appname [exec /usr/libexec/PlistBuddy -c {Print :CFBundleName} ${build.dir}/${github.project}.app/Contents/Info.plist]
    copy ${build.dir}/${github.project}.app ${destroot}${applications_dir}/${appname}.app
    # cli interface
    set cli_wrapper ${destroot}${prefix}/bin/${github.project}
    set wrapper_fd [open ${cli_wrapper} w]
    puts ${wrapper_fd} "#!/bin/sh"
    puts ${wrapper_fd} ""
    puts ${wrapper_fd} [format {exec "%s/%s.app/Contents/MacOS/%s" --cli "$@"} ${applications_dir} ${appname} ${github.project}]
    close ${wrapper_fd}
    file attributes ${cli_wrapper} -permissions 0755
    # manpage & extra script & docs
    copy ${workpath}/${github.project}/doc ${destroot}${prefix}/share/${github.project}
    ln -s ${prefix}/share/${github.project}/local_json/${local_json_script} ${destroot}${prefix}/bin/${github.project}-[lindex [split ${local_json_script} .] 0]
    ln -s ${prefix}/share/${github.project}/man/${github.project}.1 ${destroot}${prefix}/share/man/man1
}
