2015-03-06 09:12:26 -05:00
|
|
|
#!/usr/bin/env bash
|
2015-03-06 07:55:58 -05:00
|
|
|
|
|
|
|
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? () {
|
2015-03-06 07:55:58 -05:00
|
|
|
local readonly directory=$1; shift
|
|
|
|
|
|
|
|
[[ -d $directory ]]
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:12:26 -05:00
|
|
|
integer? () {
|
2015-03-06 07:55:58 -05:00
|
|
|
local readonly number=$1; shift
|
|
|
|
|
|
|
|
[[ $number =~ ^[0-9]+$ ]]
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:12:26 -05:00
|
|
|
empty? () {
|
2015-03-06 07:55:58 -05:00
|
|
|
local var=$1; shift
|
|
|
|
|
|
|
|
[[ -z $var ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
check_arguments () {
|
2015-03-06 09:12:26 -05:00
|
|
|
if empty? "$DIRECTORY" || empty? "$WAIT_TIME"; then
|
2015-03-06 07:55:58 -05:00
|
|
|
error "missing operand"
|
|
|
|
fi
|
|
|
|
|
2015-03-06 09:12:26 -05:00
|
|
|
if ! directory? "$DIRECTORY"; then
|
2015-03-06 07:55:58 -05:00
|
|
|
error "$DIRECTORY is not a directory"
|
|
|
|
fi
|
|
|
|
|
2015-03-06 09:12:26 -05:00
|
|
|
if ! integer? "$WAIT_TIME"; then
|
2015-03-06 07:55:58 -05:00
|
|
|
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
|