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

PortSystem          1.0
PortGroup           github 1.0

github.setup        XcodesOrg xcodes 2.0.2
github.tarball_from archive
revision            0
categories          devel
license             MIT
maintainers         {i0ntempest @i0ntempest} openmaintainer
supported_archs     x86_64 arm64

description         The best command-line tool to install and switch between multiple versions of Xcode
long_description    {*}${description}

set dep_git_tag_prefix \
                    v
set xcodeskit_ver   1.0.3
set xcodesloginkit_ver \
                    1.0.0
set xcodeskit_distfile \
                    XcodesKit-${xcodeskit_ver}${extract.suffix}
set xcodesloginkit_distfile \
                    XcodesLoginKit-${xcodesloginkit_ver}${extract.suffix}
set main_distfile   ${distfiles}
master_sites-append https://github.com/XcodesOrg/XcodesKit/archive/${dep_git_tag_prefix}${xcodeskit_ver}/:xcodeskit \
                    https://github.com/XcodesOrg/XcodesLoginKit/archive/${dep_git_tag_prefix}${xcodesloginkit_ver}/:xcodesloginkit
distfiles-append    ${xcodeskit_distfile}:xcodeskit \
                    ${xcodesloginkit_distfile}:xcodesloginkit

checksums           ${main_distfile} \
                    rmd160  dcaa8279486017d896abc8605eb443027c51c47b \
                    sha256  67db730edd1c768f39c197dc8e8054bd22d0859de2dd96e49b525148579be907 \
                    size    266659 \
                    ${xcodeskit_distfile} \
                    rmd160  c189919270fc61ec4fae2b462ab9e3442499a942 \
                    sha256  b8b1740467752421515cc741de2e066f104c66ac0c70fc8e7676816261c37685 \
                    size    80075 \
                    ${xcodesloginkit_distfile} \
                    rmd160  bba2781e2f2cd696b86da100351a92355f9b7e66 \
                    sha256  d0e25a892b03c272a533f7e9ea7f9ea9f6bbd34c51dbfef1d0069f5787e154a6 \
                    size    37501

post-extract {
    ln -s {*}[glob -tails -directory ${workpath} XcodesKit-${xcodeskit_ver}] ${workpath}/XcodesKit
    ln -s {*}[glob -tails -directory ${workpath} XcodesLoginKit-${xcodesloginkit_ver}] ${workpath}/XcodesLoginKit
}

use_configure       no
use_xcode           yes

build.type          xcode
build.cmd           swift
build.args          --configuration release \
                    --disable-sandbox
build.target        build

platform darwin {
    if { ${os.major} < 21 } {
        known_fail  yes
        pre-fetch {
            ui_error "${subport} requires macOS 12 or later"
            return -code error "incompatible macOS version"
        }
    }
}

destroot {
    xinstall -m 755 ${worksrcpath}/.build/release/xcodes ${destroot}${prefix}/bin/
    # All supported shells: bash, fish, zsh
    set comp_path(bash) ${destroot}${prefix}/share/bash-completion/completions/${name}
    set comp_path(fish) ${destroot}${prefix}/share/fish/vendor_completions/${name}.fish
    set comp_path(zsh)  ${destroot}${prefix}/share/zsh/site-functions/_${name}
    foreach shell {bash fish zsh} {
        xinstall -d [file dirname $comp_path(${shell})]
        system -W ${worksrcpath}/.build/release "./${name} --generate-completion-script=${shell} > $comp_path(${shell})"
    }
}
