# SPDX-License-Identifier: LSL-1.0
# Copyright (c) 2026 Markus Maiwald
# Stewardship: Self Sovereign Society Foundation
#
# This file is part of the Nexus Sovereign Core.
# See legal/LICENSE_SOVEREIGN.md for license terms.

## platform.nim
## Platform detection and BSD compatibility

import std/[os, strutils, osproc]

type
  Platform* = enum
    Linux, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, Darwin, Unknown

proc detectPlatform*(): Platform =
  ## Detect the current platform
  when defined(linux):
    return Linux
  elif defined(freebsd):
    return FreeBSD
  elif defined(netbsd):
    return NetBSD
  elif defined(openbsd):
    return OpenBSD
  elif defined(dragonfly):
    return DragonflyBSD
  elif defined(macosx):
    return Darwin
  else:
    return Unknown

proc isBSD*(): bool =
  ## Check if running on a BSD system
  let platform = detectPlatform()
  platform in [FreeBSD, NetBSD, OpenBSD, DragonflyBSD]

proc platformName*(): string =
  ## Get platform name as string
  case detectPlatform()
  of Linux: "Linux"
  of FreeBSD: "FreeBSD"
  of NetBSD: "NetBSD"
  of OpenBSD: "OpenBSD"
  of DragonflyBSD: "DragonflyBSD"
  of Darwin: "macOS"
  of Unknown: "Unknown"

proc getDefaultPaths*(): tuple[programs: string, links: string, cache: string, db: string] =
  ## Get platform-appropriate default paths
  if isBSD():
    # BSD typically uses /usr/local
    result.programs = "/usr/local/Programs"
    result.links = "/usr/local/System/Links"
    result.cache = "/var/cache/nip"
    result.db = "/var/db/nip/packages.json"
  else:
    # Linux and others
    result.programs = "/Programs"
    result.links = "/System/Links"
    result.cache = "/var/nip/cache"
    result.db = "/var/nip/db/packages.json"

proc getDefaultAdapter*(): string =
  ## Get the default/preferred adapter for this platform
  case detectPlatform()
  of FreeBSD, NetBSD, OpenBSD, DragonflyBSD:
    return "pkgsrc"  # PKGSRC is native on BSD
  of Linux:
    # Try to detect Linux distro
    if fileExists("/etc/arch-release"):
      return "pacman"
    else:
      return "nix"  # Nix works everywhere
  of Darwin:
    return "nix"  # Nix is popular on macOS
  of Unknown:
    return "nix"

proc getPkgConfigPath*(): string =
  ## Get platform-appropriate pkg-config path
  if isBSD():
    return "/usr/local/libdata/pkgconfig"
  else:
    return "/usr/lib/pkgconfig"

proc getShellConfigPath*(shell: string): string =
  ## Get shell configuration file path
  let home = getHomeDir()
  case shell.toLower()
  of "bash":
    if isBSD():
      return home / ".bash_profile"
    else:
      return home / ".bashrc"
  of "zsh":
    return home / ".zshrc"
  of "fish":
    return home / ".config/fish/config.fish"
  of "sh":
    return home / ".profile"
  else:
    return home / ".profile"

proc showPlatformInfo*() =
  ## Display platform information
  echo "Platform Information:"
  echo "  OS: ", platformName()
  echo "  BSD: ", if isBSD(): "Yes" else: "No"
  echo "  Default Adapter: ", getDefaultAdapter()
  echo ""

  let paths = getDefaultPaths()
  echo "Default Paths:"
  echo "  Programs: ", paths.programs
  echo "  Links: ", paths.links
  echo "  Cache: ", paths.cache
  echo "  Database: ", paths.db
