From cad8dcbad24c6fc2d2afc1ec3a72a18c44733169 Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Fri, 12 Aug 2016 14:45:27 +0200
Subject: [PATCH] Emit a warning if found multiple core for same arch

Also try to avoid failure by choosing one randomly (from a user POW this is perfectly acceptable since the erratic behaviour is due to a bug in Board Manager core). The warning reports the full path of the folder for easy copy/paste operations
---
 constants/constants.go |  1 +
 hardware_loader.go     |  8 +++++++-
 utils/utils.go         | 10 +++++-----
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/constants/constants.go b/constants/constants.go
index d83ee273..2381cbd3 100644
--- a/constants/constants.go
+++ b/constants/constants.go
@@ -152,6 +152,7 @@ const LOG_LEVEL_WARN = "warn"
 const MSG_ARCH_FOLDER_NOT_SUPPORTED = "'arch' folder is no longer supported! See http://goo.gl/gfFJzU for more information"
 const MSG_ARCHIVING_CORE_CACHE = "Archiving built core (caching) in: {0}"
 const MSG_BOARD_UNKNOWN = "Board {0} (platform {1}, package {2}) is unknown"
+const MSG_BOARD_MULTIPLE_CORES = "Folder {0} contains {1} packages for {2} architecture, please manually remove the unwanted ones"
 const MSG_BOOTLOADER_FILE_MISSING = "Bootloader file specified but missing: {0}"
 const MSG_BUILD_OPTIONS_CHANGED = "Build options changed, rebuilding all"
 const MSG_CANT_FIND_SKETCH_IN_PATH = "Unable to find {0} in {1}"
diff --git a/hardware_loader.go b/hardware_loader.go
index d22d0b3b..6ee4a442 100644
--- a/hardware_loader.go
+++ b/hardware_loader.go
@@ -32,6 +32,7 @@ package builder
 import (
 	"os"
 	"path/filepath"
+	"strconv"
 
 	"github.com/arduino/arduino-builder/constants"
 	"github.com/arduino/arduino-builder/i18n"
@@ -132,11 +133,16 @@ func loadPackage(targetPackage *types.Package, folder string, logger i18n.Logger
 
 		_, err := os.Stat(filepath.Join(subfolderPath, constants.FILE_BOARDS_TXT))
 		if err != nil && os.IsNotExist(err) {
-			theOnlySubfolder, err := utils.TheOnlySubfolderOf(subfolderPath)
+			theOnlySubfolder, numSubfolders, err := utils.TheOnlySubfolderOf(subfolderPath)
 			if err != nil {
 				return i18n.WrapError(err)
 			}
 
+			if numSubfolders > 1 {
+				i18n.ErrorfWithLogger(logger, constants.MSG_BOARD_MULTIPLE_CORES,
+					subfolderPath, strconv.Itoa(numSubfolders), platformId)
+			}
+
 			if theOnlySubfolder != constants.EMPTY_STRING {
 				subfolderPath = filepath.Join(subfolderPath, theOnlySubfolder)
 			}
diff --git a/utils/utils.go b/utils/utils.go
index 318b3984..600650bc 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -295,17 +295,17 @@ func ReadFileToRows(file string) ([]string, error) {
 	return strings.Split(txt, "\n"), nil
 }
 
-func TheOnlySubfolderOf(folder string) (string, error) {
+func TheOnlySubfolderOf(folder string) (string, int, error) {
 	subfolders, err := ReadDirFiltered(folder, FilterDirs)
 	if err != nil {
-		return constants.EMPTY_STRING, i18n.WrapError(err)
+		return constants.EMPTY_STRING, 0, i18n.WrapError(err)
 	}
 
-	if len(subfolders) != 1 {
-		return constants.EMPTY_STRING, nil
+	if len(subfolders) == 0 {
+		return constants.EMPTY_STRING, 0, nil
 	}
 
-	return subfolders[0].Name(), nil
+	return subfolders[0].Name(), len(subfolders), nil
 }
 
 func FilterOutFoldersByNames(folders []os.FileInfo, names ...string) []os.FileInfo {