#!/bin/bash
#                       /usr/local/bin/monitor
#  http://crystalfaeries.net/posix/bin/monitor
# celeste:crystalfaery MONITOR 2021-04-08 18:53:44+00:00
#
# Monitor live streams we are either broadcasting or downloading,
# and	video and audio files by decreasing size;
# then	ask to "delete" interactively (default NO)
#
# Options:
#	With no argument, use existing playlist.
#  -m	Media		playlist (media files on HD + mounted removable media)
#  -p	Priority	playlist (media files of priority selection)
#  -r	Rescanned	playlist (media files rescanned)
#  -w	Website		playlist (media files referenced by website)
#  -e	edit exclusions+playlist+exclusions
#  -h		help
# --help	help
#  -v		version info
# --version	version info
# NOTE:	Not yet reviewed for XDG compliance, and likely depends on other crystalfaeries.net scripts.
help=21				# lines of header to show as help

#	Configuration	(last one wins)
ip_address=10.42.0.1	# LAN IP# of streamripper host if it's not me
ip_address=127.0.0.1	# localhost if alone
alert="/usr/share/sounds/gnome/default/alerts/drip.ogg"	# alert sound
 website="${HOME}"/crystalfaeries.net/			# root directory of website contents
  diryou="${HOME}"/downloads/youtube.com/		# youtubes directory
  playou="${HOME}"/downloads/youtube.com/.playou.txt	# youtubes playlist
playlist="${HOME}"/.monitor/l.txt			# List being monitored
 mbackup="${HOME}"/.monitor/m.txt			# playlist including media
 pbackup="${HOME}"/.monitor/p.txt			# playlist priority
 wbackup="${HOME}"/.monitor/w.txt			# playlist references of website
 rbackup="${HOME}"/.monitor/r.txt			# playlist rescanned
  viewed="${HOME}"/.dush.media.txt			# list of already viewed files to skip
unhardlink "${viewed}"	# before any usage, in case we fdeduped thereby uniting ${viewed} + ${viewed}-

#	Arguments
false
webmedia=$?				# default all files not just website's
while	[ $# -ne 0 ]
do
case "$1" in
--version)
	head -n 4 $0 | tail -n 1	1>&2	# version message
	exit 0
	;;
-v)
	head -n 4 $0 | tail -n 1	1>&2	# version message
	exit 0
	;;
--help)
	head -n $help $0		1>&2	# help message
	exit 0
	;;
-h)
	head -n $help $0		1>&2	# help message
	exit 0
	;;
-e)
	# wash the sorted ${viewed} list of any deleted files and review in editor
	sort -u ${viewed} >	${viewed}-	|| exit -1
	cp /dev/null		${viewed}	|| exit -1
	while read f
	do
		if [ -s		"${f}" ]
		then # file exists and is not "deleted" (size 0)
			echo	${f} >> ${viewed}
		fi
	done < ${viewed}-			# keep old version as backup
	# review in editor
	vi "${viewed}" "$playlist" "${viewed}"	# edit viewed list and play list
	shift
	exit
	;;
-m)	# BEWARE RECURSIVE CALLING WITH THIS ARGUMENT!
	# media playlist includes all my mounted media
	find /{home,media}/`whoami`/	-type f \! -size 0	-print0	2>/dev/null			\
	|	grep -v	.abook										\
	|	grep -v	.byobu										\
	|	grep -v	.cache										\
	|	grep -v	.config										\
	|	grep -v	.firestorm									\
	|	grep -v	.local										\
	|	grep -v	.mozilla									\
	|	grep -v	.mixxx/analysis									\
	|	xargs -0 du						2>/dev/null			\
	|	sort -rn										\
	|	cut -f2-	> "${playlist}"
	shift
	;;
