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

name                    terraform
categories              sysutils
installs_libs           no
# Hashicorp Terraform is now licensed under the BSL
# https://www.hashicorp.com/license-faq#aug-10-announcement
license                 Commercial Permissive
homepage                https://www.terraform.io/downloads.html

maintainers             {emcrisostomo @emcrisostomo} \
                        {gmail.com:herby.gillot @herbygillot} \
                        openmaintainer

# *NOTE* Remember to update `latestVersion` on a version upgrade.
set latestVersion       terraform-1.15

set armAvailable        no
set customDistSubDir    yes

proc terraformBaseVersion {} {
    global subport
    return [lindex [split ${subport} "-"] 1]
}

proc terraformVersion {} {
    global patchNumber
    return [terraformBaseVersion].${patchNumber}
}

proc terraformDistBase {} {
    global name
    return ${name}_[terraformVersion]_darwin
}

subport terraform-1.15 {
    set patchNumber     8
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  14e6966259e1ac54f508045e3a9462acaaa1c553 \
                        sha256  e2e812e783771159bf758fd4e55d6dc9bb08f63e2af2c63d212721807a02c5dc \
                        size    36512579 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  659233d7088f2b5b3b04b364f864dfde396df23c \
                        sha256  f210110c5698b94d803a7a63cdb0251b5455c150841478808e2bbb343f95ed68 \
                        size    32885003

    set customDistSubDir no
}

subport terraform-1.14 {
    set patchNumber     9
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  1d506fea64304427e5c37ccde467b765c20036d6 \
                        sha256  c15326e1af102d2767d40208a0157d1402057f80192991f56803b66457304cf3 \
                        size    36082119 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  b67710d4ce92ced4b7a30082a48b00aa1bc0c85a \
                        sha256  5bc0b11b7a63c8984a41d82523356df46f7833c2e9651a39a7f8919422de5cde \
                        size    32492805

    set customDistSubDir no
}

subport terraform-1.13 {
    set patchNumber     5
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  1db10c7cba1cf50f5b281908ec261480a3001c59 \
                        sha256  92f76865230cbe6bb747e49cb3dc5b44a054324bbdd1a080bb127b326b94c404 \
                        size    30967731 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  1766f8eb1858dccc858106a4f7dba8653b27fc89 \
                        sha256  1bf942231235e7e1a4c38c6d7b820e54f526ac487f87d19f0c4a425c6ddb62cb \
                        size    28338858

    set customDistSubDir no
}

subport terraform-1.12 {
    set patchNumber     2
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  1a371ade046435de8fb11ff202cb45776f770423 \
                        sha256  c65aa74bed1dbb1c48ba4bbab11f08e7f7eeb54a422146561490275340468f19 \
                        size    29657917 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  b2994e8e24f2456754b78a3851c31fe60f64fe06 \
                        sha256  1ca02f336ff4f993d6441806d38a0bcc0bbca0e3c877b84c9c2dc80cfcd0dc8b \
                        size    27132505

    set customDistSubDir no
}

subport terraform-1.11 {
    set patchNumber     4
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  caacf2390caa33d920d4a3451fa869a91daf1a74 \
                        sha256  a56d5002b9f7647291faccc3dd1a70350e60fb61e4c45037629508b8fdc2575b \
                        size    28780134 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  f56b099ae10b2f6fac3cec052efbc5219f083e19 \
                        sha256  867e0808fa971217043e25b7a792b10720c79b1546f8a68479b74f138be73e18 \
                        size    26394448

    set customDistSubDir no
}

subport terraform-1.10 {
    set patchNumber     5
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  afce0ceb7b390191a93e1b8bb5e38a4776f830f0 \
                        sha256  4164db242076c7e99ac6aec67bc42b366925b97267e3883edaca4e4e408b082e \
                        size    28635071 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  c8797855917b96503c83b51df9535367f020d86b \
                        sha256  51910553dae21dab7f5f55912129602fd902e68a6b6d84b326a556a3e140b98b \
                        size    26251749

    set customDistSubDir no
}

subport terraform-1.9 {
    set patchNumber     8
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  2817f8603b83295702e44894e4a8d2d520977107 \
                        sha256  be591e8c59c49d0cfbc7664d24910a4b43840b89d0a4bbca662149bbf0397e91 \
                        size    27959329 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  bc13cc6255e81b5acdfb12eba1293ebba2b5570b \
                        sha256  873d7b925d08578fb6bb9c12c7cd92ae73e289e07c360f2fdd69f9036b7baaab \
                        size    25745690

    set customDistSubDir no
}

