use crate::exec;
use std::path::Path;
use std::env;
use std::str;
pub static CONFIG_FILE: &str = "/opt/zakuro/etc/profile.d/zk0.sh";
pub static CONFIG_DIR: &str = "/opt/zakuro/etc/profile.d";
use std::collections::HashSet;
pub fn eval(s1: &str) -> String {
evaluate_expr(s1)
}
use crate::common::print_debug;
pub fn evaluate_expr(s1: &str) -> String {
let mut output: String = String::from("");
if !s1.starts_with("'") {
for pathx in s1.split(':') {
let splits: Vec<&str> = pathx.split("/").collect();
let condition: bool = splits.len() > 1;
for varpath in splits {
if let Some('$') = varpath.chars().next() {
let _varpath = &varpath[1..];
if let Ok(s) = env::var(_varpath) {
env::set_var(_varpath, &s);
output.push_str(&s.to_string());
} else {
output.push_str((&varpath).as_ref());
}
} else if condition {
output.push_str(&format!("/{}", varpath));
} else {
output.push_str((&varpath).as_ref());
}
}
output.push(':');
}
if output.ends_with(':') {
output = output[..output.len() - 1].to_string();
}
output = output.replace("//", "/");
let string_list: Vec<&str> = output.split(":").collect();
let unique_values: HashSet<_> = string_list.into_iter().collect();
let unique_list: Vec<_> = unique_values.into_iter().collect();
output = unique_list.join(":");
} else {
output = String::from(s1);
}
output
}
pub fn update() {
if !Path::new(CONFIG_FILE).exists() {
for command in [
&format!("sudo mkdir -p {}", CONFIG_DIR),
&format!("sudo chown -R $USER {}", CONFIG_DIR),
&format!("wget -q 'http://get.zakuro-ai.com/env' -O {}", CONFIG_FILE),
] {
exec::tty(command);
}
}
let script_content = match std::fs::read_to_string(CONFIG_FILE) {
Ok(content) => content,
Err(e) => {
eprintln!("Error reading script: {}", e);
return;
}
};
for line in script_content.lines() {
if let Some((key, _value)) = line.split_once('=') {
if env::var(key).is_err() {
let value = evaluate_expr(_value);
print_debug(&format!("{}={}", key, value));
env::set_var(key, value);
}
}
}
}