#include "init_user_directory.hpp" #include "config.hpp" #include #include #include #include #include namespace fs = boost::filesystem; namespace pt = boost::property_tree; namespace dropshell { static std::string user_directory; bool get_user_directory(std::string& path) { path = user_directory; return !path.empty(); } void set_user_directory(const std::string& path) { user_directory = path; } void init_user_directory(const std::string& path) { // Convert to canonical path fs::path abs_path = fs::canonical(path); // The directory must exist if (!fs::exists(abs_path)) { throw std::runtime_error("The user directory does not exist: " + abs_path.string()); } // create the servers subdirectory if it doesn't exist fs::path servers_dir = abs_path / "servers"; if (!fs::exists(servers_dir)) { fs::create_directories(servers_dir); } // Update config file std::string config_path; if (!get_config_path(config_path)) { // No config file exists, create one in user's home directory const char* home = std::getenv("HOME"); if (!home) { throw std::runtime_error("HOME environment variable not set"); } fs::path config_dir = fs::path(home) / ".config" / "dropshell"; if (!fs::exists(config_dir)) { fs::create_directories(config_dir); } config_path = (config_dir / "dropshell.conf").string(); } try { pt::ptree tree; // Read existing config if it exists if (fs::exists(config_path)) { pt::read_ini(config_path, tree); } // Update user directory tree.put("user.directory", abs_path.string()); // Write back to config file pt::write_ini(config_path, tree); // Update in-memory value user_directory = abs_path.string(); std::cout << "User directory initialized to: " << abs_path.string() << std::endl; } catch (const std::exception& e) { throw std::runtime_error("Failed to update config: " + std::string(e.what())); } } } // namespace dropshell