#include "SH_platform.h"


#if defined(__aarch64__)
	#define THIS_ARCH "arm64"
#elif defined(__arm__) || defined(__TARGET_ARCH_ARM)
	#if defined(__ARM_ARCH_7__) \
		|| defined(__ARM_ARCH_7A__) \
		|| defined(__ARM_ARCH_7R__) \
		|| defined(__ARM_ARCH_7M__) \
		|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7)
		#define THIS_ARCH "armv7"
	#elif defined(__ARM_ARCH_6__) \
		|| defined(__ARM_ARCH_6J__) \
		|| defined(__ARM_ARCH_6T2__) \
		|| defined(__ARM_ARCH_6Z__) \
		|| defined(__ARM_ARCH_6K__) \
		|| defined(__ARM_ARCH_6ZK__) \
		|| defined(__ARM_ARCH_6M__) \
		|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6)
		#define THIS_ARCH "armv6"
	#elif defined(__ARM_ARCH_5TEJ__) \
		|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5)
		#define THIS_ARCH "armv5"
	#else
		#define THIS_ARCH "arm"
	#endif
#elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
	#define THIS_ARCH "i386"
#elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
	#define THIS_ARCH "x86_64"
#elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
	#define THIS_ARCH "ia64"
#elif defined(__ppc__) || defined(__ppc) || defined(__powerpc__) \
	  || defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC) \
	  || defined(_M_MPPC) || defined(_M_PPC)
	#if defined(__ppc64__) || defined(__powerpc64__) || defined(__64BIT__)
		#define THIS_ARCH "ppc64"
	#else
		#define THIS_ARCH "ppc"
	#endif
#endif
#ifndef THIS_ARCH
#  define THIS_ARCH "unknown"
#endif


int thisOS() {
	return (int)SHPlatform::
#ifdef WINDOWS
							Windows;
#else
#  ifdef BLACKBERRY
							Blackberry;
#  else
#    ifdef MAC_OS
							MacOS;
#    else
#      ifdef ANDROID
							Android;
#      else
#        ifdef FREE_BSD
							FreeBSD;
#        else
#          ifdef IOS
							iOS;
#          else
#            ifdef QNX
							QNX;
#            else
							Linux;
#            endif
#          endif
#        endif
#      endif
#    endif
#  endif
#endif
}


PIString thisArch() {
	static PIString ret = THIS_ARCH;
	return ret;
}


PIString hostType(int os, const PIString & arch) {
	return PIString::fromNumber(os) + arch;
}


PIString thisHostType() {
	return hostType(thisOS(), thisArch());
}


int OSByName(PIString os) {
	os = os.trim().toLowerCase();
	if (os == PIStringAscii("windows")) return SHPlatform::Windows;
	if (os == PIStringAscii("blackberry")) return SHPlatform::Blackberry;
	if (os == PIStringAscii("darwin") || os == PIStringAscii("macos")) return SHPlatform::MacOS;
	if (os == PIStringAscii("android")) return SHPlatform::Android;
	if (os == PIStringAscii("freebsd")) return SHPlatform::FreeBSD;
	if (os == PIStringAscii("ios") || os == PIStringAscii("iphone")) return SHPlatform::iOS;
	if (os == PIStringAscii("qnx")) return SHPlatform::QNX;
	if (os == PIStringAscii("linux")) return SHPlatform::Linux;
	return SHPlatform::Unknown;
}
