From fdb6bbab7b94f189a9d7f2a16a7d475545eb8450 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kristoffer=20=C3=96dmark?= <kristoffer.odmark90@gmail.com>
Date: Fri, 6 Apr 2018 12:06:05 +0200
Subject: [PATCH] Footprint Wizard now also handles custom Env paths

---
 common/env_paths.cpp                | 17 +++++++++++++----
 include/env_paths.h                 | 16 ++++++++++++++--
 pcbnew/dialogs/wizard_add_fplib.cpp | 19 +++++++++++--------
 3 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/common/env_paths.cpp b/common/env_paths.cpp
index 3569c436d7..9e6a6715ca 100644
--- a/common/env_paths.cpp
+++ b/common/env_paths.cpp
@@ -64,9 +64,8 @@ static bool normalizeAbsolutePaths( const wxFileName& aPathA,
     return true;
 }
 
-
 wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars,
-        const PROJECT* aProject )
+        const wxString& aProjectPath )
 {
     wxFileName envPath;
     wxString tmp, varName, normalizedFullPath;
@@ -90,9 +89,10 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars
         }
     }
 
-    if( varName.IsEmpty() && aProject )
+    if( varName.IsEmpty() && !aProjectPath.IsEmpty()
+        && wxFileName( aProjectPath ).IsAbsolute() && wxFileName( aFilePath ).IsAbsolute() )
     {
-        envPath.SetPath( aProject->GetProjectPath() );
+        envPath.SetPath( aProjectPath );
 
         if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
             varName = PROJECT_VAR_NAME;
@@ -111,6 +111,15 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars
     return normalizedFullPath;
 }
 
+wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars,
+        const PROJECT* aProject )
+{
+  if( aProject )
+    return NormalizePath( aFilePath, aEnvVars, aProject->GetProjectPath() );
+  else
+    return NormalizePath( aFilePath, aEnvVars, "" );
+}
+
 
 // Create file path by appending path and file name. This approach allows the filename
 // to contain a relative path, whereas wxFileName::SetPath() would replace the
diff --git a/include/env_paths.h b/include/env_paths.h
index bac92ffcac..a27fbcc7ca 100644
--- a/include/env_paths.h
+++ b/include/env_paths.h
@@ -39,6 +39,18 @@
 wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars,
         const PROJECT* aProject );
 
+/**
+ * Normalizes a file path to an environmental variable, if possible.
+ *
+ * @param aFilePath is the full file path (path and file name) to be normalized.
+ * @param aEnvVars is an optional map of environmental variables to try substition with.
+ * @param aProjectPath is an optional string to normalize the file path to the project path.
+ * @return Normalized full file path (path and file name) if succeeded or empty string if the
+ *          path could not be normalized.
+ */
+wxString NormalizePath(
+        const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars, const wxString& aProjectPath );
+
 /**
  * Searches the default paths trying to find one with the requested file.
  *
@@ -48,7 +60,7 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars
  * @return Full path (apth and file name) if the file was found in one of the paths, otherwise
  *      an empty string.
 */
-wxString ResolveFile( const wxString& aFileName, const ENV_VAR_MAP* aEnvVars,
-        const PROJECT* aProject );
+wxString ResolveFile(
+        const wxString& aFileName, const ENV_VAR_MAP* aEnvVars, const PROJECT* aProject );
 
 #endif /* ENV_PATHS_H */
diff --git a/pcbnew/dialogs/wizard_add_fplib.cpp b/pcbnew/dialogs/wizard_add_fplib.cpp
index 193253cb3a..b34822f0f9 100644
--- a/pcbnew/dialogs/wizard_add_fplib.cpp
+++ b/pcbnew/dialogs/wizard_add_fplib.cpp
@@ -46,6 +46,7 @@
 #include <bitmaps.h>
 
 #include <class_module.h>
+#include <env_paths.h>
 
 #ifdef BUILD_GITHUB_PLUGIN
 #include <../github/github_getliblist.h>
@@ -222,17 +223,14 @@ wxString WIZARD_FPLIB_TABLE::LIBRARY::GetRelativePath( const wxString& aBase, co
 
 wxString WIZARD_FPLIB_TABLE::LIBRARY::GetAutoPath( LIB_SCOPE aScope ) const
 {
-    const wxString& global_env = FP_LIB_TABLE::GlobalPathEnvVariableName();
     const wxString& project_env = PROJECT_VAR_NAME;
     const wxString& github_env( "KIGITHUB" );
 
     wxString rel_path;
 
-    // KISYSMOD check
-    rel_path = replaceEnv( global_env );
-
-    if( !rel_path.IsEmpty() )
-        return rel_path;
+    // The extra KIGITHUB and KIPRJCHECKS are still here since Pgm.GetLocalVariables() does not
+    // contain the KIPRJMOD env var, and the KIGITHUB does not pass the IsAbsolutePath check
+    // that happens in NormalizePath(...) since it starts with https://
 
     // KIGITHUB check
     rel_path = replaceEnv( github_env, false );
@@ -249,8 +247,13 @@ wxString WIZARD_FPLIB_TABLE::LIBRARY::GetAutoPath( LIB_SCOPE aScope ) const
             return rel_path;
     }
 
-    // Return the full path
-    return m_path;
+    rel_path = NormalizePath( wxFileName( m_path ), &Pgm().GetLocalEnvVariables(), project_env );
+
+    // If normalizePath failed, then rel_path will be empty, m_path is the full path.
+    if( rel_path.IsEmpty() )
+      return m_path;
+
+    return rel_path;
 }