##
##  terraform.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_opkg  0.13.3
%define       V_dist  20200917

#   package information
Name:         terraform
Summary:      Building, Changing and Combining Infrastructure
URL:          https://terraform.io/
Vendor:       Hashicorp
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Filesystem
License:      MPL/2.0
Version:      %{V_opkg}.%{V_dist}
Release:      20200918

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/terraform/terraform-%{V_dist}.tar.xz

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Terraform is a tool for building, changing, and versioning
    infrastructure safely and efficiently. Terraform can manage existing
    and popular service providers as well as custom in-house solutions.

%track
    prog terraform:base = {
        version   = %{V_opkg}
        url       = https://github.com/hashicorp/terraform/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }
    prog terraform:dist = {
        version   = %{V_dist}
        url       = http://download.openpkg.org/components/versioned/terraform/
        regex     = terraform-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n terraform

%build
    #   build program
    export GOPATH=`pwd`
    (   cd $GOPATH/src/github.com/hashicorp/terraform
        rm -f config_windows.go signal_windows.go config_test.go
        go build -v -o terraform.exe *.go
    ) || exit $?

    #   build plugins
    libexec=`pwd`/plugins
    %{l_shtool} mkdir -f -p -m 755 $libexec
    for plugin in \
        terraform-providers/terraform-provider-external \
        terraform-providers/terraform-provider-local \
        terraform-providers/terraform-provider-template \
        terraform-providers/terraform-provider-tls \
        terraform-providers/terraform-provider-http \
        terraform-providers/terraform-provider-dns \
        terraform-providers/terraform-provider-archive \
        terraform-providers/terraform-provider-nomad \
        terraform-providers/terraform-provider-terraform \
        terraform-providers/terraform-provider-consul \
        terraform-providers/terraform-provider-vault \
        terraform-providers/terraform-provider-docker \
        terraform-providers/terraform-provider-kubernetes \
        terraform-providers/terraform-provider-openstack \
        terraform-providers/terraform-provider-rundeck \
        terraform-providers/terraform-provider-aws \
        terraform-providers/terraform-provider-profitbricks \
        terraform-providers/terraform-provider-google \
        terraform-providers/terraform-provider-azure \
        terraform-providers/terraform-provider-heroku \
        terraform-providers/terraform-provider-github \
        terra-farm/terraform-provider-virtualbox \
        Mastercard/terraform-provider-restapi \
        GSLabDev/terraform-provider-httpfileupload \
        scottwinkler/terraform-provider-shell \
        dikhan/terraform-provider-openapi \
    ; do
        (   cd $GOPATH/src/github.com/$plugin
            go build -v
            exe=`echo $plugin | sed -e 's;.*/;;'`
            mv $exe $libexec/
        ) || exit $?
    done

%install
    #   install program
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin
    %{l_shtool} install -c -s -m 755 \
        src/github.com/hashicorp/terraform/terraform.exe \
        $RPM_BUILD_ROOT%{l_prefix}/bin/terraform
    %{l_shtool} install -c -s -m 755 \
        plugins/* \
        $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

