#!/bin/bash

#======================================================================
# /etc/sysctl.conf
#----------------------------------------------------------------------
# fs.inotify.max_user_watches=524288
#======================================================================

#======================================================================
# crontab
#----------------------------------------------------------------------
# @reboot /home/auser/www/html/phpkobo/tools/mdfsys/mdf.sh
#======================================================================

#======================================================================
# MDIR: Directory to monitor
#----------------------------------------------------------------------
MDIR="/home/auser/www/html/";
path_exclude_list="/home/auser/www/html/phpkobo/tools/mdfsys/config/exclude_list.txt";
path_php="/var/www/html/phpkobo/tools/mdfsys/mdf.php";
#======================================================================

echo "monitoring" $MDIR

#
# inotifywait -q -m -r -e modify,delete,create $MDIR
#
# inotifywait's output format is 'DIRECTORY EVENT FILE'
#
# (e.g)
# test/hello/ MODIFY test.txt
# test/hello/ CREATE,ISDIR jjj
# test/hello/jjj/ CREATE test2.txt
# test/hello/jjj/ MODIFY test2.txt
#
inotifywait -m -r -e modify --fromfile "$path_exclude_list" $MDIR | while read DIRECTORY EVENT FILE; do
	inode=$(stat -c %i "${DIRECTORY}${FILE}");
	ret=$(php "$path_php" "$DIRECTORY" "$EVENT" "$FILE" "$inode");
done

read -p "Press any key to resume ..."
