#include <iostream>
#include <filesystem>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <unistd.h>
#include <sys/utsname.h>
#include "version.hpp"

void checkPath(const std::string& path, const std::string& description) {
    std::cout << "\n=== " << description << " ===" << std::endl;
    std::cout << "Path: " << path << std::endl;
    
    if (std::filesystem::exists(path)) {
        std::cout << "EXISTS: Yes" << std::endl;
        
        if (std::filesystem::is_directory(path)) {
            std::cout << "TYPE: Directory" << std::endl;
            std::cout << "Contents:" << std::endl;
            
            try {
                for (const auto& entry : std::filesystem::directory_iterator(path)) {
                    std::cout << "  " << entry.path().filename().string() << std::endl;
                }
            } catch (const std::exception& e) {
                std::cout << "  Error listing contents: " << e.what() << std::endl;
            }
        } else {
            std::cout << "TYPE: File" << std::endl;
        }
    } else {
        std::cout << "EXISTS: No" << std::endl;
    }
}

void checkFile(const std::string& path, const std::string& description) {
    std::cout << "\n=== " << description << " ===" << std::endl;
    std::cout << "File: " << path << std::endl;
    
    if (std::filesystem::exists(path)) {
        std::cout << "EXISTS: Yes" << std::endl;
        
        std::ifstream file(path);
        if (file.is_open()) {
            std::string line;
            std::cout << "Contents:" << std::endl;
            while (std::getline(file, line)) {
                std::cout << "  " << line << std::endl;
            }
        }
    } else {
        std::cout << "EXISTS: No" << std::endl;
    }
}

int main() {
    std::cout << "Alpine Linux Library Path Checker" << std::endl;
    std::cout << "Version: " << PROJECT_VERSION << std::endl;
    
    // Get system information
    struct utsname sys_info;
    if (uname(&sys_info) == 0) {
        std::cout << "\nSystem Information:" << std::endl;
        std::cout << "  System: " << sys_info.sysname << std::endl;
        std::cout << "  Machine: " << sys_info.machine << std::endl;
        std::cout << "  Architecture: " << sys_info.machine << std::endl;
    }
    
    // Check standard library directories
    checkPath("/lib", "System Libraries (/lib)");
    checkPath("/usr/lib", "User Libraries (/usr/lib)");
    checkPath("/usr/lib64", "64-bit Libraries (/usr/lib64)");
    checkPath("/usr/local/lib", "Local Libraries (/usr/local/lib)");
    
    // Check include directories
    checkPath("/usr/include", "System Headers (/usr/include)");
    checkPath("/usr/local/include", "Local Headers (/usr/local/include)");
    
    // Check architecture-specific paths
    checkPath("/usr/lib/x86_64-linux-gnu", "x86_64 GNU Libraries");
    checkPath("/usr/lib/aarch64-linux-gnu", "aarch64 GNU Libraries");
    checkPath("/lib64", "64-bit System Libraries");
    
    // Check musl-specific configuration files
    checkFile("/etc/ld-musl-x86_64.path", "musl x86_64 Library Path Config");
    checkFile("/etc/ld-musl-aarch64.path", "musl aarch64 Library Path Config");
    
    // Check for dynamic linker
    checkFile("/lib/ld-musl-x86_64.so.1", "musl x86_64 Dynamic Linker");
    checkFile("/lib/ld-musl-aarch64.so.1", "musl aarch64 Dynamic Linker");
    
    // Check environment variables
    std::cout << "\n=== Environment Variables ===" << std::endl;
    const char* ld_library_path = std::getenv("LD_LIBRARY_PATH");
    std::cout << "LD_LIBRARY_PATH: " << (ld_library_path ? ld_library_path : "(not set)") << std::endl;
    
    const char* pkg_config_path = std::getenv("PKG_CONFIG_PATH");
    std::cout << "PKG_CONFIG_PATH: " << (pkg_config_path ? pkg_config_path : "(not set)") << std::endl;
    
    return 0;
}