Skip to content

Instantly share code, notes, and snippets.

@demanuel
Last active January 20, 2020 22:18
Show Gist options
  • Save demanuel/ebcc0bd34c13b05fe1b422169a4a593c to your computer and use it in GitHub Desktop.
Save demanuel/ebcc0bd34c13b05fe1b422169a4a593c to your computer and use it in GitHub Desktop.
#!/bin/bash
# This file is part of NewsUP.
# NewsUP is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# NewsUP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with NewsUP. If not, see https://www.gnu.org/licenses.
declare -A HISTORY
declare MONITOR_FOLDER
declare PID_FILE="/tmp/newsup_monitor.pid"
declare LOG_FILE="/tmp/newsup_log.log"
declare IS_OLDER=0
declare IS_RUNNING=0
function read_cmd_opts {
local OPTIND
while getopts "d:hs" opt
do
case "${opt}" in
d)
if [[ ! -d $OPTARG ]]
then
echo "The monitoring folder $OPTARG doesn't exist or it isn't a folder!"
exit 1
fi
MONITOR_FOLDER=$OPTARG
;;
h)
show_help
exit 1
;;
s)
if [[ ${IS_RUNNING} -eq 1 ]]
then
stop_newsup_monitor
else
echo "NewsUP monitor isn't running!"
exit 1
fi
exit 0
;;
*)
show_help
exit 1
;;
esac
done
}
function show_help {
cat <<EOF
NewsUP Monitor - Monitor your folder and uploads them to the usenet
Usage: $(basename $0) [-h] [-s] -d <folder> -- program to monitor a folder for new files/folder and launch newsup uploader.
Options:
-h shows this help
-d defines what <folder> to monitor
-s stop currently running monitor. In case of an upload being done, it will wait until completion
This program is licensed under the GPLv3 license and comes with ABSOLUTELY NO WARRANTY;
EOF
}
function check_for_uploader {
which newsup >/dev/null 2>&1
if [[ $? -ne 0 ]]
then
echo "NewsUP is missing! Please install it" > log
exit 1
fi
}
function wait_for_newsup {
echo -n "Waiting for the current upload to finish"
pgrep -f "perl.*newsup" &>/dev/null
is_running=$?
while [[ ${is_running} -eq 0 ]]
do
echo -n "."
sleep 10
pgrep -f "perl.*newsup" &>/dev/null
is_running=$?
done
echo "."
}
function stop_newsup_monitor {
pid=`cat ${PID_FILE}`
kill -9 $pid
wait_for_newsup
echo "Exiting!"
rm -f ${PID_FILE}
}
function is_old {
folder=$1
age_limit=120
folder_time=$(stat $folder -c %Y)
cur_time=$(date +%s)
time_diff=$(expr ${cur_time} - ${folder_time})
if [[ ${time_diff} -gt ${age_limit} ]]
then
IS_OLDER=1
else
IS_OLDER=0
fi
}
function monitor_and_upload {
trap "" HUP
while true
do
for directory in "${MONITOR_FOLDER}"/*
do
directory="${directory//\/\//\/}"
if [[ -e $directory && ! ${HISTORY["$directory"]+_} ]]
then
is_old "$directory"
if [[ ${IS_OLDER} -eq 1 ]]
then
echo "Found file $directory"
wait_for_newsup
newsup -f "$directory"
HISTORY["$directory"]="uploaded"
IS_OLDER=0
fi
fi
done
# clean up the history - To avoid growing infinitely and to upload again in case i deleted from the FS and added again
for directory in "${!HISTORY[@]}"
do
if [[ ! -e "$directory" ]]
then
echo "Removing $directory from history"
unset HISTORY["$directory"]
fi
done
sleep 30
done
}
if [[ -e "${PID_FILE}" ]]
then
kill -0 $(cat "${PID_FILE}") &> /dev/null && IS_RUNNING=1
fi
read_cmd_opts $@
[[ -z "${MONITOR_FOLDER}" ]] && show_help && exit 1
monitor_and_upload &>${LOG_FILE} &
echo "$!" >"${PID_FILE}"
echo "NewsUP Folder Monitor launched!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment