#!/bin/bash
#                     /usr/local/bin/podcastdownload.faery
# http://www.kahealani.com/posix/bin/podcastdownload.faery
# celeste crystalfaery 2018-08-15 22:43:26+00:00
# This is spawned by cron on reboot, runs as a background daemon
# Whenever there is too much freespace on the main disk volume
# this automatically downloads the next podcast audio listed in our download queue
# which is a list of URLs extracted from ~/Mail/podcast

MAILTO=`whoami`
umask 2
cd	 $HOME/crystalfaeries.net/audio/downloads	||	cd ~/audio/downloads	 || exit 1
let hysteresis=0 #percent of disk space hysteresis between downloadlimit and podcast
# You can drop hysteresis down to 0 and see if your system thrashes, then increase by 1 and look again
# Thrashing would be that when the disk usage drops below the nominal downloadlimit percentage in $HOME/.downloadlimit,
# that triggers podcast to download a audio, whose size is large enough, to push the disk usage percentage
# over the downloadlimit, causing downloadlimit to delete the oldest rsnapshot, which then,
# frees up enough space to trigger podcast to download another audio, which... around we go, until no backups.
# Obviously this depends on the size of your disk and how big a "percent" of it is,
# how big an incremental rsnapshot is in percent of disk, and how big the largest podcast audio are.

let sleepmax=60	# longest sleep time
let sleep=1 # reset delay to minimum presuming success
while sleep "$sleep"m;do
	let sleep=$sleep+$sleep # binary backoff
#	let percent="`df -h | grep '/home$' | cut -c 41-43 | sed 's/ //' | sed 's/ //'`"	 || exit 2
	let percent=$(df -P -T -l | grep -v tmpfs | grep -v Capacity | sed 's/  / /g;s/  / /g;s/  / /g;s/ /	/g;s/%//' | cut -f 6- | grep '/home/audio$' | cut -f 1) || exit 2
	if [[ $percent -lt `head -n 1 $HOME/.downloadlimit | cut -f 1`-$hysteresis ]]
	then # we have freespace available, so check for another podcast audio
		if [ -s ~/Mail/podcast ]
		then
			# we must extract a list of URLs from the Mail file podcast
			cp /dev/null					 /tmp/podcast.$$.txt		 || exit 3
			grep '.mp3' 		~/Mail/podcast >>	 /tmp/podcast.$$.txt
			grep '.ogg' 		~/Mail/podcast >>	 /tmp/podcast.$$.txt
			grep '.wma' 		~/Mail/podcast >>	 /tmp/podcast.$$.txt
			sort -u						 /tmp/podcast.$$.txt	\
			| sed 's/^.*http/http/'						 	\
			| sed 's/\.mp3".*$/.mp3/'					 	\
			| sed 's/\.ogg".*$/.ogg/'					 	\
			| sed 's/\.wma".*$/.wma/'					 	\
			| sed 's/\.mp3}.*$/.mp3/'					 	\
			| sed 's/\.ogg}.*$/.ogg/'					 	\
			| sed 's/\.wma}.*$/.wma/'					 	\
			| sed 's/\.mp3\].*$/.mp3/'					 	\
			| sed 's/\.ogg\].*$/.ogg/'					 	\
			| sed 's/\.wma\].*$/.wma/'					 	\
			| sed 's/{mp3remote}//'						 	\
			| sed 's/{oggremote}//'						 	\
			| sed 's/{wmaremote}//'						 	\
			| sed 's/{\/mp3remote}//'					 	\
			| sed 's/{\/oggremote}//'					 	\
			| sed 's/{\/wmaremote}//'					 	\
			| sed 's/&.*$//'						 	\
			| sed 's/ (.*$//'						 	\
			| sed 's/>.*$//'						 	\
			| sort -u >>	 ~/playlists/podcast.txt
			cp		 ~/playlists/podcast.txt	 /tmp/podcast.$$.txt		|| exit 4
			sort -u		 /tmp/podcast.$$.txt	 >	 ~/playlists/podcast.txt	|| exit 5
			rm		 /tmp/podcast.$$.txt						|| exit 6
			cp /dev/null		 ~/Mail/podcast						|| exit 7
		fi
		# process the next URL
		url="`head -n 1								 ~/playlists/podcast.txt`"
		tail -n +2								 ~/playlists/podcast.txt >	 /tmp/podcast.$$.txt
		mv	      /tmp/podcast.$$.txt					 ~/playlists/podcast.txt
		if [ "$url" != "" ]
		then # download the next podcast audio
			grep -v "$url"							 ~/playlists/podcast.txt >	 /tmp/podcast.$$.txt
			mv /tmp/podcast.$$.txt						 ~/playlists/podcast.txt
			wget -nv --mirror -c "$url"	 && let sleep=1	 || echo "$url" >>	 ~/playlists/podcast.txt
			echo	"name_tidy -r  `echo $url | sed 's/^.*\/\///' | sed 's/\/.*$//'`"
				 name_tidy -r "`echo $url | sed 's/^.*\/\///' | sed 's/\/.*$//'`"
		fi
	fi
	if [[ $sleep -gt $sleepmax ]]
	then
		let sleep=$sleepmax	# maximum time to sleep
	fi
done
exit -1
