# -*- 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 2.2.1 v
github.tarball_from archive
set git-commit      92cdd8d
# This line is for displaying commit in CLI only
revision            0
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         {kimuraw @kimuraw} \
                    {judaew @judaew} openmaintainer

checksums           sha256  1b883b1a501023d9d61eab0de8a8104830e45a563ecab9d0c925f7f00bc6215d \
                    rmd160  c7b0a25d3c3f3bd14d90cf254cce224f140e8f93 \
                    size    348389

use_configure       no
use_xcode           yes

# requirements:
# - mas: Xcode 11.4 or later
set xcodeversion_min_required 11.4
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|\$(script/version)|${version}|" ${worksrcpath}/script/generate_package_swift
    reinplace "s|\${1:-unknown}|MacPorts|" ${worksrcpath}/script/generate_package_swift
    reinplace "s|\$(git remote get-url origin)|${git.url}|" ${worksrcpath}/script/generate_package_swift
    reinplace "s|\$(git rev-parse HEAD)|${git-commit}|" ${worksrcpath}/script/generate_package_swift
}

pre-configure {
    system -W ${worksrcpath} "./script/generate_package_swift"
}

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

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

    set bash_complete ${prefix}/share/bash-completion/completions
    xinstall -d ${destroot}${bash_complete}
    xinstall -m 0644 ${worksrcpath}/contrib/completion/mas-completion.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}${bash_complete}/${name}.fish
}

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