#!/bin/bash
#                       /usr/local/bin/watchlogs
#  http://crystalfaeries.net/posix/bin/watchlogs
# celeste:crystalfaery WATCHLOGS 2021-03-19 21:59:18+00:00
# we need finer control on journalctl, being flooded with ntp messages!
# watch logs in the background on a terminal.
# we take an optional -v or --version to provide version and responsibile author of THIS COPY
# we take an optional -h or --help to elicit this help information.
# we take an optional -k argument to simply kill running watches.
# we take an optional -e argument to edit the configuration file listing logfiles to watch
#		absolute paths taken from our configuration:
# $HOME/.config/watchlogs/watchlogs.cfg
#		which, if missing, will be generated with a default watch list (edit below).
let help=13	# of header lines to spew as "help" (number of this line -1)
while [ $# != 0 ]
do	# iterate arguments
	case "${1}" in
-v | --version)
	tail -n +4 $0 | head -n 1
	exit $?
	;;
-h | --help)
	head -n $help $0
	exit $?
	;;
-k | --kill)
	pkill tail	# root really should not do this
	pkill journalctl
	exit $?
	;;
-e | --edit)
	vi /home/`whoami`/.config/watchlogs/watchlogs.cfg	# edit the config file
	exit $?
	;;
*)
	echo "$0 does not cognize $1"	>&2
	echo "Try: $0 --help"		>&2
	exit $?
	;;
esac
shift	# consume argument
done	# iterating arguments

if [ \! -s						   /home/`whoami`/.config/watchlogs/watchlogs.cfg ]
then	# default logs:
	mkdir -p					   /home/`whoami`/.config/watchlogs || exit -1
	echo "# Default watchlogs configuration for `whoami` at `now`"	\
							>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
	echo $HOME/.chronicle.log			>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
	echo $HOME/.rawdog/log.txt			>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
	echo $HOME/downloads/youtube.com/.youlog.txt	>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
	echo /usr/local/share/ag/silverlog.txt		>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
	echo /var/log/apache2/error.log			>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
	echo /var/log/dar.log				>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
	echo /var/log/darchive.log			>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
	echo /var/log/rsnapshot.log			>> /home/`whoami`/.config/watchlogs/watchlogs.cfg
fi
echo "=======	`/usr/local/bin/now`	=======	$0	======="
echo -n "tail -n 2 -F "					>> /tmp/$$.txt

for domain in $(ps -ef --forest | grep wg[e]t | grep -v " sed " | sed 's/https:\/\/// ; s/http:\/\/// ; s/^.* // ; s/\/.*$//' | sort -u)
do
	if [ -r $HOME/downloads/$domain/.wget.txt ]
	then
		echo -n "$HOME/downloads/$domain/.wget.txt "	>> /tmp/$$.txt
	else
		echo "`/usr/local/bin/now` $0: $HOME/downloads/$domain/.wget.txt not readable."	1>&2
	fi
done

for f in $(grep -v '#' $HOME/.config/watchlogs/watchlogs.cfg | sort -u)
do	# ignore commented and empty lines
	echo -n "$f "						>> /tmp/$$.txt
done
pkill tail	# terminate previous incarnation(s) (-: and maybe some others especially if root)

# We really should learn the standard RUN / PID protocols...
# Kill our previous execution so that:
# * we cease tailing wgets no longer running
# * we begin tailing wgets started since then
# same for journalctl, so we don't run multiple copies

if [ -x $( which journalctl 2>/dev/null ) ]
then
	pkill	journalctl
#		journalctl -f	& disown %1	# tail     systemd	in background
else
	echo "journalctl not found in $PATH"
fi
`cat /tmp/$$.txt` 	& disown %1	# tail non-systemd	in background
sleep	6	# give the background job some time to run before we exit and user gets a prompt for next command
echo ""
echo "Upcoming Events:"
/usr/local/bin/events	# one-shot notice of upcoming events
exit $?
tail -F logfile|while read l; do sed 's/find/replace/g' <<< $l; done
