use std::env;
use std::fs;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;

pub fn read_env_var() {
    let filename = "device.env";
    let mut path = env::var("HOME").unwrap().to_string();
    path.push_str("/Scripts/");
    path.push_str(filename);
    println!("{}", path);

    fs::read_to_string(path.clone())
        .expect("Something went wrong reading the file : device.env");

    if let Ok(lines) = read_lines(path) {
        for line in lines {
            if let Ok(var) = line {
                let new_var: Vec<&str> = var.split('=').collect();
                env::set_var(new_var[0], new_var[1]);
            }
        }
    }

    // println!("\n");
    // for (key, value) in env::vars() {
    //     println!("{}: {}", key, value);
    // }
    // println!("\n");
}

fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
    let file = File::open(filename)?;
    Ok(io::BufReader::new(file).lines())
}
