File size: 3,164 Bytes
1269259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPF Smart Gateway - Path Resolution
// Copyright 2026 Joseph Stone - All Rights Reserved
//
// Single source of truth for all SPF path resolution.
// Uses SPF_ROOT env var (preferred) or walk-up discovery from binary location.
// Cached via OnceLock for zero-overhead repeated access.
//
// SECURITY NOTE: Write allowlist paths are computed here but ENFORCED
// in validate.rs. The allowlist remains compiled Rust, not configurable.
//
// CLONE NOTE: SPF_ROOT env var is checked FIRST so cloned instances
// resolve to their own root, not the primary's. Walk-up from binary
// location is the fallback for single-instance setups.

use std::path::{Path, PathBuf};
use std::sync::OnceLock;

static SPF_ROOT_CACHE: OnceLock<PathBuf> = OnceLock::new();
static ACTUAL_HOME_CACHE: OnceLock<PathBuf> = OnceLock::new();

/// Find SPFsmartGATE root — supports cloned instances.
///
/// Resolution order:
///   1. SPF_ROOT environment variable (clone-safe, explicit)
///   2. Walk up from binary location looking for Cargo.toml
///   3. HOME env + /SPFsmartGATE
///   4. Panic (unrecoverable — cannot operate without known root)
pub fn spf_root() -> &'static Path {
    SPF_ROOT_CACHE.get_or_init(|| {
        // PRIMARY: SPF_ROOT env var — explicit, clone-safe
        if let Ok(root) = std::env::var("SPF_ROOT") {
            let p = PathBuf::from(&root);
            if p.exists() {
                return p;
            }
        }

        // FALLBACK: walk up from binary location
        if let Ok(exe) = std::env::current_exe() {
            if let Ok(canonical) = exe.canonicalize() {
                let mut dir = canonical.parent();
                while let Some(d) = dir {
                    if d.join("Cargo.toml").exists() {
                        return d.to_path_buf();
                    }
                    dir = d.parent();
                }
            }
        }

        // Last resort: HOME/SPFsmartGATE
        if let Ok(home) = std::env::var("HOME") {
            return PathBuf::from(home).join("SPFsmartGATE");
        }

        panic!("Cannot determine SPFsmartGATE root: SPF_ROOT not set, binary walk-up failed, HOME not set");
    })
}

/// Actual user home directory — parent of SPFsmartGATE root.
///
/// Resolution order:
///   1. Parent directory of spf_root()
///   2. HOME environment variable
///   3. Panic
pub fn actual_home() -> &'static Path {
    ACTUAL_HOME_CACHE.get_or_init(|| {
        if let Some(parent) = spf_root().parent() {
            return parent.to_path_buf();
        }
        if let Ok(home) = std::env::var("HOME") {
            return PathBuf::from(home);
        }
        panic!("Cannot determine home directory: spf_root has no parent and HOME not set");
    })
}

/// System package manager path — platform-detected at compile time.
/// Android/Termux: PREFIX env or /data/data/com.termux/files/usr
/// Linux/macOS: /usr
pub fn system_pkg_path() -> String {
    if cfg!(target_os = "android") {
        if let Ok(prefix) = std::env::var("PREFIX") {
            return prefix;
        }
        "/data/data/com.termux/files/usr".to_string()
    } else {
        "/usr".to_string()
    }
}