use std::env;
use std::path::Path;
use std::{thread, time};
use sysinfo::{ProcessExt, System, SystemExt};

fn main() {
    let second_argument = match env::args().nth(1) {
        Some(x) => x,
        None => panic!("You have to specify a path.")
    };
    let path = Path::new(&second_argument);
    println!("Desired Path: {:?}", path);
    println!("Desired Directory: {:?}", path.parent());
    println!("Desired File: {:?}", path.file_name());
    let file_name = match path.file_name() {
        Some(x) => x,
        None => panic!("No valid filename")
    };
    let parent_path = match path.parent() {
        Some(x) => x,
        None => panic!("No valid path")
    };
    let five_seconds = time::Duration::from_secs(1);
    loop {
        let mut has_wait = false;
        let s = System::new_all();
        for process in s.get_process_by_name(file_name.to_str().unwrap()) {
            println!("{} {} {:?}", process.pid(), process.name(), process.root());
            if process.root() == parent_path {
                thread::sleep(five_seconds);
                has_wait = true;
                break;
            }
        }
        if !has_wait {
            break;
        };
    }
}
