#
# There are multiple ways that CPU architectured can be named.
# KERN_ARCH is what is used when specifying ARCH=xxx when building the
# kernel.
#
# GCE_ARCH is what gets used when setting specifying the architecture
# when creating the GCE image using the --architecture flag
# ARCH (after being canonicalized by this function) is defined by
# the Debian architecture port names[1] and is used internally
# by gce-xfstests and kvm-xfstests.  So for example, when we add
# support for support for Power architectures, the ARCH name that should
# be used is ppc64 or ppc64el.
#

function set_my_arch () {
    if test -n "${MY_ARCH:=}" ; then
        return
    fi
    local arch=$(uname -m)
    case $arch in
	aarch64)   MY_ARCH=arm64  ;;
	i386|i686) MY_ARCH=i386   ;;
	x86_64)    MY_ARCH=amd64  ;;
	*)         MY_ARCH=$arch  ;;
    esac
}

function set_default_arch ()
{
    if test -n "${ARCH:=}" ; then
	return
    fi
    if test -n "${DEFAULT_ARCH:=}" ; then
	ARCH="$DEFAULT_ARCH"
    else
	set_my_arch
	ARCH="$MY_ARCH"
    fi
}


# This function takes as input a user-supplied architecture (which
# generally should be a Debian port name, but users might use
# a $(uname -m) instead.
#
function set_canonicalized_arch () {
    case "$1" in
	arm64|aarch64)
	    ARCH="arm64"
	    GCE_ARCH="ARM64"
	    KERN_ARCH="arm64"
	    ;;
	i386)
	    ARCH="i386"
	    GCE_ARCH=""
	    KERN_ARCH="i386"
	    ;;
	x86-64|x86_64|X86_64|amd64)
	    ARCH="amd64"
	    GCE_ARCH="X86_64"
	    KERN_ARCH="x86_64"
	    ;;
	riscv64)
	    ARCH="riscv64"
	    GCE_ARCH=""
	    KERN_ARCH="riscv"
	    ;;
	*)
	    echo "Architecture $1 not supported"
	    exit 1
	    ;;
    esac
}

function set_cross_compile ()
{
    set_my_arch
    case "$ARCH" in
	arm64)
	    if test "$MY_ARCH" != arm64 ; then
		CROSS_COMPILE=aarch64-linux-gnu-
	    fi
	    ;;
    esac
}

function get_kernel_file_info () {
    local is_gs=
    KERNEL_ARCH=
    KERNEL_VERSION=
    KERNEL="$1"

    case "$KERNEL" in
	gs://*)
	    is_gs=yes
	    ;;
    esac

    if test -n "$is_gs" ; then
	local f=$(mktemp --tmpdir gs_stat_XXXXXX)
	if gsutil stat "$KERNEL" > "$f" ; then
	    cat "$f" | sed -e '1,/^    Metadata:/d' | \
		sed -e '/^    [A-Z]/,$d' > "$f.new"
	    mv "$f.new" "$f"
	else
	    rm -f "$f"
	    exit 1
	fi
	KERNEL_ARCH=$(cat "$f" | grep arch= | sed -e 's/^ *arch=//' -e 's/: *$//')
	KERNEL_VERSION=$(cat "$f" | grep ver= | sed -e 's/^ *ver=//' -e 's/: *$//')
	rm -f "$f"
    else
	if test ! -f "$KERNEL" ; then
	    echo "get-kernel-arch: file not found: $KERNEL" >&2
	    exit 1
	fi
	info=$(file -m "$DIR/util/kernel-magic" "$KERNEL" | sed -e "s;$KERNEL: ;;")
	case "$info" in
	    "Linux kernel"*)
		KERNEL_ARCH=$(echo $info | tr A-Z a-z |awk '{print $3}')
		d=$(dirname $(dirname $(dirname $(dirname "$KERNEL"))))
		if test -f "$d/.git_version" ; then
		    KERNEL_VERSION=$(cat "$d/.git_version")
		fi
		if test -f "$d/.config" ; then
		    if test "$KERNEL_ARCH" = "x86" ; then
			KERNEL_ARCH=$(head "$d/.config" | grep "^#.*Kernel Configuration" | \
				   awk '{print $2}' | sed -e 's;Linux/;;')
			if test "$KERNEL_ARCH" = "x86_64" ; then
			    KERNEL_ARCH=amd64
			fi
		    fi
		fi
		;;
	    "Debian binary package"*)
		KERNEL_ARCH=$(dpkg -I "$KERNEL" | grep "^ Architecture: " | \
			   awk '{print $2}')
		KERNEL_VERSION=$(dpkg -I "$KERNEL" | grep "^ Version: " | \
			  awk '{print $2}')
		;;
	    *)
		echo "get-kernel-arch $i is not a kernel" >&2
		echo "$info"
		exit 1
	esac
    fi
}
