////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1996-2025 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <cstdlib>

#include <string>

#include "kpse.h"
#include "lo-error.h"
#include "lo-utils.h"
#include "oct-env.h"
#include "pathsearch.h"

OCTAVE_BEGIN_NAMESPACE(octave)

directory_path::directory_path (const std::string& s)
  : m_orig_path (s), m_initialized (false), m_expanded_path (),
    m_path_elements ()
{
  if (! m_orig_path.empty ())
    init ();
}

std::list<std::string>
directory_path::elements ()
{
  return m_initialized ? m_path_elements : std::list<std::string> ();
}

std::list<std::string>
directory_path::all_directories ()
{
  std::list<std::string> retval;

  if (m_initialized)
    {
      for (const auto& elt : m_path_elements)
        {
          std::string elt_dir = kpse_element_dir (elt);

          if (! elt_dir.empty ())
            retval.push_back (elt_dir);
        }
    }

  return retval;
}

std::string
directory_path::find_first (const std::string& nm)
{
  return m_initialized ? kpse_path_search (m_expanded_path, nm) : "";
}

std::list<std::string>
directory_path::find_all (const std::string& nm)
{
  return (m_initialized
          ? kpse_all_path_search (m_expanded_path, nm)
          : std::list<std::string> ());
}

std::string
directory_path::find_first_of (const std::list<std::string>& names)
{
  return (m_initialized
          ? kpse_path_find_first_of (m_expanded_path, names) : "");
}

std::list<std::string>
directory_path::find_all_first_of (const std::list<std::string>& names)
{
  return (m_initialized
          ? kpse_all_path_find_first_of (m_expanded_path, names)
          : std::list<std::string> ());
}

void
directory_path::init ()
{
  static bool octave_kpse_initialized = false;

  if (! octave_kpse_initialized)
    {
      std::string env_val = sys::env::getenv ("KPATHSEA_DEBUG");

      if (! env_val.empty ())
        {
          unsigned int env_debug_flags = 0;

          try
            {
              unsigned long val = std::stoul (env_val);

              if (val > std::numeric_limits<unsigned int>::max ())
                (*current_liboctave_warning_with_id_handler)
                  ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring out of range KPATHSEA_DEBUG value '%s'", env_val.c_str ());
              else
                env_debug_flags = val;
            }
          catch (const std::invalid_argument&)
            {
              (*current_liboctave_warning_with_id_handler)
                ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring invalid KPATHSEA_DEBUG value '%s'", env_val.c_str ());
            }
          catch (const std::out_of_range&)
            {
              (*current_liboctave_warning_with_id_handler)
                ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring out of range KPATHSEA_DEBUG value '%s'", env_val.c_str ());
            }

          kpse_debug |= env_debug_flags;
        }

      octave_kpse_initialized = true;
    }

  m_expanded_path = kpse_path_expand (m_orig_path);

  for (kpse_path_iterator pi (m_expanded_path); pi != std::string::npos; pi++)
    m_path_elements.push_back (*pi);

  m_initialized = true;
}

char
directory_path::path_sep_char ()
{
  return SEPCHAR;
}

std::string
directory_path::path_sep_str ()
{
  return SEPCHAR_STR;
}

OCTAVE_END_NAMESPACE(octave)
