use std::path::Path;

use anyhow::Result;
use posix_acl::PosixACL;
use walkdir::WalkDir;

pub fn write_acl_recursive<P: AsRef<Path>>(path: P, acl: PosixACL) -> Result<()> {
    for entry in WalkDir::new(path).min_depth(0).follow_links(false) {
        let entry = entry?;
        let path = entry.path();
        println!("Writing ACL for: {:?} {:?}", path, acl);
        match entry.file_type().is_dir() {
            true => {
                acl.write(&path)?;
                acl.write_default(&path)?;
            }
            false => {
                acl.write(&path)?;
            }
        }
    }

    Ok(())
}
