#!/bin/sh
# Copyright (C) 2010 - 2018 FARGOS Development, LLC
# buildArch - report build and target architecture names
# $Id$

originDir=`dirname "${0}"`

defaultRootPath="${BT_ROOT_PATH:-/opt/backingTrees}:${SB_TOOLS_ROOT:+:}${SB_TOOLS_ROOT}"
btRootList=""
separator=" "
displayFor=""
# Default, display native architecture
thisOS=`uname -s`
thisCPU=`uname -m`
case "${thisOS}" in
CYGWIN_NT*) # simplify variants
	# CYGWIN_NT-6.3-WOW64 - 64-bit Windows 8.1
	# CYGWIN_NT-10.0 - 64-bit Windows 10
	thisOS="CYGWIN_NT"
	;;
esac


while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [-s separator] [--btroot backingTreeRoot] [{--ba | --ta | --all | --avail | --native | {--base [archName]} | {--subtype [archName]}}]\n" "$0" >&2
		printf "  --all = list all known architecture configurations\n" >&2
		printf "  --avail = list all available architectures on this host\n" >&2
		printf "  --native = display native architecture for this host\n" >&2
		printf "  --ba = list current build architecture\n" >&2
		printf "  --ta = list current target architecture\n" >&2
		printf "  --base = display base architecture (filename permitted)\n" >&2
		printf "  --subtype = display subtype of architecture (filename permitted)\n" >&2
		exit 1
		;;
	-avail | --avail)
		displayFor="avail"
		;;
	-all | --all)
		displayFor="all"
		;;
	-ba | --ba)
		if test -z "${SB_BUILD_ARCH}"
		then
			printf "[notDefined]\n" >&2
			exit 1
		else
			printf "%s\n" "${SB_BUILD_ARCH}"
			exit 0
		fi
		;;
	-ta | --ta)
		if test -z "${SB_TARGET_ARCH}"
		then
			printf "[notDefined]\n" >&2
			exit 1
		else
			printf "%s\n" "${SB_TARGET_ARCH}"
			exit 0
		fi
		;;
	-s)
		separator="${2}"
		shift
		;;
	-bt | --bt | -btroot | --btroot)
		btRootList="${btRootList}${btRootList:+:}${2}"
		shift
		;;
	-native | --native)
		printf "%s.%s\n" "${thisOS}" "${thisCPU}"
		exit 0
		;;
	-base | --base)
		queryArch="${2:-${SB_TARGET_ARCH}}"
		basename ${queryArch} | sed -e 's/targetEnv_//' -e 's/+/ /g' -e 's/_sh.txt//' | awk -F. '{ result=$1; if (NF>1) result = result "." $2; print result }'
		exit 0
		;;
	-subtype | --subtype)
		queryArch="${2:-${SB_TARGET_ARCH}}"
		basename ${queryArch} | sed -e 's/targetEnv_//' -e 's/_sh.txt//' | awk -F. '{ result=$3; for(i=4;i<=NF;i+=1) { result = result "." $i; }  print result }'
		exit 0
		;;
	-*)
		printf "%s: unrecognized option \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		printf "%s: unrecognized argument \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	esac
	shift
done

if test -z "${SB_BUILD_ARCH}"
then
	SB_BUILD_ARCH="${thisOS}.${thisCPU}"
	export SB_BUILD_ARCH
fi

archDefFiles=""
case "${displayFor}" in
all)
	archDefFiles="ALL_supportedArchitectures.txt"
	archDefFiles="${archDefFiles} `hostname -s`_supportedArchitectures.txt"
       	archDefFiles="${archDefFiles} ${SB_BUILD_ARCH}_supportedArchitectures.txt"
	;;
avail)
	archDefFiles="${archDefFiles} `hostname -s`_supportedArchitectures.txt"
       	archDefFiles="${archDefFiles} ${SB_BUILD_ARCH}_supportedArchitectures.txt"
	;;
*)
	# Default, display native architecture
	printf "%s.%s\n" "${thisOS}" "${thisCPU}"
	exit 0
	;;
esac

if test -z "${btRootList}"
then # not set via command line
	btRootList="${defaultRootPath}"
fi
# convert colon-separated path into distinct elements
rootList=`pathedit --space ${btRootList}`
# retrieve data from configuration files
sbDefFiles=""
for btRoot in ${rootList}
do
	for f in ${archDefFiles}
	do
		cFile=`${originDir}/findFileInSB -d "${btRoot}" config/${f} 2>/dev/null`
		sbDefFiles="${sbDefFiles}${sbDefFiles:+ }${cFile}"
		cFile=`${originDir}/findFileInSB -q -all BuildEnv/${f} 2>/dev/null`
		sbDefFiles="${sbDefFiles}${sbDefFiles:+ }${cFile}"
	done
done

if test -z "${sbDefFiles}"
then
	printf "%s: no configuration files located.\n" "${0}" >&2
	if test -z "${SB_BUILD_ARCH}"
	then
		printf "\tYou probably want to do a workon first.\n" >&2
	fi
	exit 1
fi

list=`awk '{ if ((NF > 0) && (substr($1,0,1) != "#")) print $1; }' ${sbDefFiles} | sort | uniq` 
answer=""
for a in ${list}
do
	answer="${answer}${answer:+${separator}}${a}"
done
if test -z "${answer}"
then
	printf "%s: no configuration data could be obtained.\n" "${0}" >&2
	exit 1
fi
echo ${answer}
exit 0
