#!/bin/sh
#$Id: output_arch_type,v 1.8 2023/02/27 19:10:30 marcum Exp $
#FUNCTION

# ==============================================================================
# Function to output arch_type information
#
# output_arch_type [-a,-ab,-b,-s,-m,-o] [-x86] [-base base_sys_arch_type]
#
# -a		: Output all known architecture types.
# -ab		: Output all known base architecture types.
# -as		: Output all known base system architecture types.
# -b		: Output current base architecture type.
# -s		: Output current base system architecture type.
# -m		: Output current machine type.
# -o		: Output current architecture type [default].
# -x86		: Set machine type to x86-64 on arm64.
# -base base_sys_arch_type
#		: Output current information as if the current base system 
#		  architecture was base_sys_arch_type
# ==============================================================================

output_arch_type ()
{

#-------------------------------------------------------------------------------
# check arguments
#-------------------------------------------------------------------------------

	arch_mode=
	base_sys_arch_type=
	list_arch_type="-o"

	while [ X"$1" != X-- ]; do

		case "$1" 
		in      
			"")		set -- -- $@
					;;

			-x86)		arch_mode="$1"
					shift 1
					;;

			-base)		base_sys_arch_type_flag="$1 $2"
					shift 2
					;;

			*)		list_arch_type="$1"
					shift 1
					;;
		esac
	done

#-------------------------------------------------------------------------------
# output all known arch_types
#-------------------------------------------------------------------------------

	if [ "${list_arch_type}" = "-a" ]; then

		set_current_arch_type -known_arch_types

		list=

		ifs_def="$IFS"

		IFS="!"

		for info in ${known_arch_types}; do

			IFS="="

			set -- ${info}

			base_sys_label=$1

			info="$2"

			IFS=":"

			set -- ${info}

			base_label=$1

			shift 1

			known_sub_types="$*"

			for info in ${known_sub_types}; do

				IFS=";"

				set -- ${info}

				label="$1"

				IFS="${ifs_def}"

				if [ "$label" != "" ]; then

					if [ "$list" = "" ]; then

						list="$label"
					else

						list="$list $label"
					fi
				fi
			done

			IFS="!"
		done

		IFS="${ifs_def}"

		echo "$list"

#-------------------------------------------------------------------------------
# output all known base_arch_types
#-------------------------------------------------------------------------------

	elif [ "${list_arch_type}" = "-ab" ]; then

		set_current_arch_type -known_arch_types

		list=

		ifs_def="$IFS"

		IFS="!"

		for known_type in ${known_arch_types}; do

			IFS="="

			set -- ${known_type}

			base_sys_label=$1

			known_type="$2"

			IFS=":"

			set -- ${known_type}

			base_label=$1

			IFS="${ifs_def}"

			if [ "${base_label}" != "" ]; then

				if [ "$list" = "" ]; then

					list="${base_label}"
				else

					list="$list ${base_label}"
				fi
			fi

			IFS="!"
		done

		IFS="${ifs_def}"

		echo "$list"

#-------------------------------------------------------------------------------
# output all known base_sys_arch_types
#-------------------------------------------------------------------------------

	elif [ "${list_arch_type}" = "-as" ]; then

		set_current_arch_type -known_arch_types

		list=

		ifs_def="$IFS"

		IFS="!"

		for known_type in ${known_arch_types}; do

			IFS="="

			set -- ${known_type}

			base_sys_label=$1

			IFS="${ifs_def}"

			if [ "${base_sys_label}" != "" ] && [ "${base_sys_label}" != "${previous}" ]; then

				if [ "$list" = "" ]; then

					list="${base_sys_label}"
				else

					list="$list ${base_sys_label}"
				fi

				previous="${base_sys_label}"
			fi

			IFS="!"
		done

		IFS="${ifs_def}"

		echo "$list"

#-------------------------------------------------------------------------------
# output base_arch_type
#-------------------------------------------------------------------------------

	elif [ "${list_arch_type}" = "-b" ]; then

		set_current_arch_type ${base_sys_arch_type_flag}

		echo "${base_arch_type}"

#-------------------------------------------------------------------------------
# output base_sys_arch_type
#-------------------------------------------------------------------------------

	elif [ "${list_arch_type}" = "-s" ]; then

		set_current_arch_type ${base_sys_arch_type_flag}

		echo "${base_sys_arch_type}"

#-------------------------------------------------------------------------------
# output machine_type
#-------------------------------------------------------------------------------

	elif [ "${list_arch_type}" = "-m" ]; then

		set_current_arch_type

		echo "${machine_type}"

#-------------------------------------------------------------------------------
# output arch_type
#-------------------------------------------------------------------------------

	else

		set_current_arch_type ${arch_mode} ${base_sys_arch_type_flag}

		echo "${arch_type}"

#-------------------------------------------------------------------------------

	fi
}
