##
##  go.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 information
Name:         go
Summary:      The Go Programming Language
URL:          http://golang.org/
Vendor:       Robert Griesemer, Rob Pike, Ken Thompson
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Language
License:      BSD
Version:      1.25.3
Release:      20251014

#   list of sources
Source0:      http://golang.org/dl/go%{version}.src.tar.gz
Patch0:       go.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, make, bison, flex
PreReq:       OpenPKG, openpkg >= 20160101, gcc, x509
BuildPreReq:  go-cc
PreReq:       go-cc

%description
    Go is a concurrent garbage-collected system programming language,
    designed 2007 by Robert Griesemer, Rob Pike and Ken Thompson at
    Google. Go is expressive, concise, clean, and efficient. Its
    concurrency mechanisms make it easy to write programs that get
    the most out of multicore and networked machines, while its novel
    type system enables flexible and modular program construction. Go
    compiles quickly to machine code yet has the convenience of garbage
    collection and the power of run-time reflection. It's a fast,
    statically typed, compiled language that feels like a dynamically
    typed, interpreted language.

%track
    prog go = {
        version   = %{version}
        url       = http://golang.org/dl/
        regex     = go(\d+\.\d+(?:\.\d+)?)\.src\.tar\.gz
    }

%prep
    %setup -q -n go
    %patch -p0

%build
    #   patch the patch
    %{l_shtool} subst %{l_value -s -a} \
        src/crypto/x509/root_linux.go \
        src/crypto/x509/root_bsd.go

    #   configure package
    export GOCACHE="`pwd`"
    export GOROOT="`pwd`"
    export GOBIN="`pwd`/bin"
    export GOROOT_FINAL="%{l_prefix}/lib/go"
    if [ -x %{l_prefix}/lib/go/bin/go ]; then
        export GOROOT_BOOTSTRAP="%{l_prefix}/lib/go"
    elif [ -x %{l_prefix}/lib/go117/bin/go ]; then
        export GOROOT_BOOTSTRAP="%{l_prefix}/lib/go117"
    elif [ -x %{l_prefix}/lib/go-bootstrap/bin/go ]; then
        export GOROOT_BOOTSTRAP="%{l_prefix}/lib/go-bootstrap"
    else
        ( echo "ATTENTION! Because Go version 1.5 and higher is written in Go itself you"
          echo "either need an already (older) installed \"go\" package or you have to install"
          echo "at least once the \"go117\" or \"go-bootstrap\" package for bootstrapping."
        ) | %{l_rpmtool} msg -b -t error
        exit 1
    fi
    export GOOS=""
    export GOARCH=""
    case "%{l_platform -t}" in
        *-freebsd* ) GOOS="freebsd" ;;
        *-linux*   ) GOOS="linux"   ;;
        *-macosx*  ) GOOS="darwin"  ;;
    esac
    case "%{l_platform -t}" in
        ix86-*  ) GOARCH="386"   ;;
        amd64-* ) GOARCH="amd64" ;;
    esac
    if [ ".$GOOS" = . -o ".$GOARCH" = . ]; then
        echo "ERROR: platform not supported" 1>&2
        exit 1
    fi

    #   provide the dedicated C/C++ compiler wrappers
    #   (which is also available later for Go to compile)
    export CC="%{l_prefix}/bin/go-cc"
    export CXX="%{l_prefix}/bin/go-c++"

    #   build program
    %{l_shtool} mkdir -f -p -m 755 $GOBIN
    cd src
    %{l_bash} make.bash

%install
    #   cleanup source tree
    rm -rf bin-tmp
    rm -f favicon.ico
    rm -rf [0-9][0-9] [0-9][a-f] [a-f][0-9] [a-f][a-f]
    rm -rf [A-R]* doc misc test

    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/lib/go

    #   install program
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/lib/go/

    #   install wrapper executables
    for prg in go gofmt; do
        ( echo "#!/bin/sh"
          echo "if [ \".\$GOCACHE\" = . ]; then"
          echo "    GOCACHE=\`pwd\`"
          echo "    export GOCACHE"
          echo "fi"
          echo "exec %{l_prefix}/lib/go/bin/$prg \${1+\"\$@\"}"
        ) >$prg
        %{l_shtool} install -c -m 755 \
            $prg $RPM_BUILD_ROOT%{l_prefix}/bin/$prg
    done

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

%files -f files

%clean

