# -*- 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
PortGroup           github 1.0

github.setup        mas-cli mas 7.0.0 v
github.tarball_from archive
set git-commit      7c70ffd
# This line is for displaying commit in CLI only
revision            1
categories          sysutils
description         Mac App Store command line interface
long_description    A simple command line interface for the Mac App Store. \
                    Designed for scripting and automation.
license             MIT

maintainers         {judaew @judaew} \
                    openmaintainer

checksums           sha256  c40e5d614a2aac0d804b1538782e448af297b314a1fc9785092d3fd2573dd3f2 \
                    rmd160  a3980c1054079edec44ae611c1b829a4310c1267 \
                    size    182666

use_configure       no
use_xcode           yes

# requirements:
# - mas: Swift 6.2 (Xcode 26 or later)
set xcodeversion_min_required 26
pre-fetch {
    if {[vercmp ${xcodeversion} ${xcodeversion_min_required}] < 0} {
        ui_error "${name} @${version} requires Xcode ${xcodeversion_min_required} or later but you have Xcode ${xcodeversion}."
        ui_error "See https://guide.macports.org/chunked/installing.xcode.html for download links."
        return -code error "incompatible Xcode version"
    }
}

post-patch {
    reinplace "s|\$(Scripts/version)|${version}|" ${worksrcpath}/Scripts/prebuild ${worksrcpath}/Scripts/package ${worksrcpath}/Scripts/_setup_script
    reinplace "s|\${MAS_DISTRIBUTION:-unknown}|MacPorts|" ${worksrcpath}/Scripts/prebuild
    reinplace "s|\$(git remote get-url origin)|${git.url}|" ${worksrcpath}/Scripts/prebuild
    reinplace "s|\$(git rev-parse HEAD)|${git-commit}|" ${worksrcpath}/Scripts/prebuild
    reinplace "s|libexec/bin/mas|libexec/${name}/bin/${name}|" ${worksrcpath}/Scripts/${name}
}

build.cmd           swift
build.target        build
build.args          --configuration release \
                    --arch ${configure.build_arch} \
                    --disable-sandbox

post-build {
    system -W ${worksrcpath} "${build.cmd} package --disable-sandbox generate-manual"
}

destroot {
    xinstall -d ${destroot}${prefix}/libexec/${name}/bin
    xinstall -m 0755 ${worksrcpath}/.build/${configure.build_arch}-apple-macosx/release/${name} \
        ${destroot}${prefix}/libexec/${name}/bin/${name}
    
    xinstall -m 0755 ${worksrcpath}/Scripts/${name} \
        ${destroot}${prefix}/bin/${name}

    xinstall -m 0755 ${worksrcpath}/.build/plugins/GenerateManual/outputs/${name}/${name}.1 \
        ${destroot}${prefix}/share/man/man1/
    
    set bash_complete ${prefix}/share/bash-completion/completions
    xinstall -d ${destroot}${bash_complete}
    xinstall -m 0644 ${worksrcpath}/contrib/completion/mas.bash \
        ${destroot}${bash_complete}/${name}

    set fish_complete ${prefix}/share/fish/vendor_completions.d
    xinstall -d ${destroot}${fish_complete}
    xinstall -m 0644 ${worksrcpath}/contrib/completion/mas.fish \
        ${destroot}${fish_complete}/${name}.fish
}

github.livecheck.regex  {([0-9.]+)}
