#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# 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.

set -eE
set -o nounset
set -o pipefail

SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
source "${SCRIPT_ROOT}/eks-d-common"

build::log::setup

function build::install::binary() {
    local -r file="$1"
    local __result=$2

    build::common::rpm_package_for_binary $file "__rpm_package"
    build::common::filename_from_rpm $file "__expected_filename"

    local -r final_bin=$NEWROOT$__expected_filename

    if [ -f $final_bin ]; then
        echo "${__expected_filename} already exists, skipping."
        eval $__result="''"
        return
    fi

    echo "Installing ${__expected_filename} from ${__rpm_package}"

    if [ -z "$__rpm_package" ]; then
        echo "Error: RPM not found for $file!"
        exit 1
    fi

    if [[ "$__expected_filename" != "$file" ]]; then
        echo "Warning: Provided filename does not match actual: $file != $__expected_filename"
    fi

    local -r extract_dir=$DOWNLOAD_DIR/$__rpm_package

    # download/extract rpm and add to the final rpm db which will be included in the final image
    build::common::extract_rpm $__rpm_package $extract_dir

    local extracted_bin=$extract_dir$__expected_filename

    if [ ! -f "$extracted_bin" ]; then
        extracted_bin=$extract_dir${__expected_filename#/usr}
        if [ ! -f "$extracted_bin" ]; then
            echo "Error: Filename not included in RPM!"
            exit 1
        fi
    fi

    # copy desired binary into newroot folder path
    echo "Copying $extracted_bin to $final_bin"

    # if we are installing a bin that was copied into fakeroot for scriptlets
    # we need to remove the link before copying
    [ -L $final_bin ] && unlink $final_bin

    mkdir -p $(dirname $final_bin)
    cp $extracted_bin $final_bin

    build::install::configs_and_licenses $extract_dir

    eval $__result="'$final_bin'"
}

function build::install::configs_and_licenses() {
    local -r extract_dir="$1"

    # this is probably not perfect, but try and perserve any txt files, like conf files which may be neccessary
    # for the exec being copied.
    # also grabs licenses files
    cp -rf $extract_dir/etc $NEWROOT 2>/dev/null || :
    cp -rf $extract_dir/usr/share/{doc,man,licenses} $NEWROOT/usr/share 2>/dev/null || :
}


for file in "$@"; do
    build::install::binary $file installed_bin

    if [ -n "${installed_bin}" ]; then 
        build::common::install_deps_for_binary $installed_bin
    fi
done