void SystemDirsAppend( SEARCH_STACK* aSearchStack )
{
    // No clearing is done here, the most general approach is NOT to assume that
    // our appends will be the only thing in the stack.  This function has no
    // knowledge of caller's intentions.

    // wxPathList::AddEnvList() is broken, use SEARCH_STACK::AddPaths().
    // SEARCH_STACK::AddPaths() will verify readability and existence of
    // each directory before adding.
    SEARCH_STACK maybe;

    // User environment variable path is the first search path.  Chances are
    // if the user is savvy enough to set an environment variable they know
    // what they are doing.  It should take precedence over anything else.
    // Otherwise don't set it.
    maybe.AddPaths( wxGetenv( wxT( "KICAD" ) ) );

#ifdef __WXMAC__
    // Add the directory for the user-dependent, program specific data files.
    maybe.AddPaths( GetOSXKicadUserDataDir() );

    // Global machine specific application data
    maybe.AddPaths( GetOSXKicadMachineDataDir() );

    // Global application specific data files inside bundle
    maybe.AddPaths( GetOSXKicadDataDir() );
#else
    // This is from CMAKE_INSTALL_PREFIX.
    // Useful when KiCad is installed by `make install`.
    // Use as second ranked place.
    maybe.AddPaths( wxT( DEFAULT_INSTALL_PATH ) );

    // Add the directory for the user-dependent, program specific data files.
    // According to wxWidgets documentation:
    // Unix: ~/.appname
    // Windows: C:\Documents and Settings\username\Application Data\appname
    maybe.AddPaths( wxStandardPaths::Get().GetUserDataDir() );

    {
        // Should be full path to this program executable.
        wxString   bin_dir = Pgm().GetExecutablePath();

#if defined(__MINGW32__)
        // bin_dir uses unix path separator.  So to parse with wxFileName
        // use windows separator, especially important for server inclusion:
        // like: \\myserver\local_path .
        bin_dir.Replace( wxFileName::GetPathSeparator( wxPATH_UNIX ),
                         wxFileName::GetPathSeparator( wxPATH_WIN ) );
#endif

        wxFileName bin_fn( bin_dir, wxEmptyString );

        // Dir of the global (not user-specific), application specific, data files.
        // From wx docs:
        // Unix: prefix/share/appname
        // Windows: the directory where the executable file is located
        // Mac: appname.app/Contents/SharedSupport bundle subdirectory
        wxString data_dir = wxStandardPaths::Get().GetDataDir();

        if( bin_fn.GetPath() != data_dir )
        {
            // add data_dir if it is different from the bin_dir
            maybe.AddPaths( data_dir );
        }

        // Up one level relative to binary path with "share" appended below.
        bin_fn.RemoveLastDir();
        maybe.AddPaths( bin_fn.GetPath() );
    }

    /* The normal OS program file install paths allow for a binary to be
     * installed in a different path from the library files.  This is
     * useful for development purposes so the library and documentation
     * files do not need to be installed separately.  If someone can
     * figure out a way to implement this without #ifdef, please do.
     */
#if defined(__MINGW32__)
    maybe.AddPaths( wxGetenv( wxT( "PROGRAMFILES" ) ) );
#else
    maybe.AddPaths( wxGetenv( wxT( "PATH" ) ) );
#endif
#endif

#if defined(DEBUG) && 0
    maybe.Show( "maybe wish list" );
#endif

    // Append 1) kicad, 2) kicad/share, 3) share, and 4) share/kicad to each
    // possible base path in 'maybe'. Since SEARCH_STACK::AddPaths() will verify
    // readability and existence of each directory, not all of these will be
    // actually appended.
    for( unsigned i = 0; i < maybe.GetCount();  ++i )
    {
        wxFileName fn( maybe[i], wxEmptyString );

#ifndef __WXMAC__
        if( fn.GetPath().AfterLast( fn.GetPathSeparator() ) == wxT( "bin" ) )
        {
            fn.RemoveLastDir();

            if( !fn.GetDirCount() )
                continue;               // at least on linux
        }
#endif

        aSearchStack->AddPaths( fn.GetPath() );

#ifndef __WXMAC__
        fn.AppendDir( wxT( "kicad" ) );
        aSearchStack->AddPaths( fn.GetPath() );     // add maybe[i]/kicad

        fn.AppendDir( wxT( "share" ) );
        aSearchStack->AddPaths( fn.GetPath() );     // add maybe[i]/kicad/share

        fn.RemoveLastDir();                         // ../  clear share
        fn.RemoveLastDir();                         // ../  clear kicad

        fn.AppendDir( wxT( "share" ) );
        aSearchStack->AddPaths( fn.GetPath() );     // add maybe[i]/share

        fn.AppendDir( wxT( "kicad" ) );
        aSearchStack->AddPaths( fn.GetPath() );     // add maybe[i]/share/kicad
#endif
    }

#if defined(DEBUG) && 0
    // final results:
    aSearchStack->Show( __func__ );
#endif
}
// See also FindKicadHelpPath.cpp.notused.
wxString SearchHelpFileFullPath( const SEARCH_STACK& aSStack, const wxString& aBaseName )
{
    wxArrayString   subdirs;
    wxArrayString   altsubdirs;
    SEARCH_STACK    ss = aSStack;

    // It might already be in aSStack, but why depend on other code
    // far away when it's so easy to add it again (to our copy) as the first place to look.

    // This is CMAKE_INSTALL_PREFIX unless DEFAULT_INSTALL_PATH was defined during
    // build configuration:
    ss.AddPaths( wxT( DEFAULT_INSTALL_PATH ), 0 );

#if defined(__WXMAC__)
    ss.AddPaths( GetOSXKicadMachineDataDir() );
    ss.AddPaths( Pgm().GetExecutablePath(), 0 );

    // OS X packages can have the help files in
    // /Library/Application\ Support/kicad/help,
    // and in Contents/SharedSupport/help inside the
    // bundle.
    // Below we account for an international subdirectory.
    subdirs.Add( wxT( "help" ) );
    altsubdirs.Add( wxT( "Contents" ) );
    altsubdirs.Add( wxT( "SharedSupport" ) );
    altsubdirs.Add( wxT( "help" ) );
#endif

#if ! defined(__WXMAC__) // && defined(__linux__)
    // This is the executable path minus the trailing bin directory used on Windows and Linux.
    wxFileName tmp( Pgm().GetExecutablePath(), wxEmptyString );
    wxArrayString binDirs = tmp.GetDirs();

    if( !binDirs.IsEmpty() && binDirs[ binDirs.GetCount() - 1 ].CmpNoCase( wxT( "bin" ) ) == 0 )
        tmp.RemoveLastDir();

    ss.AddPaths( tmp.GetPath(), 0 );

    // Based on kicad-doc.bzr/CMakeLists.txt, line 20, the help files are
    // installed into "<CMAKE_INSTALL_PREFIX>/share/doc/kicad/help" for linux.
    // This is ${KICAD_HELP} var in that CMakeLists.txt file.
    // Below we account for an international subdirectory.
    subdirs.Add( wxT( "share" ) );
    subdirs.Add( wxT( "doc" ) );
    subdirs.Add( wxT( "kicad" ) );
    subdirs.Add( wxT( "help" ) );

    // Based on kicad-doc.bzr/CMakeLists.txt, line 35, the help files are
    // installed into "<CMAKE_INSTALL_PREFIX>/doc/help" for Windows.
    // This is ${KICAD_HELP} var in that CMakeLists.txt file.
    // Below we account for an international subdirectory.
    altsubdirs.Add( wxT( "doc" ) );
    altsubdirs.Add( wxT( "help" ) );
#endif

    // If there's a KICAD environment variable set, always use that guy's path first.
    if( !Pgm().GetKicadEnvVariable().IsEmpty() )
        ss.AddPaths( Pgm().GetKicadEnvVariable(), 0 );

    /* Search for a help file.
     *  we *must* find a help file.
     *  so help is searched in directories in this order:
     *  help/<canonical name> like help/en_GB
     *  help/<short name> like help/en
     *  help/en
     */

    wxLocale* i18n = Pgm().GetLocale();

    // We try to find help file in help/<canonical name>
    // If fails, try to find help file in help/<short canonical name>
    // If fails, try to find help file in help/en
    wxArrayString locale_name_dirs;
    locale_name_dirs.Add( i18n->GetCanonicalName() );           // canonical name like fr_FR

    // wxLocale::GetName() does not return always the short name
    locale_name_dirs.Add( i18n->GetName().BeforeLast( '_' ) );  // short canonical name like fr
    locale_name_dirs.Add( wxT( "en" ) );                        // default (en)

#if defined(DEBUG) && 1
    ss.Show( wxString( __func__ ) );
    wxLogDebug( wxT( "%s: m_help_file:'%s'" ), __func__, GetChars( aBaseName ) );
#endif

    wxLogDebug( wxT( "Checking SEARCH_STACK for file %s" ), GetChars( aBaseName ) );

    // Help files can be html (.html ext) or pdf (.pdf ext) files.
    // Therefore, <BaseName>.html file is searched and if not found,
    // <BaseName>.pdf file is searched in the same paths
    wxString fn;

    for( unsigned ii = 0; ii < locale_name_dirs.GetCount(); ii++ )
    {
        subdirs.Add( locale_name_dirs[ii] );
        altsubdirs.Add( locale_name_dirs[ii] );

        fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &altsubdirs );

        if( !fn.IsEmpty() )
            break;

        fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &altsubdirs );

        if( !fn.IsEmpty() )
            break;

        fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &subdirs );

        if( !fn.IsEmpty() )
            break;

        fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &subdirs );

        if( !fn.IsEmpty() )
            break;

        subdirs.RemoveAt( subdirs.GetCount() - 1 );
        altsubdirs.RemoveAt( altsubdirs.GetCount() - 1 );
    }

    return fn;
}
void S3D_PLUGIN_MANAGER::loadPlugins( void )
{
    std::list< wxString > searchpaths;
    std::list< wxString > pluginlist;
    wxFileName fn;

#ifndef __WXMAC__

    #ifdef DEBUG
    // set up to work from the build directory
    fn.Assign( wxStandardPaths::Get().GetExecutablePath() );
    fn.AppendDir( wxT("..") );
    fn.AppendDir( wxT("plugins") );
    fn.AppendDir( wxT("3d") );

    std::string testpath = std::string( fn.GetPathWithSep().ToUTF8() );
    checkPluginPath( testpath, searchpaths );
    #endif

    #ifndef _WIN32  // suppress 'kicad' subdir since it is redundant on MSWin
    fn.Assign( wxStandardPaths::Get().GetPluginsDir(), "" );
    fn.RemoveLastDir();
    fn.AppendDir( wxT( "kicad" ) );
    #else
        fn.Assign( wxStandardPaths::Get().GetExecutablePath() );
    #endif

    fn.AppendDir( wxT( "plugins" ) );
    fn.AppendDir( wxT( "3d" ) );
    checkPluginPath( std::string( fn.GetPathWithSep().ToUTF8() ), searchpaths );

    checkPluginPath( wxT( "/usr/lib/kicad/plugins/3d" ), searchpaths );
    checkPluginPath( wxT( "/usr/local/lib/kicad/plugins/3d" ), searchpaths );
    checkPluginPath( wxT( "/opt/kicad/lib/kicad/plugins/3d" ), searchpaths );

    // note: GetUserDataDir() gives '.pcbnew' rather than '.kicad' since it uses the exe name;
    fn.Assign( wxStandardPaths::Get().GetUserDataDir(), "" );
    fn.RemoveLastDir();
    #ifdef _WIN32
    fn.AppendDir( wxT( "kicad" ) );
    #else
    fn.AppendDir( wxT( ".kicad" ) );
    #endif
    fn.AppendDir( wxT( "plugins" ) );
    fn.AppendDir( wxT( "3d" ) );
    checkPluginPath( fn.GetPathWithSep(), searchpaths );

#else

   // Search path on OS X is
   // (1) User     ~/Library/Application Support/kicad/PlugIns/3d
   checkPluginPath( GetOSXKicadUserDataDir() + wxT( "/PlugIns/3d" ), searchpaths );
   // (2) Machine  /Library/Application Support/kicad/PlugIns/3d
   checkPluginPath( GetOSXKicadMachineDataDir() + wxT( "/PlugIns/3d" ), searchpaths );
   // (3) Bundle   kicad.app/Contents/PlugIns/3d
   fn.Assign( Pgm().GetExecutablePath() );
   fn.AppendDir( wxT( "Contents" ) );
   fn.AppendDir( wxT( "PlugIns" ) );
   fn.AppendDir( wxT( "3d" ) );
   checkPluginPath( fn.GetPathWithSep(), searchpaths );

#endif

    std::list< wxString >::iterator sPL = searchpaths.begin();
    std::list< wxString >::iterator ePL = searchpaths.end();

    while( sPL != ePL )
    {
#ifdef DEBUG
        do {
            std::ostringstream ostr;
            ostr << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << ":\n";
            ostr << " * [DEBUG] searching path: '" << (*sPL).ToUTF8() << "'";
            wxLogTrace( MASK_3D_PLUGINMGR, "%s\n", ostr.str().c_str() );
        } while( 0 );
#endif
        listPlugins( *sPL, pluginlist );
        ++sPL;
    }

    if( pluginlist.empty() )
        return;

    sPL = pluginlist.begin();
    ePL = pluginlist.end();

    while( sPL != ePL )
    {
        KICAD_PLUGIN_LDR_3D* pp = new KICAD_PLUGIN_LDR_3D;

        if( pp->Open( sPL->ToUTF8() ) )
        {
#ifdef DEBUG
            do {
                std::ostringstream ostr;
                ostr << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << ":\n";
                ostr << "* [DEBUG] adding plugin";
                wxLogTrace( MASK_3D_PLUGINMGR, "%s\n", ostr.str().c_str() );
            } while( 0 );
#endif
            m_Plugins.push_back( pp );
            int nf = pp->GetNFilters();

            #ifdef DEBUG
            do {
                std::ostringstream ostr;
                ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
                ostr << " * [INFO] adding " << nf << " filters";
                wxLogTrace( MASK_3D_PLUGINMGR, "%s\n", ostr.str().c_str() );
            } while( 0 );
            #endif

            for( int i = 0; i < nf; ++i )
            {
                char const* cp = pp->GetFileFilter( i );

                if( cp )
                    addFilterString( wxString::FromUTF8Unchecked( cp ) );
            }

            addExtensionMap( pp );

            // close the loaded library
            pp->Close();
        }
        else
        {
#ifdef DEBUG
            do {
                std::ostringstream ostr;
                ostr << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << ":\n";
                ostr << "* [DEBUG] deleting plugin";
                wxLogTrace( MASK_3D_PLUGINMGR, "%s\n", ostr.str().c_str() );
            } while( 0 );
#endif
            delete pp;
        }

        ++sPL;
    }

#ifdef DEBUG
    do {
        std::ostringstream ostr;
        ostr << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << ":\n";
        ostr << "* [DEBUG] plugins loaded";
        wxLogTrace( MASK_3D_PLUGINMGR, "%s\n", ostr.str().c_str() );
    } while( 0 );
#endif

    return;
}