subport terraform-1.8 {
    set patchNumber     5
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  02be902b4283abd925c53c262659c46cc386a41e \
                        sha256  051c702e156a4d1a1c628783cf2ca0e1db8cca7b4c0f1686ea623558ed5560f9 \
                        size    27659471 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  34b4a8868ba8ecf4556f411b83101ad9a3346a42 \
                        sha256  627c5005ab4a2bee36316f4967a41f16d55f79ea737f78b6bb34325c728c73e1 \
                        size    25454927

    set customDistSubDir no
}

subport terraform-1.7 {
    set patchNumber     5
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  e275422df8da1d6a44f84713131842e2b16e07b0 \
                        sha256  0eaf64e28f82e2defd06f7a6f3187d8cea03d5d9fcd2af54f549a6c32d6833f7 \
                        size    27560118 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  fe2bd475f4f3dd1e568fae5fcb1aeea2440a441e \
                        sha256  99c4d4feafb0183af2f7fbe07beeea6f83e5f5a29ae29fee3168b6810e37ff98 \
                        size    25902918

    set customDistSubDir no
}

subport terraform-1.6 {
    set patchNumber     6
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  c170c3c03cf7783ad7317622c399df433988a53e \
                        sha256  33376343c7e0279b674c1c8b8a31dc3174ac09dd796d32651cc5e3b98f220436 \
                        size    26507656 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  f5b9c6b353ef7a03c3d2f2c080a47335ef758cb5 \
                        sha256  01e608fc04cf54869db687a212d60f3dc3d5c828298514857f9e29f8ac1354a9 \
                        size    24865571

    set customDistSubDir no
}

subport terraform-1.5 {
    set patchNumber     7
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  d7c50065f08a09a34603caef28c003e164afd35a \
                        sha256  b310ec0e626e9799000cfc8e30247cd827cf7f8030c8e0400257c7f111e93537 \
                        size    22296467 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  6a845150b5f6ba25292df61097ae21ed7847d96a \
                        sha256  db7c33eb1a446b73a443e2c55b532845f7b70cd56100bec4c96f15cfab5f50cb \
                        size    21020511

    set customDistSubDir no
}

subport terraform-1.4 {
    set patchNumber     7
    revision            0

    set armAvailable    yes

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  c8b0bfb7af57bad9d02d8823b7fb9efff6ca4588 \
                        sha256  603764c07862bd3a87fce193f8b9823383df22626b254f353c83511635763301 \
                        size    22051376 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  b6df404982deb2d117e9b61abb263cfc7ab78e80 \
                        sha256  4b2ae04467469b923d038e6720ae1f92cb2adaa96b7ab08199c2fffee8b45baa \
                        size    20612022

    set customDistSubDir no
}

subport terraform-1.3 {
    set patchNumber     10
    revision            0

    set armAvailable    yes

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  b8f26c8d223d6994cbab22bc83bb7190ba807386 \
                        sha256  e5cf68ef9b259503abf515a1716fcfe21d46af22e24b8ebbbe7849fbfafb428c \
                        size    21195636 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  400bb1a0fa0ee0c09257ffe51e58508737ff74cf \
                        sha256  39cf7882108034f78c0d9144153271efb11ba99924170828eda9b0196f3da6fd \
                        size    19794701

    set customDistSubDir no
}

subport terraform-1.2 {
    set patchNumber     9
    revision            2

    set armAvailable    yes

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  02ba5fe97460e154341311b9a7730cd0774d8d45 \
                        sha256  84a678ece9929cebc34c7a9a1ba287c8b91820b336f4af8437af7feaa0117b7c \
                        size    21672810 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  64b51950c5c8d14099b5ff58cd9656101817b611 \
                        sha256  bc3b94b53cdf1be3c4988faa61aad343f48e013928c64bfc6ebeb61657f97baa \
                        size    20280541
}

subport terraform-1.1 {
    set patchNumber     9
    revision            1

    set armAvailable    yes

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  2629b4a4d65c551cfacd3299652d2c64da9fd588 \
                        sha256  685258b525eae94fb0b406faf661aa056d31666256bf28e625365a251cb89fdc \
                        size    20850638 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  a4b25d61936984263845a50ba578c3bfccc8b629 \
                        sha256  39fac4be74462be86b2290dd09fe1092f73dfb48e2df92406af0e199cfa6a16c \
                        size    20093184
}

