#!/bin/bash
#                     /usr/local/bin/vodcastdownload.muse
# http://www.crystalfaeries.net/posix/bin/vodcastdownload.muse
# celeste:crystalfaery 2014-08-08 09:23:12+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 vodcast video listed in our download queue
# which is a list of URLs extracted from ~/Mail/vodcast

MAILTO=`whoami`
umask 2
# at the time of coding this, /home/downloads is vast and space does not have to be tested, therefore
# we are monitoring the volume upon which lives ~ = $HOME
cd	 /home/downloads	||	cd ~/video	 || exit 1
let hysteresis=0 #percent of disk space hysteresis between rsnapshotlimit and vodcast
# 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 rsnapshotlimit percentage in /etc/downloadlimit,
# that triggers vodcast to download a video, whose size is large enough, to push the disk usage percentage
# over the rsnapshotlimit, causing rsnapshotlimit to delete the oldest rsnapshot, which then,
# frees up enough space to trigger vodcast to download another video, 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 vodcast video 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/video | cut -f 1) || exit 2
	if [[ $percent -lt `head -n 1 /etc/downloadlimit | cut -f 1`-$hysteresis ]]
	then # we have freespace available, so check for another vodcast video
		if [ -s ~/Mail/vodcast ]
		then
			# we must extract a list of URLs from the Mail file vodcast
grep '.avi'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.avi".*$/\.avi/'    | sed 's/\.avi\].*$/\.avi/'    | sed 's/\.avi\}.*$/\.avi/'    >> ~/playlists/vodcast.txt
grep '.mov'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.mov".*$/\.mov/'    | sed 's/\.mov\].*$/\.mov/'    | sed 's/\.mov\}.*$/\.mov/'    >> ~/playlists/vodcast.txt
grep '.mp4'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.mp4".*$/\.mp4/'    | sed 's/\.mp4\].*$/\.mp4/'    | sed 's/\.mp4\}.*$/\.mp4/'    >> ~/playlists/vodcast.txt
grep '.m4v'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.m4v".*$/\.m4v/'    | sed 's/\.m4v\].*$/\.m4v/'    | sed 's/\.m4v\}.*$/\.m4v/'    >> ~/playlists/vodcast.txt
grep '.mv4'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.mv4".*$/\.mv4/'    | sed 's/\.mv4\].*$/\.mv4/'    | sed 's/\.mv4\}.*$/\.mv4/'    >> ~/playlists/vodcast.txt
grep '.ogv'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.ogv".*$/\.ogv/'    | sed 's/\.ogv\].*$/\.ogv/'    | sed 's/\.ogv\}.*$/\.ogv/'    >> ~/playlists/vodcast.txt
grep '.webm'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.webm".*$/\.webm/'  | sed 's/\.webm\].*$/\.webm/'  | sed 's/\.webm\}.*$/\.webm/'  >> ~/playlists/vodcast.txt
grep '.wmv'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.wmv".*$/\.wmv/'    | sed 's/\.wmv\].*$/\.wmv/'    | sed 's/\.wmv\}.*$/\.wmv/'    >> ~/playlists/vodcast.txt
grep '.h.264'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.h.264".*$/\.h.264/'| sed 's/\.h.264\].*$/\.h.264/'| sed 's/\.h.264\}.*$/\.h.264/'>> ~/playlists/vodcast.txt
grep '.264'	~/Mail/vodcast | sed 's/^.*http/http/' | sed 's/\.264".*$/\.264/'    | sed 's/\.264\].*$/\.264/'    | sed 's/\.264\}.*$/\.264/'    >> ~/playlists/vodcast.txt
			cp /dev/null	~/Mail/vodcast # this mail file serves only to queue vodcast URLs
			sed 's/ (.*$//' < ~/playlists/vodcast.txt > /tmp/vodcast.$$.txt
			sort -u < /tmp/vodcast.$$.txt > ~/playlists/vodcast.txt
		fi
		# process the next URL
		url="`head -n 1								 ~/playlists/vodcast.txt`"
		tail -n +2								 ~/playlists/vodcast.txt >	 /tmp/vodcast.$$.txt
		mv		 /tmp/vodcast.$$.txt					 ~/playlists/vodcast.txt
		if [ "$url" != "" ]
		then # download the next vodcast audio
			grep -v "$url"							 ~/playlists/vodcast.txt >	 /tmp/vodcast.$$.txt
			mv	 /tmp/vodcast.$$.txt					 ~/playlists/vodcast.txt
			wget -nv --mirror -c "$url"	 && let sleep=1	 || echo "$url" >>	 ~/playlists/vodcast.txt
			echo	"name_tidy -r  `echo $url | sed 's/^.*\/\///' | sed 's/\/.*$//'`"
				 name_tidy -r "`echo $url | sed 's/^.*\/\///' | sed 's/\/.*$//'`"
		fi
	fi
	if [ -s ~/playlists/vodcast.txt ]
	then
		echo "~/playlists/vodcast.txt has size:"
		ls -Flad ~/playlists/vodcast.txt
	else
		echo "~/playlists/vodcast.txt is null:"
		ls -Flad ~/playlists/vodcast.txt
		echo "~/playlists/vodcast.txt- is:"
		ls -Flad ~/playlists/vodcast.txt-
	fi
	if [[ $sleep -gt $sleepmax ]]
	then
		let sleep=$sleepmax	# maximum time to sleep
	fi
done
exit -1



syntax highlighted by Code2HTML, v. 0.9.1