"""
paths.py - Single source of truth for runtime path resolution.

The honeypot needs a "working directory" containing:
  etc/          config file (honeypot.cfg.base)
  log/          rotating log files (created on demand)
  data/         geolocation databases, SQLite db, etc.

Priority for locating the working directory:
  1. SIPHONEY_WORKDIR environment variable
  2. Current working directory

The bundled read-only default (`etc/honeypot.cfg.base`)
is installed inside the `siphoney` package and located via the package's
own __file__ attribute, which works on all Python versions without
requiring pkg_resources or importlib.resources.
"""

from __future__ import absolute_import


from os import getcwd, environ
from os.path import abspath, dirname, join


def get_workdir():
    """Return the absolute path to the runtime working directory."""
    env = environ.get('SIPHONEY_WORKDIR', '').strip()
    if env:
        return abspath(env)
    return getcwd()


def workdir_path(*parts):
    """Return an absolute path rooted at the working directory."""
    return join(get_workdir(), *parts)


def bundled(*parts):
    """
    Return the filesystem path to a file bundled inside the installed package.
    Arguments are path components relative to the siphoney/data/ directory,
    passed as separate strings (like os.path.join) to avoid hardcoded separators.

    Uses the package's own __file__ to locate the data directory, which works
    on all Python versions (2.7+) without requiring pkg_resources or
    importlib.resources.
    """
    # siphoney/data/ lives alongside this module's package (core/ is a sibling
    # of siphoney/), so we go up one level from core/ to find siphoney/data/.
    here = dirname(abspath(__file__))
    package_dir = join(dirname(here), 'siphoney')
    return join(package_dir, 'data', *parts)