subport terraform-1.0 {
    set patchNumber     11
    revision            1

    set armAvailable    yes

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           [terraformDistBase]_amd64.zip \
                        rmd160  fe6a5e94830ae8def4ce95d335235e7f36405776 \
                        sha256  551a16b612edaae1037925d0e2dba30d16504ff4bd66606955172c2ed8d76131 \
                        size    19422757 \
                        [terraformDistBase]_arm64.zip \
                        rmd160  83be809223dbb6b990aebc939031602d9a8b3749 \
                        sha256  737e1765afbadb3d76e1929d4b4af8da55010839aa08e9e730d46791eb8ea5a6 \
                        size    18467868
}

subport terraform-0.15 {
    set patchNumber     5
    revision            1

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           rmd160  2468e59f0385d606b8a77651069474cc06c319be \
                        sha256  5ad75ed3def05f36b5c5dab97dee00b5d3d86be9f9dcd205b48136312505f3fc \
                        size    33932321
}

subport terraform-0.14 {
    set patchNumber     11
    revision            1

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           rmd160  88eb139f672f0771f25ef8bd49e08b23945f3385 \
                        sha256  88d266a53b4c09e9778123f274351d7f6e48c08c12edaece8f4e360ade3bd847 \
                        size    34764932
}

subport terraform-0.13 {
    set patchNumber     7
    revision            1

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           rmd160  7fb4ef9b32db35fe7674d8d5ca20b534e99dbb1f \
                        sha256  60f3561eb11fa6c61321d6c8b698023eb7b74dc1a49210bd5f5acb03f453db9b \
                        size    35823339
}

subport terraform-0.12 {
    set patchNumber     31
    revision            1

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           rmd160  06f2c7d2147afd485ad478635c3a55335e4b99ee \
                        sha256  ebd96d0c1fc206480a61a190059d3e8aebdfa8733bfa309d7a34ad5a3e0eb322 \
                        size    29365299
}

subport terraform-0.11 {
    set patchNumber     15
    revision            1

    # stealth update; remove for next version
    dist_subdir         ${name}/${subport}_${revision}_1

    checksums           rmd160  cacd0de975ff689cd0efa237bd88e1839f90aeeb \
                        sha256  9c3214dcaa277c3773d52d514a76959c82896a1661061b7e5f9523c38add10b7 \
                        size    22024460
}

subport terraform_select {}

if {${subport} eq ${name}} {
    PortGroup           obsolete 1.0

    replaced_by         ${latestVersion}
    version             1.15.8
    revision            0

} elseif {${subport} eq "terraform_select"} {
    version             0.0.0
    revision            0
    supported_archs     noarch
    platforms           any

    description         Common files for selecting the default terraform \
                        version
    long_description    This port installs files that allow 'port select' \
                        to create links to the preferred default version \
                        of terraform.
    homepage            https://www.macports.org/
    
    conflicts           tenv

    distfiles
    use_configure       no
    build {}

    destroot {
        select::install terraform ${filespath}/base
        select::install terraform ${filespath}/none
    }

    livecheck.type     none

} else {
    set baseVersion     [terraformBaseVersion]
    set baseName        terraform${baseVersion}

    version             [terraformVersion]

    distname            [terraformDistBase]_amd64
    supported_archs     x86_64

    if ${armAvailable} {
        supported_archs x86_64 arm64

        if {${build_arch} eq "arm64"} {
            distname    [terraformDistBase]_arm64
        }
    }

    if ${customDistSubDir} {
        dist_subdir   ${name}/${version}_${revision}
    }

    depends_run         port:terraform_select

    description         A tool for building, changing, and versioning \
                        infrastructure safely and efficiently.
    long_description \
                        Terraform is an infrastructure as code software \
                        by HashiCorp. It allows users to define a datacenter \
                        infrastructure in a high-level configuration language, \
                        from which it can create an execution plan to build \
                        the infrastructure in a service provider such as AWS.

    master_sites        https://releases.hashicorp.com/${name}/${version}

    use_configure       no
    use_zip             yes
    extract.mkdir       yes

    select.group        terraform
    select.file         ${workpath}/${baseName}

    build {
        file copy ${filespath}/terraformX.YY.template ${workpath}/${baseName}
        reinplace "s|@@BASE_VERSION@@|${baseVersion}|g" ${workpath}/${baseName}
    }

    destroot {
        xinstall ${worksrcpath}/terraform \
            ${destroot}${prefix}/bin/terraform${baseVersion}
    }

    set regexVersionPart [string map {. {\.}} $baseVersion]
    livecheck.type      regex
    livecheck.url       https://releases.hashicorp.com/terraform/
    livecheck.regex     >terraform_(${regexVersionPart}\\.\\d+)<

    notes "
    To make this the default terraform run:
        sudo port select --set terraform ${baseName}
    "
}
