1
0
Fork 0
misc-linux-tools/bin/watchd

100 lines
1.6 KiB
Text
Raw Normal View History

2015-03-06 09:12:26 -05:00
#!/usr/bin/env bash
readonly PROGRAM_NAME=$(basename "$0")
readonly PROGRAM_LOC=$(readlink -m "$(dirname $0)")
readonly ARGS="$@"
readonly ARG_COUNT="$#"
LAST_SHA=0
usage () {
echo "usage: $PROGRAM_NAME [DIRECTORY] [WAIT_TIME]"
}
error () {
local readonly message=$1; shift
echo "$PROGRAM_NAME: $message"
usage
exit 1
}
current_sha () {
ls -lR "$DIRECTORY" \
| sha1sum
}
sha_changed? () {
[[ "$(current_sha)" != "$LAST_SHA" ]]
}
update_sha () {
LAST_SHA=$(current_sha)
}
alert_update () {
now=$(date +"%Y-%m-%d %I:%M:%S")
echo "[$now] Directory Updated"
}
2015-03-06 09:12:26 -05:00
directory? () {
local readonly directory=$1; shift
[[ -d $directory ]]
}
2015-03-06 09:12:26 -05:00
integer? () {
local readonly number=$1; shift
[[ $number =~ ^[0-9]+$ ]]
}
2015-03-06 09:12:26 -05:00
empty? () {
local var=$1; shift
[[ -z $var ]]
}
check_arguments () {
2015-03-06 09:12:26 -05:00
if empty? "$DIRECTORY" || empty? "$WAIT_TIME"; then
error "missing operand"
fi
2015-03-06 09:12:26 -05:00
if ! directory? "$DIRECTORY"; then
error "$DIRECTORY is not a directory"
fi
2015-03-06 09:12:26 -05:00
if ! integer? "$WAIT_TIME"; then
error "$WAIT_TIME is not an integer"
fi
}
run () {
while true; do
if sha_changed?; then
update_sha
alert_update
fi
sleep "$WAIT_TIME"
done
}
main () {
local readonly d=$1; shift
local readonly w=$1; shift
readonly DIRECTORY=${d:-$(pwd)}
readonly WAIT_TIME=${w:-15}
check_arguments
update_sha
echo "--------------------------------------------------------------------------------"
echo "+ Monitoring $DIRECTORY at interval $WAIT_TIME seconds"
echo "--------------------------------------------------------------------------------"
run
}
main $ARGS