-p)
	# priority playlist includes all my mounted media's devideos & devaudios
	find	/{home,media}/`whoami`/*/{video/devideo,audio/devaudio}/	\
	-type f	\! -size 0	-print0	2>/dev/null				\
	|	xargs -0 du		2>/dev/null				\
	|	sort -rn	|	cut -f2-	>	"${playlist}"
	shift
	;;
-r)
	cp /dev/null "$playlist"	# reset playlist
	shift
	;;
-w)
	true
	webmedia=$?			# webmedia implies:
	cp /dev/null "$playlist"	# reset playlist
	shift
	;;
*)
	echo "$0 does not cognize $@"	1>&2	# error message
	echo "try: -h or --help"	1>&2	# error message
	exit -1
	;;
esac
done

#       Initialization
appname="$(basename $0)"
#tmpfile="$(mktemp /tmp/${appname}.XXXXXX)" || exit 1
#trap "let exitval=$?; rm -f $tmpfile; exit $exitval" 0 1 2 15

#	Monitor streamrips or largest media

if [ \! -s $playlist ]
then	# use old playlist if it exists, otherwise construct a new one:
	if [ $webmedia -eq 0 ]
	then	# webmedia playlist (this playlist presumes NO remote files, e.g. http://domain.net/file.ogg)
#du: cannot access '/audio/bruce_mitchell/earth_heal/02-earth_heal-f7driowvkru.ogg': No such file or directory
#du: cannot access '/audio/clairvoyance/celeste_crystalfaery.2010-11-10.full.mp3': No such file or directory
#du: cannot access '/audio/clairvoyance/celeste_crystalfaery.2011-11-10.ogg': No such file or directory
#du: cannot access '/audio/dwmlc/lv8.mp3': No such file or directory
#du: cannot access '/audio/posix/l5-campaign2.mp4': No such file or directory
#du: cannot access '/audio/quantumsandbox_1': No such file or directory
#du: cannot access '/audio/quantumsandbox_1.html': No such file or directory
#du: cannot access '/audio/quantumsandbox_1.rem': No such file or directory
#du: cannot access '/audio/quantumsandbox_1/dcqs_episode001.mp3': No such file or directory
#du: cannot access '/audio/rss': No such file or directory
		for f in $(cat "${website}"/audio/.crystalfaeries.files.audio.txt) ;
		do
			du $f				2>> "${HOME}"/.chronicle.log
		done	|	sort -nr	|	cut -f 2-	> $playlist
		rsync -auvH $playlist $wbackup
		cat "${viewed}" | while read f
		do	# prune pre-viewed items
			nice ionice -c 3 grep -v "${f}"	$playlist \
			>				$playlist-
			mv	$playlist-		$playlist
		done
	else	# regular playlist
		# Find all audio+video media by decreasing size:
		nice ionice -c 3 find						\
			"${HOME}"/documents/					\
			/media/`whoami`/*/documents/				\
			/audio/							\
			/media/`whoami`/*/audio/				\
			/usr/local/doc/audio/					\
			/video/							\
			/media/`whoami`/*/video/				\
			/usr/local/doc/video/					\
			\( \( -iname '*.avi'	\)				\
			-o \( -iname '*.dsf'	\)				\
			-o \( -iname '*.flac'	\)				\
			-o \( -iname '*.flv'	\)				\
			-o \( -iname '*.m4?'	\)				\
			-o \( -iname '*.mkv'	\)				\
			-o \( -iname '*.mov'	\)				\
			-o \( -iname '*.mp?'	\)				\
			-o \( -iname '*.og?'	\)				\
			-o \( -iname '*.opus'	\)				\
			-o \( -iname '*.ram'	\)				\
			-o \( -iname '*.rcd'	\)				\
			-o \( -iname '*.rm*'	\)				\
			-o \( -iname '*.spx'	\)				\
			-o \( -iname '*.swf'	\)				\
			-o \( -iname '*.vob'	\)				\
			-o \( -iname '*.wav'	\)				\
			-o \( -iname '*.webm'	\)				\
			-o \( -iname '*.wm?'	\)				\
			\) -type f \! -size 0 -print 2>/dev/null		\
			| xargs du						\
			| sort -nr						\
			| cut -f2- > $playlist	# generate updated playlist by decreasing size
		rsync -auvH $playlist $rbackup	# backup the full list 
		cat "${viewed}" | while read f
		do	# prune pre-viewed items
			nice ionice -c 3 grep -v "${f}"	$playlist \
			>				$playlist-
			mv	$playlist-		$playlist
		done
	fi
fi

