#!/bin/sh

set -eu

. /usr/share/geordi/_functions

Echo_info "Using g++: ${GXX}"

if [ ! -x "${GXX}" ]
then
	Echo_error "g++ binary '${GXX}' is not executable"
	exit 1
fi

Echo_debug "Copying compiler into chroot"

GCC_VERSION=$(${GXX} -v 2>&1 | awk '/^gcc version/ { print $3 }')
if [ -z "${GCC_VERSION}" ]
then
	Echo_error "Could not determine g++ version"
	exit 1
fi

Echo_debug "Using g++ version: ${GCC_VERSION}"
Echo_debug "Target architecture: $(${GXX} -dumpmachine)"

Copy_gcc_binary () {
	local BINARY
	BINARY="${1}"

	Echo_debug "Copying ${BINARY} and its dependencies"

	Copy_file "${BINARY}"
	Copy_libs_for_binary "${BINARY}"
}

Copy_gcc_binary "${GXX}"
Copy_gcc_binary "$(which as)"
Copy_gcc_binary "$(which ld)"
Copy_gcc_binary "$(${GXX} -print-prog-name=cc1plus)"
