#!/bin/sh
#$Id: set_current_arch_type,v 1.22 2023/03/15 23:11:40 marcum Exp $
#FUNCTION

# ==============================================================================
# Set current arch_type function
#
# set_current_arch_type [-x86] [-base base_sys_arch_type] [-known_arch_types]
#
#	-x86			: set MacOSX machine type from arm64 to x86_64
#	-base base_sys_arch_type: set arch_type for base_sys_arch_type instead
#				  of that for current system
#
#	Set arch type variables:
#
#		OS_type
#		machine_type
#		arch_mode
#		base_arch_type
#		base_sys_arch_type
#		def_arch_type
#		arch_type
#
# 	-known_arch_types	: set known arch types variable
#
#		known_arch_types
#
# ==============================================================================

set_current_arch_type ()
{

#-------------------------------------------------------------------------------
# default
#-------------------------------------------------------------------------------

	arch_mode=$1
	base_arch_type=
	base_sys_arch_type=
	def_arch_type=
	arch_type=

#-------------------------------------------------------------------------------
# Set input arguments
#-------------------------------------------------------------------------------

set_known_arch_types=

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

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

		-x86)		arch_mode=$1
				shift 1
				;;

		-known_arch_types)
				set_known_arch_types=yes
				shift 1
				;;

		-base)		base_sys_arch_type=$2
				shift 2
				;;

		*)		shift 1
				;;
	esac
done

#-------------------------------------------------------------------------------
# Set list of known types
#-------------------------------------------------------------------------------

	if [ "${set_known_arch_types}" = "yes" ]; then

		known_arch_types="\
WIN=\
WIN:\
WIN64;Windows 64-bit on x86-64;gfortran,gcc,g++!\
Linux-arm=\
Linux-arm:\
Linux-arm64;Linux 5.15 or higher with gcc 11.3.0 or higher on arm64;gfortran,gcc,g++!\
Linux-x86=\
Linux-x86:\
Linux-x86-64;Linux 3.10 or higher with gcc 4.8.5 or higher on x86-64;gfortran,gcc,g++!\
MacOSX-arm=\
MacOSX-arm:\
MacOSX-arm64;MacOSX 11.0 or higher on arm64;gfortran,clang,clang++!\
MacOSX-x86=\
MacOSX-x86:\
MacOSX-x86-64;MacOSX 10.13 or higher on x86-64;gfortran,clang,clang++!\
"

		return
	fi

#-------------------------------------------------------------------------------
# set OS and machine type
#-------------------------------------------------------------------------------

	if [ "${base_sys_arch_type}" = "" ]; then

		OS_type=`uname -s`
		machine_type=`uname -m`
	else
		OS_type=
		machine_type=
	fi

#-------------------------------------------------------------------------------
# WIN32/WIN64
#-------------------------------------------------------------------------------

	if [ "${base_sys_arch_type}" = "WIN" ] || [ "`echo ${OS_type} | grep CYGWIN`" != "" ] || [ "`echo ${OS_type} | grep MINGW`" != "" ]; then

		base_arch_type=WIN
		base_sys_arch_type=WIN
		def_arch_type="${base_arch_type}64"
		arch_type="${base_arch_type}64"

#-------------------------------------------------------------------------------
# Linux-arm, Linux-x86
#-------------------------------------------------------------------------------

	elif [ "${base_sys_arch_type}" = "Linux-arm" ] || [ "${base_sys_arch_type}" = "Linux-x86" ] || [ "$OS_type" = "Linux" ]; then

		if [ "${base_sys_arch_type}" = "Linux-arm" ] || [ "${machine_type}" = "aarch64" ]; then
			base_arch_type=Linux-arm64
			base_sys_arch_type=Linux-arm
		else
			base_arch_type=Linux-x86-64
			base_sys_arch_type=Linux-x86
		fi

		def_arch_type=${base_arch_type}
		arch_type=${base_arch_type}

#-------------------------------------------------------------------------------
# MacOSX-arm, MacOSX-x86
#-------------------------------------------------------------------------------

	elif [ "${base_sys_arch_type}" = "MacOSX-arm" ] || [ "${base_sys_arch_type}" = "MacOSX-x86" ] || [ "$OS_type" = "Darwin" ]; then

		if [ "${base_sys_arch_type}" = "MacOSX-arm" ] || [ "${machine_type}" = "arm64" ]; then
			base_arch_type=MacOSX-arm64
			base_sys_arch_type=MacOSX-arm
			def_arch_type=${base_arch_type}
			arch_type=${base_arch_type}
			if [ "${arch_mode}" = "-x86" ]; then
				arch_type=MacOSX-x86-64
			else
				arch_type=${base_arch_type}
			fi
		else
			base_arch_type=MacOSX-x86-64
			base_sys_arch_type=MacOSX-x86
			def_arch_type=${base_arch_type}
			arch_type=${base_arch_type}
		fi
	fi

#-------------------------------------------------------------------------------
}
