# -*- 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

name                android-platform-tools
version             35.0.1
categories          java devel
installs_libs       no
maintainers         {judaew @judaew} openmaintainer

homepage            https://developer.android.com/studio/
description         Platform-Tools for Google Android SDK (adb and fastboot)
long_description    {*}${description}
# different files are covered with different licenses. See
# platform-tools/NOTICE.txt for details
license             BSD MIT NCSA Apache-2 GPL-2 LGPL-2.1

master_sites        https://dl.google.com/android/repository
distname            platform-tools_r${version}-darwin
use_zip             yes

checksums           rmd160  ed75c9f283646548ae13f2a09b0be3c090b37905 \
                    sha256  0556ad1d5828b38aee125501493735ec773ab0ffc062a1c77e95ebcfed29a89b \
                    size    12476293

use_configure       no

worksrcdir          platform-tools

# detect which version to depend on based on HW capabilities
if {$subport eq $name} {
    switch ${configure.build_arch} {
        arm64 {
            depends_run port:android-platform-tools-latest
        }
        x86_64 {
            if {![catch {sysctl hw.optional.sse4_1} sse4] && $sse4 == 1} {
                depends_run port:android-platform-tools-latest
            } else {
                depends_run port:android-platform-tools-no_sse4
            }
        }
        i386 {
            depends_run port:android-platform-tools-i386
        }
        default {
            depends_run port:android-platform-tools-latest
        }
    }
}

# define versioned subports
subport android-platform-tools-latest {
    supported_archs x86_64 arm64
}

subport android-platform-tools-no_sse4 {
    version         31.0.3
    description     Platform-Tools for Google Android SDK (adb and fastboot): \
                    legacy version for non-SSE4 64-bit Intel CPUs
    supported_archs x86_64
    set ver_hash    e8b2b4cbe47c728c1e54c5f524440b52d4e1a33c
    distname        ${ver_hash}.platform-tools_r${version}-darwin
    checksums       rmd160  26a2d4602ddd7537798bacd3adf6d5c376dc3e77 \
                    sha256  773c08cfa31cec1bb4568ce5b374366e6310a5ffc21875024604a0f65bc831b1 \
                    size    13227985
    notes-append    "The current version of ${name} requires SSE4.1, which is not \
                    supported by your CPU. The last version compatible with your CPU \
                    (${version}) has been installed instead."
    livecheck.type  none
}

subport android-platform-tools-i386 {
    version         23.0.1
    description     Platform-Tools for Google Android SDK (adb and fastboot): \
                    legacy version for 32-bit Intel CPUs
    supported_archs i386
    distname        platform-tools_r${version}-macosx
    checksums       rmd160  7d0d3fbfdf2bbb8e1d6430373da88c2e77b9c6de \
                    sha256  d2439f5de236c3831c048b678653c5955487351be8e196c65923b4eca5c47692 \
                    size    2489850
    notes-append    "The current version of ${name} requires a 64-bit CPU. The last \
                    version compatible with your CPU (${version}) has been installed \
                    instead."
    livecheck.type  none
}

build {}

if {$subport ne $name} {
    destroot {
        set sdk_dir ${prefix}/share/java/android-sdk-macosx
        xinstall -d ${destroot}${sdk_dir}
        file copy ${worksrcpath} ${destroot}${sdk_dir}/platform-tools

        foreach bin {adb fastboot} {
            ln -s ${sdk_dir}/platform-tools/${bin} ${destroot}${prefix}/bin/${bin}
        }
    }
} else {
    supported_archs noarch
    platforms       any
    distfiles
    destroot {
        xinstall -d  ${destroot}${prefix}/share/doc/${name}/
        system "echo $name is a stub port > ${destroot}${prefix}/share/doc/${name}/ReadMe.txt"
    }
}

pre-activate {
    if {![catch {lindex [registry_active android-platform-tools] 0} installed]} {
        set _version [lindex $installed 1]
        if {[vercmp $_version 34.0.5] < 0} {
            registry_deactivate_composite android-platform-tools "" [list ports_nodepcheck 1]
        }
    }
}

livecheck.url       https://developer.android.com/studio/releases/platform-tools.html
livecheck.regex     <h4 id=".+">(\[0-9\.\]+)