# play them and ask if they should be removed ("y*" will remove)
# interlacing with playing local media files, we scan streaming ports:
# port 8000		icecast2	(our broadcast stream)
# port 8001-8007	streamrips	(downloading streams)
# port 8008		HTML server
# port 8009		streamrip KKCR
# port 8080		HTML server
let sleep=1
while sleep $sleep	# binary backoff upon failures
do
	# ports 80, 8008, 8080 are reserved for various web servers: http://crystalfaeries.net/servers.html
	#for  port in 8009 {8007..8001}
	for   port in 8009
	do	# iterate the relay channels for any streamripper relays
		mplayer http://$ip_address:$port
	done
	# we allocated 8000 for IceCast2 Stream (our own broadcast)
	mplayer fey.crystalfaeries.net:8000/stream.ogg			# IceCast2 Stream

	# now, about that playlist...
	file="$(head -n 1 "$playlist" 2>/dev/null)"		# the next largest media file
	if [ "X$file" == "X" ]
	then	# null line
	    if [ -s "$playlist" ]
	    then	# remove the null line
	#	rm				"$playlist"-	# does not like overwrites if on NAS
		mv		"$playlist"	"$playlist"-	# does not like overwrites if on NAS
		tail -n +2	"$playlist"- >	"$playlist" 2>/dev/null	# update the playlist
	    else	# rebuild the null list
		# We assume the youtube playlist of youtubedownload
		# after our scan-generated playlist,
		# in order to pick-up youtube videos downloaded after our scan started.
		if [ -s "$playou" ]
		then
			cp		"$playou"	"$playlist"
			cp /dev/null	"$playou"
		else	# this will HARD LOOP IF there are no media files at all!
			exec monitor -m	# rescan	####### RECURSIVE #######
		fi
	    fi
	else	# (dis)play it and ask to remove it	# DEVELOPMENT NOTE: review this else clause for improvement
	    nice ionice -c 3 grep       "$file" "${viewed}" >& /dev/null
	    if [[ $? -ne 0 ]]
	    then	# we have not yet viewed this file
	    	if [ -s "$file" ]
		then	# it is not a deleted (size 0) file
		  if [ -d "$file" ]
		  then
			echo "Skipping Directory $file" 1>&2	# why is this in the play list? or why don't we play the whole dir? recursive?
		  else
		    #	Display contextual info re: the file and its' usage in our website:
		    df --full | head -n 4 # show disk usage and all known copies of this file:
