# -*- 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.1
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  3f5b7c796818d4ab9ef63517678161c5286f45e0 \
                    sha256  9a283925d85a41f483997f43213738e9b12a091bf94da20b0f94fe990903725d \
                    size    262793 \
                    ${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
    # xcodes completion for fish shell included in fish 4.1
    set comp_path(bash) ${destroot}${prefix}/share/bash-completion/completions/${name}
    #set comp_path(fish) ${destroot}${prefix}/share/fish/completions/${name}.fish
    set comp_path(zsh)  ${destroot}${prefix}/share/zsh/site-functions/_${name}
    foreach shell {bash zsh} {
        xinstall -d [file dirname $comp_path(${shell})]
        system -W ${worksrcpath}/.build/release "./${name} --generate-completion-script=${shell} > $comp_path(${shell})"
    }
}
