#!/bin/bash
#
# Provide --all XOR --all-default XOR (--config-tags XXX)*; if config-tags
# provided, the --all args ignored
#
# EXAMPLES:
#		./ScriptsLib/GetConfigurations
#		./ScriptsLib/GetConfigurations --config-tags "Windows"
#		./ScriptsLib/GetConfigurations --config-tags "Windows 32" --quiet
#		./ScriptsLib/GetConfigurations --all-default
#		./ScriptsLib/GetConfigurations --all
#		./ScriptsLib/GetConfigurations --cross-compiled-only
#
get_script_dir () {
     #https://www.ostricher.com/2014/10/the-right-way-to-get-the-directory-of-a-bash-script/
     SOURCE="${BASH_SOURCE[0]}"
     # While $SOURCE is a symlink, resolve it
     while [ -h "$SOURCE" ]; do
          DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
          SOURCE="$( readlink "$SOURCE" )"
          # If $SOURCE was a relative symlink (so no "/" as prefix, need to resolve it relative to the symlink base directory
          [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
     done
     DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
     echo "$DIR"
}
MY_PATH=$(get_script_dir)
QUIET=0
ALL=1
ALLDEFAULT=0
CROSS_COMPILED_ONLY=0

CONFIG_TAG_RESTRICTION=""
while [[ $# -gt 0 ]]; do
  case "$1" in
    --config-tags)
	  if [[ $# -eq 1 ]]; then
		echo Expected another argument
		exit 1;
	  fi
      CONFIG_TAG_RESTRICTION=$2
      shift 2
      ;;
    --quiet)
      QUIET=1
      shift 1
      ;;
    --all)
      ALL=1
      ALLDEFAULT=0
      shift 1
      ;;
    --all-default)
      ALL=0
      ALLDEFAULT=1
      shift 1
      ;;
    --cross-compiled-only)
      CROSS_COMPILED_ONLY=1
      shift 1
      ;;
    --help)
      echo "GetConfigurations returns all configurations; GetConfigurations --config-tags 'abc def' returns all configurations with BOTH the abc and def tags; --all-default returns only default for current platform configurations; --cross-compiled-only returns only cross-compiled configurations; optional --quiet flag suppresses warnings"
      exit 0
      ;;
    --) # end argument parsing
      shift
      break
      ;;
    -*|--*=) # unsupported flags
      echo "Error: Unsupported flag $1" >&2
      exit 1
      ;;
  esac
done

SORT=/usr/bin/sort
if [ ! -e $SORT ]; then
	SORT=sort
fi
#NB: need full path top find cuz of winDoze
if [ -e "$MY_PATH/../ConfigurationFiles/" ] ; then
	#/usr/bin/find "$MY_PATH/../ConfigurationFiles/" -mindepth 1 -maxdepth 1 -type f -print0 | xargs -0 -I {} basename "{}" .xml | tr '\n' ' '
	RAW_CONFIGS=`/usr/bin/find "$MY_PATH/../ConfigurationFiles/" -type f -exec basename \{} .xml \; | $SORT | tr '\n' ' '`
fi
# Take the raw configs, and apply restrictions to produce the list NEW_CONFIGS
if [ "$CONFIG_TAG_RESTRICTION" == "" ] ; then
  if [ $ALL -eq 1 ]; then
  	NEW_CONFIGS="$RAW_CONFIGS"
  fi
  if [ $ALLDEFAULT -eq 1 ]; then
    UNAME=`$MY_PATH/DetectedHostOS`
    if [ "$UNAME" == "" ]; then
      UNAME=`uname`
    fi
    NEW_CONFIGS=""
    for c in $RAW_CONFIGS ; do
      BBD=`$MY_PATH/../ScriptsLib/GetConfigurationParameter $c BuildByDefault`
      if [ "$BBD" == 'always' ] || [ "$BBD" == "$UNAME" ]; then
        NEW_CONFIGS="$NEW_CONFIGS $c"
      fi
    done
  fi
else
	NEW_CONFIGS=""
	for c in $RAW_CONFIGS ; do
		ITS_TAGS=`$MY_PATH/../ScriptsLib/GetConfigurationParameter $c ConfigTags`
		#echo c=$c, and itsTAGS = $ITS_TAGS
		allMatch=1
		for t in $CONFIG_TAG_RESTRICTION ; do
      itMatched=0
      for ct in $ITS_TAGS ; do
        if [[ "$ct" == "$t" ]]; then
          itMatched=1
        fi
      done
      if [[ $itMatched -eq 0 ]]; then
        allMatch=0
      fi
		done
		if [ $allMatch -eq 1 ]; then
			NEW_CONFIGS="$NEW_CONFIGS $c"
		fi
	done
fi
if [ $CROSS_COMPILED_ONLY -eq 1 ]; then
  NEW_CONFIGS2=""
  for c in $NEW_CONFIGS ; do
    if [ `$MY_PATH/../ScriptsLib/GetConfigurationParameter $c CrossCompiling` == 'true' ]; then
      NEW_CONFIGS2="$NEW_CONFIGS2 $c"
    fi
  done
  NEW_CONFIGS=$NEW_CONFIGS2
fi
# Print the final NEW_CONFIGS list
echo $NEW_CONFIGS
if [ "$NEW_CONFIGS" == "" ] ; then
  #not necessarily a bug/mistake, but often a typo;
  if [ "$QUIET" == "0" ] ; then
    echo "Warning: no tags match $CONFIG_TAG_RESTRICTION" >&2
  fi
fi
