#!/bin/sh
#
# Copyright © 2020 – 2022 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


if [ "$#" -ne 7 ]; then
    echo "go-build-wrapper: wrong arguments" >&2
    echo "Usage: go-build-wrapper [SOURCE DIR]" >&2
    echo "                        [OUTPUT ROOT DIR]" >&2
    echo "                        [OUTPUT FILE]" >&2
    echo "                        [VERSION]" >&2
    echo "                        [C COMPILER]" >&2
    echo "                        [DYNAMIC LINKER]" >&2
    echo "                        [MIGRATION PATH FOR COREOS/TOOLBOX]" >&2
    exit 1
fi

if ! cd "$1"; then
    echo "go-build-wrapper: failed to enter source directory $1"
    exit 1
fi

tags=""
if $7; then
    tags="-tags migration_path_for_coreos_toolbox"
fi

if ! libc_dir=$("$5" --print-file-name=libc.so); then
    echo "go-build-wrapper: failed to read the path to libc.so" >&2
    exit 1
fi

if ! libc_dir_canonical=$(readlink --canonicalize "$libc_dir"); then
    echo "go-build-wrapper: failed to canonicalize the path to libc.so" >&2
    exit 1
fi

if ! libc_dir_canonical_dirname=$(dirname "$libc_dir_canonical"); then
    echo "go-build-wrapper: failed to read the dirname of the canonicalized path to libc.so" >&2
    exit 1
fi

if ! dynamic_linker_basename=$(basename "$6"); then
    echo "go-build-wrapper: failed to read the basename of dynamic linker $6" >&2
    exit 1
fi

if ! dynamic_linker_canonical=$(readlink --canonicalize "$6"); then
    echo "go-build-wrapper: failed to canonicalize dynamic linker $6" >&2
    exit 1
fi

if ! dynamic_linker_canonical_dirname=$(dirname "$dynamic_linker_canonical"); then
    echo "go-build-wrapper: failed to read the dirname of the canonicalized dynamic linker $dynamic_linker_canonical" >&2
    exit 1
fi

dynamic_linker="/run/host$dynamic_linker_canonical_dirname/$dynamic_linker_basename"

# shellcheck disable=SC2086
go build \
        $tags \
        -trimpath \
        -ldflags "-extldflags '-Wl,-dynamic-linker,$dynamic_linker -Wl,-rpath,/run/host$libc_dir_canonical_dirname' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$4" \
        -o "$2/$3"

exit "$?"
