diff options
author | Peter Ludikovsky <peter@ludikovsky.name> | 2016-07-28 14:54:16 +0200 |
---|---|---|
committer | Peter Ludikovsky <peter@ludikovsky.name> | 2016-07-28 14:54:16 +0200 |
commit | 73e35d529ed91ff538a32e5317197a407392404e (patch) | |
tree | 93a763f7ab39f9c9d76af0f95f079b598161b441 |
Initial commit
-rwxr-xr-x | junker.sh | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/junker.sh b/junker.sh new file mode 100755 index 0000000..28fdf1a --- /dev/null +++ b/junker.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +[ ! -z "${DEBUG}" ] && set -x + +BASE=$1 +INOTIFY='/usr/bin/inotifywait' +INOTIFY_EVENTS='close_write,moved_to' +INOTIFY_OPTIONS='--monitor --recursive --event'" ${INOTIFY_EVENTS}" +SALEARN=/usr/bin/sa-learn +SAOPTS=--no-sync +LOGS=$2 + +function OpenLog { + exec 3>>"${LOGS}/junker.log" + exec 1>&3 + exec 2>&3 + echo -e "$( date +'%Y%m%d %H:%M:%S' )\tLog opened" +} +function CloseLog { + echo -e "$( date +'%Y%m%d %H:%M:%S' )\tLog closed" + exec 3>&- +} + +function Cleanup { + RunAndLog echo ${SALEARN} --sync + CloseLog + exit 0 +} + +function RunAndLog { + echo -e "$( date +'%Y%m%d %H:%M:%S' )\t$@" + $@ +} + +function Reload { + CloseLog + OpenLog +} + +OpenLog + +trap "Cleanup" EXIT SIGINT SIGTERM +trap "Reload" SIGHUP + +while IFS=' ' read -r dir event file +do + if [[ "${event}" == *MOVED_TO* && "${dir}" == */Junk/ ]] + then + RunAndLog echo ${SALEARN} ${SAOPTS} --forget "${dir}/${file}" + RunAndLog echo ${SALEARN} ${SAOPTS} --spam "${dir}/${file}" + elif [[ "${event}" == *MOVED_TO* && "${dir}" == */!Junk/ ]] + then + RunAndLog echo ${SALEARN} ${SAOPTS} --forget "${dir}/${file}" + RunAndLog echo ${SALEARN} ${SAOPTS} --ham "${dir}/${file}" + elif [[ "${event}" == *CLOSE_WRITE* && "${dir}" == */Junk/ ]] + then + RunAndLog echo ${SALEARN} ${SAOPTS} --spam "${dir}/${file}" + else + RunAndLog echo ${SALEARN} ${SAOPTS} --ham "${dir}/${file}" + fi +done < <( ${INOTIFY} ${INOTIFY_OPTIONS} --format '%w %e %f' ${BASE} ) |