nice ionice -c 3    find	 /media/`whoami`/*/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \;  > /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find	 /media/`whoami`/*/{audio,video,documents}/ -samefile	   "${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find "${website}"/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find "${website}"/{audio,video,documents}/ -samefile	   "${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
		    sort -u											      < /tmp/$$.txt
		    echo "website usage:"
nice ionice -c 3    grep -i "$(basename ${file})"			\
			    "${website}"/*.html		\
			    "${website}"/src/*		\
			    "${website}"/src/.20*	\
			    2>/dev/null
		    mplayer "$alert" > /dev/null 2>&1	# warn the user of window popping up
		    sleep 3					# wait for user to chill out
		    extension="$(echo ${file} | sed 's/^.*\.//')"
		    case "${extension}" in
		    txt | text | description )	# Text File
			firefox	"$file" || icecat	"$file" || exit 1	# display the text
			#	Display contextual info re: the file and its' usage in our website:
			df --full | head -n 4 # show disk usage and all known copies of this file:
nice ionice -c 3    find	 /media/`whoami`/*/{audio,video,documents}/ -iname "$(basename ${file})"	  -exec ls -Flad {} \;  > /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find	 /media/`whoami`/*/{audio,video,documents}/ -samefile	"${file}"	  -exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find "${website}"/{audio,video,documents}/ -iname "$(basename ${file})" -exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find "${website}"/{audio,video,documents}/ -samefile	 "${file}"	  -exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
			sort -u												< /tmp/$$.txt
			echo "website usage:"
nice ionice -c 3	grep -i $(basename "${file}") "${website}"/src/* "${website}"/src/.20* "${website}"/contents.html	2>/dev/null
			;;
		    htm | html | pdf )	# HyperText Markup Language and PDFs via browser
			firefox		"$file" || icecat	"$file" || exit 1	# display the webpage
			#	Display contextual info re: the file and its' usage in our website:
			df --full | head -n 4 # show disk usage and all known copies of this file:
nice ionice -c 3    find	 /media/`whoami`/*/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \;  > /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find	 /media/`whoami`/*/{audio,video,documents}/ -samefile	"${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find "${website}"/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find "${website}"/{audio,video,documents}/ -samefile	 "${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
			sort -u											      < /tmp/$$.txt
			echo "website usage:"
			nice ionice -c 3 grep -i $(basename "${file}") "${website}"/src/* "${website}"/src/.20* "${website}"/contents.html
			;;
		    gif | jpeg | jpg | png | pnm | svg | tif )	# Still and Animated Pictures
			gwenview	"$file" || gthumb	"$file" || exit 1	# display the album artwork
			#	Display contextual info re: the file and its' usage in our website:
			df --full | head -n 4 # show disk usage and all known copies of this file:
nice ionice -c 3    find	 /media/`whoami`/*/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \;  > /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find	 /media/`whoami`/*/{audio,video,documents}/ -samefile	"${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find "${website}"/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3    find "${website}"/{audio,video,documents}/ -samefile	 "${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
			sort -u											      < /tmp/$$.txt
			echo "website usage:"
			nice ionice -c 3 grep -i $(basename "${file}") "${website}"/src/* "${website}"/src/.20* "${website}"/contents.html
nice ionice -c 3    find /audio/ /video/ -type f -name '*.html' -exec grep -H bruce_mitchell-earth_heal-f7driowvkru.jpg $(basename {} ) \;
			;;
		    aac | flac | m3u | m4a | mp3 | oga | ogg | opus | wma )	# Audio Playlists and Files and Videos
			mplayer	-fs -idx -loop 0 "$file" 2>/dev/null && let sleep=1 || let sleep=$sleep+$sleep	# binary back-off upon failure
			#	Display contextual info re: the file and its' usage in our website:
			df --full | head -n 4 # show disk usage and all known copies of this file:
nice ionice -c 3	find	 /media/`whoami`/*/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \;  > /tmp/$$.txt 2>/dev/null
nice ionice -c 3	find	 /media/`whoami`/*/{audio,video,documents}/ -samefile	"${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3	find "${website}"/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3	find "${website}"/{audio,video,documents}/ -samefile	 "${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
			sort -u											      < /tmp/$$.txt
			echo "website usage:"
			nice ionice -c 3 grep -i $(basename "${file}") "${website}"/src/* "${website}"/src/.20* "${website}"/contents.html
			;;
		    3gp | avi | mkv | mov | mp4 | ogv | ram | rm | webm )	# Video formats NOT handled by mplayer
			vlc -f			"$file"	&& let sleep=1 || let sleep=$sleep+$sleep	# binary back-off upon failure
#			vlc -f -L		"$file"	&& let sleep=1 || let sleep=$sleep+$sleep	# binary back-off upon failure
			#	Display contextual info re: the file and its' usage in our website:
			df --full | head -n 4 # show disk usage and all known copies of this file:
nice ionice -c 3	find	 /media/`whoami`/*/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \;  > /tmp/$$.txt 2>/dev/null
nice ionice -c 3	find	 /media/`whoami`/*/{audio,video,documents}/ -samefile	"${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3	find "${website}"/{audio,video,documents}/ -iname "$(basename ${file})"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
nice ionice -c 3	find "${website}"/{audio,video,documents}/ -samefile	 "${file}"	-exec ls -Flad {} \; >> /tmp/$$.txt 2>/dev/null
			sort -u											      < /tmp/$$.txt
			echo "website usage:"
			nice ionice -c 3 grep -i $(basename "${file}") "${website}"/src/* "${website}"/src/.20* "${website}"/contents.html
			;;
		    esac	# do we return the status of the last executed command within the case? or for case itself?
		    if [[ $? -ne 0 ]]
		    then	# not referenced by website
		    	delete -i	"${file}"	# do you want me to delete *this* copy?
		    fi
		    if [ -s "${file}" ]
		    then	# if file viewed and kept non zero (not deleted), mark as seen to hide it
			    echo	"${file}" >>	${viewed}
		    else	# if file viewed and deleted (missing or size 0 now), unmark as seen
			    nice ionice -c 3 grep -v	"${file}"	${viewed} >	${viewed}-
			    mv								${viewed}-	\
									${viewed}
	    	    fi
		  fi	# it was a file versus directory
		else	# it is a deleted (size 0) file
			:	# silently ignore it
		fi
	    else	# we have viewed this file
		    :	# silently ignore it
	    fi
	fi	# (dis)play it and ask to remove it
#	rm                     	 			"$playlist"-	# does not like overwrites if on NAS
	mv				"$playlist"	"$playlist"-	# does not like overwrites if on NAS
nice ionice -c 3 grep -v "$file"	"$playlist"- >	"$playlist"	# update the playlist
done
exit	$?	#	PAU FOR NOW

