#!/bin/bash

inotifywait -e attrib -m -r /var/lib/one/datastores |
while read path action file; do
    if [[ "$action" == "ATTRIB" ]]; then
        if [[ -f ${path}/${file} ]]; then
            if [[ $path != *".isofiles/" ]]; then
                USER=$(stat -c '%U' ${path}/${file})
                if [[ "$USER" == "root" ]]; then
                    sudo chown oneadmin:oneadmin ${path}/${file}
                else
                    sudo setfacl -m user:oneadmin:r ${path}/${file}
                fi
            fi
        fi
    fi
done
